Internationalization (i18n) library for CodeIgniter

Warning: this tutorial and library are out of date. Prefer the last version, more robust.

About

Language in URL

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

Just one view

views/about.php

<h2>
  <?= $this->lang->line('who'); ?>
</h2>

XML file with localized text

language/about_lang.xml

<text id="who">
	<en>Who</en>
	<fr>Qui</fr>
</text>

Installation

  • Put MY_Language.php and MyController.php in libraries
  • Put url_helper.php in helpers
  • In config/autoload.php: $autoload['libraries'] = array('MyController');
  • In config/config.php, make sure $config['base_url'] corresponds to your configuration.
  • In config/routes.php add:
$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
 
$route['^fr$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];

How to use it

Create a controller

controllers/about.php

<?php
class About extends Controller {
	function index()
	{
		// load language file
		$this->lang->load('about');
 
		$this->load->view('about');
	}
}
?>

Create an XML file with the localized text

language/about_lang.xml

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <text id="about_me">
    <en>I'm just a man</en>
    <fr>Je ne suis qu'un homme</fr>
  </text>
</data>

Create a view

views/about.php

<p><?= $this->lang->line('about_me'); ?></p>

That's it!

Check it's working:

If it's not, try this: CodeIgniter 1.6.1 + i18n library (You may need to set RewriteBase in the .htaccess to make the URL rewriting work).

How it works

  • routes.php: ignore language in URL when looking for the controller
  • MyController.php: check language in URL
  • controllers/about.php: load localized strings for current language
  • views/about.php: display localized string

Note: if your root URL / is requested:

  • first visit: redirection to default_controller using language from visitor's browser.
  • following: redirection to default_controller using language from previous visit (from visitor's cookie).

Note: anchor(“about”, “test”) generates a link to /en/about (if the current language is English).

Customization

Add another language

Example: German (de)

  • In config/routes.php add:
$route['^de/(.+)$'] = "$1";
$route['^de$'] = $route['default_controller'];
  • In MY_Language.php add to $supportedLanguages:
"de"	=> "de",
"de-at"	=> "de",
"de-ch"	=> "de",
"de-de"	=> "de"
  • Add the German text in your XML file:
<text id="who">
	<en>Who</en>
	<fr>Qui</fr>
	<de>Wer</de>
</text>

That's it!

Goodies

Editing long XML files is not fun. To help you, I've written XMLLocalizationEditor for Mac OS X (source included).

Example:

XML code generated:

Result

 

Feedback

Hey! Its cool, and the osx app too!
peti
May 2, 2008
#1
Thanks for this great addition. Got one error when i want to load the default language in MY_Language.php: $CI->config->set_item('lang', $str); should be: $CI->config->set_item('language', $str); Carnalito
Carnalito
August 8, 2008
#2
Hi, thanks for this nice way for i18n of my site, it's really great. Anyway, I have a little problem - when I load library anywhere in my controller (like $this->load->library('validation'); in contructor or any function), it is unusable in real. When I lokk at log, I see that constructor of that library was called (like "Validation Class Initialized"), but when I try to use it (like $this->validation->set_rules($rules);) I get errors ("Undefined property: Admin::$validation" and "Call to a member function set_rules() on a non-object"). This is weird, I used the same pieceof code earlier when not using your library and it was ok. And what is weirder, when I put that library in autoload section to be loaded everytime, it works! (But now I found out that it works in controller, but not in view files, so when I want to use "$this->validation->username_field", I have to use it in controller and pass its value to view with my variable instead of using that statement in view. I hope I wrote it clear. That validation library as only demonstrative, I had that problem with both integrated library (like Validation) and my libraries. Can you tell where could be problem? Thank you
verisof
October 27, 2008
#3
Verisof, thanks a lot. I'm going to work on this and will get back to you very soon.
Jérôme Jaglale
October 27, 2008
#4
I had already written another complain (similar like before, but with models), but I think I figured it out. I didn't really like the whole MyController thing (loading class, that extends Controller, as library). So I deleted it and put its contents to private function in my own controller and I call it in contructor of that controller. It makes more sense for me and all the loading stuff works now. I put a snippet here: http://pastebin.com/f703d7778 Of course it's possible I overlooked something so correct me if I did something wrong.
verisof
October 28, 2008
#5
hi could you add language selection with flags on the view/about.php?
dtoolbox
February 24, 2009
#6
Hi, Good work! I need to change the default language. How can i do?. Thanks
jordi
February 24, 2009
#7
hi Jérôme, i'm having some trouble using HTML code in my language files. the controller didn't accept it, neither accents or html accents (&ccedil; = ç). there any way to overcome this?
lucio jr - brazil
March 14, 2009
#8
Hi Lucio, you can use CDATA sections in your XML files.
Jérôme Jaglale
March 15, 2009
#9