I'm researching hours and hours, but I could not find any clear, efficient way to make it :/
I have a codeigniter base website in English and I have to add a Polish language now. What is the best way to make my site in 2 language depending visitor selection?
is there any way to create array files for each language and call them in view files depends on Session from lang selection? I don't wanna use database.
Appreciate helps! I'm running out of deadline :/ thanks!!
I second Randell's answer.
However, one could always integrate a GeoIP such as http://www.maxmind.com/app/php or http://www.ipinfodb.com/. Then you can save the results with the codeigniter session class.
If you want to use the ipinfodb.com api You can add the ip2locationlite.class.php file to your codeigniter application library folder and then create a model function to do whatever geoip logic you need for your application, such as:
Follow this https://github.com/EllisLab/CodeIgniter/wiki/CodeIgniter-2.1-internationalization-i18n
its simple and clear, also check out the document @ http://ellislab.com/codeigniter/user-guide/libraries/language.html
its way simpler than
For easier use CI have updated this so you can just use
and to translate text
and if you want to warp it inside label then use optional parameter
This will output
For good reading http://ellislab.com/codeigniter/user-guide/helpers/language_helper.html
I am using such code in config.php:
.... and then i'm using usualy internal mechanizm of CI
o, almost forget! in views i using buttons, which seting cookie 'language' with language, prefered by user.
So, first this code try to detect "preffered language" setted in user`s useragent (browser). Then code try to read cookie 'language'. And finaly - switch sets language for CI-application
In the controller add following lines when you make the cunstructor
i.e, after
parent::Controller();
add below lines
create helper file lang_translate_helper.php with following function and put it in directory system\application\helpers
for each of the language, create a directory with language abbrevation like en, nl, fr, etc., under system\application\languages
create language file in above (respective) directory which will contain $lang array holding pairs label=>language_value as given below
nl_site_lang.php
en_site_lang.php
you can store multiple files for same language with differently as per the requirement e.g, if you want separate language file for managing backend (administrator section) you can use it in controller as $this->lang->load('nl_admin', 'nl');
nl_admin_lang.php
and finally to print the label in desired language, access labels as below in view
label('welcome', $this);
OR
label('hello word', $this);
note the space in hello & word you can use it like this way as well :)
whene there is no lable defined in the language file, it will simply print it what you passed to the function label.