Internationalization (i18n) Library is an extension of  CodeIgniter Language Library. Recently I extended this library with codeIgniter main language to build a multi-language site and found that very easy and interesting to implement codeigniter HMVC structure.

How It works:

It places the language in the URL

  • yoursite.com/en/about
  • yoursite.com/fr/about

Need an extension with  CodeIgniter Language Class

View:

<p>
  <?=lang('about.gender')?>
</p>

English language file:
$lang['about.gender'] = "I'm a man";

French language file:
$lang['about.gender'] = "Je suis un homme";

Installation:

  • Download: here
  • Put MY_Language.php and MY_Config.php in system/application/libraries

Configuration :

  • You must be using pretty URLs (without index.php). With Apache it's usually achieved with mod_rewrite through an .htacess

In config.php

  • $config['base_url'] must correspond to your configuration.
  • $config['index_page'] = ””

In config/routes.php add

<?php
// URI like '/en/about' -> use controller 'about'
$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
 
// '/en' and '/fr' URIs -> use default controller
$route['^fr$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
?>

Use:

You have to build a bilingual English/French page.

language files:

system/application/language/english/about_lang.php

<?php
 
$lang['about.gender'] = "I'm a man";
 
/* End of file about_lang.php */
/* Location: ./system/language/english/about_lang.php */

system/application/language/french/about_lang.php
<?php
 
$lang['about.gender'] = "Je suis un homme";
 
/* End of file about_lang.php */
/* Location: ./system/language/french/about_lang.php */

Controller:

 
system/application/controllers/about.php

<?php
class About extends Controller {
 
    function index()
    {
        // you might want to just autoload these two helpers
        $this->load->helper('language');
        $this->load->helper('url');
 
        // load language file
        $this->lang->load('about');
 
 
        $this->load->view('about');
    }
}
 
/* End of file about.php */
/* Location: ./system/application/controllers/about.php */

View:


 system/application/views/about.php
 

<p><?=lang('about.gender')?></p>

<p><?=anchor('music','Shania Twain')?></p>

For more click here