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.
Have the language in the URL
Keep using 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";
Result with maestric.com/en/about
<p>I'm a man</p>
Result with maestric.com/fr/about
<p>Je suis un homme</p>
MY_Language.php in system/application/librariesMY_url_helper.php in system/application/helpersmod_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'];
Let's build a bilingual English/French page.
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 */
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 */
system/application/views/about.php
<p><?=lang('about.gender')?></p> <p><?=anchor('music','Shania Twain')?></p>
<p>I'm a man</p> <p><a href="http://mywebsite.com/en/music">Shania Twain</a></p>
<p>Je suis un homme</p> <p><a href="http://mywebsite.com/fr/music">Shania Twain</a></p>
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.site_url('about/my_work'); // http://mywebsite.com/en/about/my_work site_url('css/styles.css'); // http://mywebsite.com/css/styles.css
$this->lang->lang(); // en
anchor($this->lang->switch_uri('fr'),'Display current page in French');
MY_url_helper ensures your links are prefixed by the appropriate language.
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
In system/application/libraries/MY_Language.php
”” from the $special array$default_uri to something else like home/ will be redirected to en/home, if English is your default language$languages arraysystem/application/libraries/MY_Language.php: add new item in $languages arrayconfig/routes.php: add // example: add German language (de) $route['^de/(.+)$'] = "$1"; $route['^de$'] = $route['default_controller'];
system/application/language