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.phpandMY_Config.phpinsystem/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





Awesome post !! +fav
I find this pretty interesting:)
Nice indeed but about is not very french. You can make it better by making custom url depending of the language :
add a route for non english url :
$route['fr/apropos'] = “about”;
You can also get those words from your language file.
Hey, how would you use the switch_uri function with the added routes?
fr/apropos will become en/apropos once you use switch_uri…
It won’t localize the second segment… what would be the best way to achieve this?
well…i have a question. What i need to do when the data of my site is coming from database. Do i need to make multiple entry in database for multiple language?
well…i have a question. What i need to do when the data of my site is coming from database. Do i need to make multiple entry in database for multiple language?
You have to maintain different languages files for labels, caption etc. if you want content in different language then you have to store that on DB.
Khub Bhalo. Actually I am new to codeigniter. According to you I have used the “i18n Library” in my site to make it multilingual and it’s working exactly. But it does not solve my problem properly. English is the default language of my site. So I need site URL should be http://mysite.com/about/ instead of http://mysite.com/en/about/
only for English and when I will switched to the other languages then URL will looks like at present (http://mysite.com/se/about/, http://mysite.com/fr/about/ and etc). Is it possible to do that with “i18n Library”? If yes, can you please help me? Thanks in advance.
You can remove this level by .htaccess modification, The advantages of this tag is, you can make SEO friendly URLs in each language but with this you can’t do this. you can use _remap() function for that
Thanks for your answer. If you don’t mind can you please give me an example? I think that will be very helpful for me.
how about using database to create multi language content?
regards
Hi,
This is very good post, i contributed one plugin for codeigniter framework. Once check it – http://www.anil2u.info/2010/05/29/codeigniter-multi-language-support-using-google-translate-api/
Thank you.
Thanks man..
im learning CI one by one…
^^,
Hi.. Thanks for the instructions. I’m currently rebuilding the backend of our website, to be written in the CI framework.
To make it multilingual is important, and your guide helped me.
Hello Kazi Abdullah,
I make this website : http://www.emlakbulun.com/registration/corporate/37
currently this site is in two language : Turkish & English
but when i fetch translate value from database. i found some characters like : ?, 1/4 etc.
Can you please give me solution about the problem.
Thanks,
Mohammed Khalid Khan
Halo,
I believe I did exactly what’s written here
but I dunno why when I go to page about, it always falls back to main page…
my default address is http://localhost/gu
and I’m using CI vers. 1.7.3
is it ok?
This tutorial is plagiarim from this website : http://www.maestric.com/doc/php/codeigniter_i18n
You can find an update for CI 2.0 over here : http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n/
Sumon, don’t forget to mention your source !
how with codeignter 2.0.0
Nice code,
But, I am whit a problem.
$config['language'] = “”;
The language my Site change when I exchange above.
For example:
$config['language'] = “french”; -> site in french
$config['language'] = “english”; -> site in english
When I use by URI mysite/en/about or mysite/fr/about. Iit’s calling always o $config.
Please, help me.
Hi, Nice post !
Translation is good but when I come back to my home page is always with the default language of the browser.
For example: firefox is in english.
I click to the french link to translate, it works. I navigate on my web site, it will be always in french but if i click on my home page link, my french langage is replaced by the default langage of my browser (english)
Could you help me please?
what about 2,0,3
I think followup url is fine
http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n
yes.It seems.Any reviews and commnents