Multi Language in CodeIgniter

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.

  1. $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:

  1. $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:

  1. $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:

  1. $this->lang->load(‘information’,’english’);

Next to use the hooks, add the following line to the application/config/config.php file.

  1. $config[‘enable_hooks’] = TRUE;

Once the hooks are enabled, open application/config/hooks.php file and define a hook through the following code:

  1. $hook[‘post_controller_constructor’] = array(
  2. ‘class’ => ‘LanguageLoader’,
  3. ‘function’ => ‘initialize’,
  4. ‘filename’ => ‘LanguageLoader.php’,
  5. ‘filepath’ => ‘hooks’
  6. );

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:

  1. class LanguageLoader
  2. {
  3. function initialize() {
  4. $ci =& get_instance();
  5. $ci->load->helper(‘language’);
  6. $ci->lang->load(‘information’,’english’);
  7. }

This will load the appropriate language file. Now you could easily fetch individual text lines through the following code

  1. $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.

  1. $autoload[‘libraries’] = array(‘session’);
  2. $autoload[‘helper’] = array(‘url’);

Next, create LanguageSwitcher.php controller for actually implementing the language switch.Add the following code to it:

  1. <?php if ( ! defined(‘BASEPATH’)) exit(‘Direct access allowed’);
  2. class LanguageSwitcher extends CI_Controller
  3. {
  4. public function __construct() {
  5. parent::__construct();
  6. }
  7. function switchLang($language = “”) {
  8. $language = ($language != “”) ? $language : “english”;
  9. $this->session->set_userdata(‘site_lang’, $language);
  10. redirect($_SERVER[‘HTTP_REFERER’]);
  11. }
  12. }

Now replace the code in application/hooks/LanguageLoader.php file with the following code:

  1. <?php
  2. class LanguageLoader
  3. {
  4. function initialize() {
  5. $ci =& get_instance();
  6. $ci->load->helper(‘language’);
  7. $siteLang = $ci->session->userdata(‘site_lang’);
  8. if ($siteLang) {
  9. $ci->lang->load(‘information’,$siteLang);
  10. } else {
  11. $ci->lang->load(‘information’,’english’);
  12. }
  13. }
  14. }

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:

  1. RewriteEngine on
  2. RewriteCond $1 !^(index\.php)
  3. 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.

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email