Internationalization (i18n) library for CodeIgniter

Warning: this tutorial and library are out of date. Prefer the last version, which works with scaffolding and fixes some problems.

Updated on April 15 2009 (fixed reported problems). Previous version is still available.

What it does

Have the language in the URL

  • maestric.com/en/about
  • maestric.com/fr/about

Keep using CodeIgniter Language Class

Example

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";

Result with maestric.com/en/about

<p>I'm a man</p>

Result with maestric.com/fr/about

<p>Je suis un homme</p>

Installation

  • Put MY_Language.php in system/application/libraries
  • Put MY_url_helper.php in system/application/helpers

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

// 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

Let's 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>

Test

http://your_base_url/en/about

<p>I'm a man</p>
 
<p><a href="http://mywebsite.com/en/music">Shania Twain</a></p>

http://your_base_url/fr/about

<p>Je suis un homme</p>
 
<p><a href="http://mywebsite.com/fr/music">Shania Twain</a></p>

Notes

  • You might need to translate some of CodeIgniter's language files in system/language. Example: if you're using the “Form Validation” library for French pages, translate system/language/form_validation_lang.php to system/application/language/french/form_validation_lang.php.
  • links to internal pages are prefixed by the current language, but links to files are not.
site_url('about/my_work');
// http://mywebsite.com/en/about/my_work
 
 
site_url('css/styles.css');
// http://mywebsite.com/css/styles.css
  • Get the current language
$this->lang->lang();
// en
  • Switch to another language
anchor($this->lang->switch_uri('fr'),'Display current page in French');
  • the root page (/) is supposed to be some kind of splash page, without any specific language. This can be changed: see “No splash page” below.

How it works

MY_url_helper ensures your links are prefixed by the appropriate language.

Options

Special URIs

A special URI is not prefixed by a language. The root URI (/) is by default a special URI.

You might need other special URIs, like for an admin section, which would be in just one language.

In system/application/libraries/MY_Language.php, add admin to the $special array. That's it! Now links to admin won't be prefixed by the current language.

site_url('admin');
// http://mywebsite.com/admin

No splash page

In system/application/libraries/MY_Language.php

  • remove ”” from the $special array
  • set $default_uri to something else like home
  • now a request to / will be redirected to en/home, if English is your default language
  • the default language is the first item of the $languages array

Add a new language

  • system/application/libraries/MY_Language.php: add new item in $languages array
  • config/routes.php: add
// example: add German language (de)
$route['^de/(.+)$'] = "$1"; 
$route['^de$'] = $route['default_controller'];
  • create corresponding language folder in system/application/language
 

Feedback

Thx for the library.

dx_auth users have to remove the begining '/' from URI locations at the dx_auth config file
&#40; '/auth/logout/' -> 'auth/logout'&#41;

// URI Locations
$config['DX_logout_uri'] = 'auth/logout/';
$config['DX_register_uri'] = 'auth/register/';
$config['DX_forgot_password_uri'] = 'auth/forgot_password/';
$config['DX_change_password_uri'] = 'auth/change_password/';
$config['DX_cancel_account_uri'] = 'auth/cancel_account/';
stoney
February 18, 2010
#1