Internationalization (i18n) library for CodeIgniter

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

// 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_Config.php contains an override of site_url(): language segment added (when appropriate) to URLs generated with anchor(), form_open()...

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. 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 language to $languages array
// example: German (de)
'de' => 'german',
  • config/routes.php: add new routes
// example: German (de)
$route['^de/(.+)$'] = "$1"; 
$route['^de$'] = $route['default_controller'];
  • create corresponding language folder in system/application/language. For this “German” example, it would be called german.
 

Feedback

This April 15, 2009 update fixes the issues reported before:

- Mark: autoloading of language files wasn't working

- François: language segment was sometimes added to a link when there was already one

- Frank: it wasn't possible to use the library methods in a controller's constructor

Thank you all for your feedback.
Jérôme Jaglale
April 15, 2009
#1
Thanks Jerome, get an error and not sure why, simply replaced the old files with new ones, changed the MY to FW as I had before when all was fine. Fatal error: Call to undefined method FW_Language::init_language() in W:\www\s\system\libraries\Hooks.php on line 205
BS
April 16, 2009
#2
BS, please remove the hook in config/hooks.php, it's not needed anymore.
Jérôme Jaglale
April 16, 2009
#3
When I try to switch to /es/ I get this error: Unable to load the requested language file: language/profiler_lang.php There is no file by that name, and language folder is setup the same as worked with previous version... what did I miss this time? :) thanks a million!
BS
April 16, 2009
#4
Oops, i forgot to mention, this breaks scaffolding links (edit/add/etc), were you aware? I can send you a link to show you what I'm talking about via email rjdjohnston(at)gmail(dot)com if you like.
BS
April 16, 2009
#5
Started a thread on my error from switch from en to es, see here: http://codeigniter.com/forums/viewthread/111938/ Dank je!

April 17, 2009
#6
This April 17, 2009 update fixes the issues reported before:

- BS: scaffolding wasn't working
- Dan: wrong URL generated with form_open(), redirection bugs

Note:
If you've been using a previous version, make sure to remove helpers/MY_url_helper.

Note from Dan:
For people using dx_auth module they will need to edit the config file (system/application/config/dx_auth.php) at line 188 onwards there are some variable definitions e.g. $config['DX_deny_uri'] = '/auth/deny/'; It is best to remove the leading / e.g. $config['DX_deny_uri'] = 'auth/deny/'; otherwise when a user is redirected to the login form the url comes up as http://idm.ts/en//auth/login - this does actually display the right page and doesnt affect it but just to make it look professional!

Thank you for your feedback.
Jérôme Jaglale
April 17, 2009
#7
This April 20, 2009 update fixes a bug: the redirection wasn't working when the language in the URL was wrong.
Jérôme Jaglale
April 20, 2009
#8
Thank you very much for this very useful library! Saved me a ton of work.
Jeroen van der Gulik
May 18, 2009
#9
Thanks a lot, it's excellent !
alyvest
May 20, 2009
#10
I've just modified a line in MY_language.php If the URI doesn't content language information, i'm loading the page asking with the default language. I don't use the default uri anymore Code: header("Location: " . $CFG->site_url($this->lang($this->languages[$this->default_lang()]).'/'.$segment), TRUE, 302);
alyvest
May 20, 2009
#11