MULTI LANGUAGE IN CODEIGNITER FRAMEWORK
Create Language Files
In application/language folder, create new language files as below:
English Language File
Create new folder name english. In this folder, create new PHP file named content_lang.php as below:
defined('BASEPATH') or exit('No direct script access allowed');$lang['message'] = 'Hello World';?>
French Language File
Create new folder name french. In this folder, create new PHP file named content_lang.php as below:
defined('BASEPATH') or exit('No direct script access allowed');$lang['message'] = 'Bonjour tout le monde';?>
German Language File
Create new folder name german. In this folder, create new PHP file named content_lang.php as below:
defined('BASEPATH') or exit('No direct script access allowed');$lang['message'] = 'Hallo Welt';?>
Set BASE URL
In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:
$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';
AutoLoad Helpers
In CodeIgniter Project, Open autoload.php file in config folder. Add url helper to helper config as below:
$autoload['helper'] = array('url');
AutoLoad Libraries
In CodeIgniter Project, Open autoload.php file in config folder. Add session library to libraries config as below:
$autoload['libraries'] = array('session');
Create Language Hook
Create new hook and config to run hook as below:
Enable Hook
In CodeIgniter Project, Open config.php in config folder. Set value for enable_hooks config as below:
$config['enable_hooks'] = TRUE;
Define New Hook
In CodeIgniter Project, Open hooks.php file in config folder. Define new hook as below:
defined('BASEPATH') OR exit('No direct script access allowed');$hook['post_controller_constructor'] = array( 'class' => 'LanguageLoader', 'function' => 'initialize', 'filename' => 'LanguageLoader.php', 'filepath' => 'hooks');
Create LanguageLoader Class
Create new PHP file named LanguageLoader.php in application/hooks folder as below:
defined('BASEPATH') or exit('No direct script access allowed');class LanguageLoader{ function initialize() { $ci = & get_instance(); $ci->load->helper('language'); $siteLang = $ci->session->userdata('site_lang'); if ($siteLang) { $ci->lang->load('content', $siteLang); } else { $ci->lang->load('content', 'english'); } }}
Create Controller
Create new PHP file named demo.php in controllers folder as below:
defined('BASEPATH') or exit('No direct script access allowed');class Demo extends CI_Controller{ public function __construct() { parent::__construct(); } function index() { $this->session->set_userdata('site_lang', "english"); $this->load->view('demo/index'); } function switchLang($language = "") { $this->session->set_userdata('site_lang', $language); redirect('demo'); }}
Set Default Controller
In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'demo';
Create View
Create new folder named demo in views folder. In this folder, create new views as below:
Index View
Create new PHP file named index.php as below:
defined('BASEPATH') or exit('No direct script access allowed'); ?><html> <head> <title>Languagetitle> head> <body> <div style="margin:0 auto; text-align: center;"> <a href=" echo site_url("demo/switchLang/english"); ?>">Englisha> | <a href=" echo site_url("demo/switchLang/german"); ?>">Germana> | <a href=" echo site_url("demo/switchLang/french"); ?>">Frencha> <br><br> echo $this->lang->line('message'); ?> div> body>html>
Structure of CodeIgniter Project
Run Application
Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index
Output
Select English language
Output
Select French language
Output
Select German language
Output
0 Comments