Specify Default Language and Language Options
Go to application/config folder and open config.php. Add the following line to specify the website’s default language.
- $config[‘language’] = ‘english’;
Now to create the language options, of to application/language folder and create separate subfolders for each language. Next, in each subfolder, create a file with the suffix _lang. For instance, if you wish to offer a file named information.php in English and German languages, use the following structure:
Application→ language → English → information_lang.php
Application→ language → German → information_lang.php
Set up Language Files
Within the language file, information_lang.php, add every line of text to a $lang array in the following format:
- $lang[‘language_key’] = ‘type your message’;
For instance, the entry for the text line, “Type message in German” in the file in the German language folder would look like:
- $lang[‘welcome_message’] = ‘Type message in German’;
Load Language File
Once the files are in place, it is time to load the language files.
For this, add the following code to the controller’s _construct() function:
- $this->lang->load(‘information’,’english’);
Next to use the hooks, add the following line to the application/config/config.php file.
- $config[‘enable_hooks’] = TRUE;
Once the hooks are enabled, open application/config/hooks.php file and define a hook through the following code:
- $hook[‘post_controller_constructor’] = array(
- ‘class’ => ‘LanguageLoader’,
- ‘function’ => ‘initialize’,
- ‘filename’ => ‘LanguageLoader.php’,
- ‘filepath’ => ‘hooks’
- );
Go to application/hooks folder and create a file named LanguageLoader.php. Create the LanguageLoader class in the file and add the following code to it:
- class LanguageLoader
- {
- function initialize() {
- $ci =& get_instance();
- $ci->load->helper(‘language’);
- $ci->lang->load(‘information’,’english’);
- }
This will load the appropriate language file. Now you could easily fetch individual text lines through the following code
- $this->lang->line(‘welcome_message’);
Switch Language
At this point all the necessary ideas for multilingual web apps are in place. It is time to switch languages. But before this, open the application/config/autoload.php file and load SESSION library and the URL helper.
- $autoload[‘libraries’] = array(‘session’);
- $autoload[‘helper’] = array(‘url’);
Next, create LanguageSwitcher.php controller for actually implementing the language switch.Add the following code to it:
- <?php if ( ! defined(‘BASEPATH’)) exit(‘Direct access allowed’);
- class LanguageSwitcher extends CI_Controller
- {
- public function __construct() {
- parent::__construct();
- }
- function switchLang($language = “”) {
- $language = ($language != “”) ? $language : “english”;
- $this->session->set_userdata(‘site_lang’, $language);
- redirect($_SERVER[‘HTTP_REFERER’]);
- }
- }
Now replace the code in application/hooks/LanguageLoader.php file with the following code:
- <?php
- class LanguageLoader
- {
- function initialize() {
- $ci =& get_instance();
- $ci->load->helper(‘language’);
- $siteLang = $ci->session->userdata(‘site_lang’);
- if ($siteLang) {
- $ci->lang->load(‘information’,$siteLang);
- } else {
- $ci->lang->load(‘information’,’english’);
- }
- }
- }
Notice that I have use the URl for switching language without index.php. Thus I will a .htaccess file in the root directory with the following code:
- RewriteEngine on
- RewriteCond $1 !^(index\.php)
- RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
Wrapping Up
As you could see, creating a multilingual website in CodeIgniter is simple enough. If you need help in implementing the idea in your CodeIgniter project, just drop a line and I will get back to you.