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
is this library work with ocular layout library? thx..
Hammudi
July 15, 2009
#12
Thanks a lot, great work. I discovered small bug in MY_Language: If var $special = array (""); - this array is empty, the function is_special() doesn't work as expected because even empty the URL is treated as special
Martin Rusev
August 6, 2009
#13
Thanks Jérôme, great script! Is there a way I could translate the links too? Example: English url: en/about French url: fr/propos Both URLs routed to the about.php controller where the language strings returned according to the langugae. This would highly increase the search-engine friendlyness of the script. I'm trying to find a solution, but I'm a beginner...
vito
August 15, 2009
#14
Amazing, you saved my life bud!!!
Jorch
August 20, 2009
#15
Just integrated with CI with HMVC, worked fine till now. :)
Mahbubur Rahman
August 24, 2009
#16
Very nice. It's possible to remove the language from the URI ?
Ed
August 25, 2009
#17
Ed: no, the goal here is actually to rely on the language in the URI: so we don't need to set a cookie on the client browser or use the HTTP headers sent by the browser. Also the page will be referenced by search engines in both languages.
Jérôme Jaglale
August 25, 2009
#18
this looks very good... I will try it out merci beaucoup Jerome...
Luca
August 26, 2009
#19
Hi, just to mention that it worked like a charm... indeed saved me hours of work :-) thanks a lot
Luca
August 26, 2009
#20
So i had to drop the HMVC with i18n library. Views in folders don't seem to work easily. Need to change a lot in router to get HMVC+i18n to work. So I'm back without HMVC because i18n was the main need.
Mahbubur Rahman
August 31, 2009
#21
great work man!!
Leandro Gilioli
September 18, 2009
#22
This is great! Really usefull!
demogar
September 18, 2009
#23
very great lib, but I need also HMVC to work, any suggestions? another thing, redirect default language is nice but if you type /controllername/functionname it does not redirect to let's say /en/controllername/functionname
mike
October 17, 2009
#24
This is all fine and dandy, but having to manually write all the routes is not all that useful. Plus, if you are trying to make a multilingual site, you do not want the URIs to be in english:

http://your-base-url/en/about
http://your-base-url/fr/about

It should be like this:
http://your-base-url/en/about
http://your-base-url/fr/a-propos

Ideally this would work by either checking your database to find the uri, or have the system dynamically generate the routing config file.

That's my two cents worth.
Peter Hebert
October 22, 2009
#25
Peter, what do you mean by "having to manually write all the routes"? It's only two routes for each language.

I agree about the localized URLs, that should be an option. I got something working on a project, but it's too much of a hack. I have yet to come with a better solution. Thanks for the feedaback!
Jérôme Jaglale
October 22, 2009
#26
I mean that if you want to call your controller/function by the appropriate name in each language, you would need to write a route for each of them:

ie. if your controller is 'about', and the function is 'men', then you would need two routes if you wanted to call it in english, french, and spanish:

$route['a-propos/hommes'] = "about/men";
$route['acerca/hombres'] = "about/men";

The same would apply for every controller and function.
Peter Hebert
October 27, 2009
#27
Ok, you were already talking about the localization of the URLs..

But you would have to declare them somewhere anyway, routes.php or database, no?

I don't see how you could generate that, especially if you use parameters with the functions: store/buy/chocolat_blanc -> store/buy/white_chocolate

Any ideas?
Jérôme Jaglale
October 27, 2009
#28
Great work, i have a question How can i load a different language from the controller? I have to send an email to the user which has a different preffered language than the admin, how can i load the translation for the user ? Thank you!
Cristian Boboc
November 3, 2009
#29
Automatic configuration for ci_I18n library

//create config/supported_lang.php file
$config [ 'supported_lang'] =
array (
'en' => 'Spanish',
'in' => 'English',
'gl' => 'Galician',
'ca' => 'catalan',
'ja' => 'japanese',
'de' => 'Dutch',
);

//in libraries/MY_Language.php

replace var $languages= array(...)
like this var $languages;

in the class constructor
add
global $CFG;
$this->languages = $CFG->item ( 'supported_lang');

//in config/routes.php
add
global $CFG;

//Load the config file
$CFG->load( 'supported_lang');

//browser language
$browser_lang = strtolower (substr ($_SERVER ["HTTP_ACCEPT_LANGUAGE"], 0.2));
//if supported
if(array_key_exists($browser_lang,$CFG->item('supported_lang')))
{
$CFG->set_item('language', $CFG->item($browser_lang,'supported_lang'));
}

$route['default_controller'] = "my_default_controller";
$route['scaffolding_trigger'] = "";
//
foreach($CFG->item('supported_lang') as $lang => $value)
{
$route["^$lang/(.+)$"] = "$1";

$route["^$lang\$"] = $route['default_controller'];

}

I want them to be useful
Jérôme Jaglale thank for their fantastic
library and sorry for my English
tolo
November 3, 2009
#30
Cristian, you could use the second parameter:
$this->lang->load('about', 'fr');
Jérôme Jaglale
November 3, 2009
#31
Great work. I am doing this small assignment and got familiar with CodeIgniter just for that. The aim of the assignment is i18n. I am using the form_validation library for my assignment and I need to have localized error messages for the library. If I create a lang file under application/languages/french/form_validation_lang.php how can I dynamically load it in the controller? Will your library do this? Thanks!
chamila
December 31, 2009
#32
Yes, your helper sure helps!! Thanks again for the great work. :)
chamila
December 31, 2009
#33
This is great !! Is this library working with HMVC library?
James
January 2, 2010
#34
@alyvest: I think the better solution would be header("Location: " . $CFG->site_url($this->lang($this->languages[$this->default_lang()]).$URI->uri_string()), TRUE, 302); If no language is provided
Solutionvibes.com
January 9, 2010
#35
hi this one is one of my favorites CI library i have one question is if i have default language is English and i dont want to use en in url and when i want to change language then i want lang name in url fr default maestric.com/about localized maestric.com/fr/about
umefarooq
January 12, 2010
#36
umefarooq, it's probably possible with a bit of customization: just one route for the fr (routes.php), no redirect if there's isn't any language in the URL and don't add the language segment if it's English (MY_Language.php).
Jérôme Jaglale
January 12, 2010
#37
Hey Jérôme, thanks for the library, it's pretty wicked! Definitely saved me some time :) I am stuck with a little problem tho and was wondering if u can help me out. I'm trying to reroute some URIs to the same controller as such: /collection/2010 /collection/2009 etc should all reroute to the 'collection' class and pass the 'function' (2009 or 2010) as a variable. I've tried adding a simple route rule to the routes.php but haven't found a way to do so correctly... you got any ideas?!
Lennart Thiel
January 16, 2010
#38
Hello, I tried to implement your script, but i get this error: Fatal error: Call to a member function localized() on a non-object in [app]\application\libraries\MY_Config.php on line 19 . Can you please explain what might be wrong? Thanks and great job ;)
Dever
January 17, 2010
#39
Hi your library is no more working with modular separation here is the link http://codeigniter.com/forums/viewthread/121820/ so what's the solution for it.
webmasterdubai
February 3, 2010
#40
Hi I have made some changes in your MY_Config.php file regarding special URI. I was working on it I have created admin panel which is my special URI but the form URI alway's getting lang segment in it. I have made some changes in site_url function may be will help all this library users
function site_url($uri = '')
 {
            
  if (is_array($uri))
  {
   $uri = implode('/', $uri);
  }
  
  if (function_exists('get_instance'))  
  {
   $CI =& get_instance();
                        $segment =  $CI->uri->segment(1);
                        if(!$CI->lang->is_special($segment))
   $uri = $CI->lang->localized($uri);   
  }

  return parent::site_url($uri);
 }
It will not localize form for special URI
webmasterdubai
February 3, 2010
#41
here again modified site_url function
function site_url($uri = '')
 {
            
  if (is_array($uri))
  {
   $uri = implode('/', $uri);
  }
  
  if (function_exists('get_instance'))  
  {
   $CI =& get_instance();

                        $segment =  $CI->uri->segment(1);

                        if(!in_array($segment, $CI->lang->special))
   $uri = $CI->lang->localized($uri);
                        
  }

  return parent::site_url($uri);
 }
webmasterdubai
February 4, 2010
#42
Him Jerome!

I'm testing this on my local machine and I want to be able to change the language on my home page. I have this code:

&lt;?php echo anchor($this->lang->switch_uri('en'), 'EN'); ?&gt;
&lt;?php echo anchor($this->lang->switch_uri('ro'), 'RO'); ?&gt;

When I press any of the two links, the language remains the same... the default RO...

Can you help me with that?
Thanks!
Constantin
February 10, 2010
#43
Hi again!
I got this working by adding

else
{
$uri = $lang;
}

in the 'switch_uri' function.

Thanks again for this really useful!
Constantin
February 10, 2010
#44
* Thanks again for this really useful! :)
Constantin
February 10, 2010
#45
** Thanks again for this really useful feature!

I wonder what's with this hurry... :)
Constantin
February 10, 2010
#46
how can we get active language using this library.
webmasterdubai
March 1, 2010
#47
@webmasterdubai:

you could use the uri class in your controller;
$data['curr_lang'] = $this->uri->segment(1);
remote
March 19, 2010
#48
thanks you
khacntt
March 23, 2010
#49
I have implemented this technic to my site. But when i check the header of my site's home page I see "HTTP/1.1 302 Moved Temporarily". But I wondwer if it will be problem for the google not to index regularly or pagerank problem?

Is there way to not to see this redirect for mysite?

My site : http://www.bizimoyunsitesi.com .

Thanks for the help.
Taner Ozdas
April 1, 2010
#50
Sorry for the above message , I have commented below lines from MY_Language.php then the header become normal with 200 .

function MY_Language() {
//header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);
//exit;
}
TANER OZDAS
April 1, 2010
#51
When i submit a form, I get the following error. I've also changed the file according to "webmasterdubai".

[quote]

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/MY_Config.php
Line Number: 21

--------------------------------------

A PHP Error was encountered
Severity: Warning
Message: in_array() [function.in-array]: Wrong datatype for second argument
Filename: libraries/MY_Config.php
Line Number: 21
--------------------------------------
Fatal error: Call to a member function localized() on a non-object in E:\wamp\www\mlc\application\libraries\MY_Config.php on line 22

[/quote]

What am i missing? I've also declared the special uri.

Thanks in advance
Nan V
April 1, 2010
#52
Hi, Jérôme!

Is there a way to get this 'country_code' => 'language' array from a database table? I a model for this, but I can't call it within MY_Language library. Do you have any suggestions for this problem?
Thanks in advance!
ci
April 4, 2010
#53
I think a rewrite of redirect is needed,

localized wont work in my case coz I have to replace a lot.

how about adding a My_uri_helper that add language when redirect.

I'm having Problems with ION AUTH.
John Smith
April 5, 2010
#54
Hey Jerome,
Is there a new version that I can download to use in my project.
Mugendi
April 5, 2010
#55
To choose the default language based on the browser language, from Aleix Ventayol
function default_lang()
{
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )
{
$languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );

// $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
// need to remove spaces from strings to avoid error
$languages = str_replace( ' ', '', $languages );
// we change ; to , to create an array where language codes doesn't have quality
$languages = str_replace( ';', ',', $languages );
$languages = explode( ",", $languages );


//find which language we should use
//the array will have some entries like q=0.5, but we don't care about
//them cause we're not going to find them in the languages array, so we don't
//need to waste time removing them
foreach ($languages as $lang)
{
if (isset($this->languages[$lang]))
return $lang;
}
}


foreach ($this->languages as $lang => $language)
{
return $lang;
}
}
Jérôme Jaglale
April 15, 2010
#56
Any help please...

When i submit a form, I get the following error. I've also changed the file according to "webmasterdubai".

[quote]

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/MY_Config.php
Line Number: 21

--------------------------------------

A PHP Error was encountered
Severity: Warning
Message: in_array() [function.in-array]: Wrong datatype for second argument
Filename: libraries/MY_Config.php
Line Number: 21
--------------------------------------
Fatal error: Call to a member function localized() on a non-object in E:\wamp\www\mlc\application\libraries\MY_Config.php on line 22

[/quote]

What am i missing? I've also declared the special uri.

Thanks in advance
Nan V
April 23, 2010
#57
For use any Lang in route.php

// example:
$route['^../(.+)$'] = "$1"; // For 2 chars
$route['^...../(.+)$'] = "$1"; // For 5 chars ex.: /pt_br/about
$route['^..$'] = $route['default_controller'];

www.qprocura.com.br
May 8, 2010
#58
Hi, Jérôme,

Thanks a lot for this helpful library. We recently have forked it to add the support of hostname based language detection. The fork URL: http://github.com/metastudio/codeigniter_i18n May be it will help somebody 8)
Andrey Chernih
May 19, 2010
#59
What is the best way to outputs translation from database?

I am planning to add a new table with id, language_id, reference_id (this will the id of original entry), reference_table(such as content, menu, etc), their reference_fields (it could be any fields from the original inputs), values, modified_date, modified_by and published.

I will appreciate any inputs. Thanks in advance.
i18n rocks
May 23, 2010
#60
Hi all,

First at all sorry for my english,

I'm using the Modular Separation library by wirezdesign and philsturgeon but this extension doesn't seem to work. I tested extending MY_Language from MX_Language and MY_Config from MX_Config.

What can i do?

I like so much this library, and i would like to use it.
xterico.
June 1, 2010
#61
I solved my problem. I tried to extend MX_Config and MX_Language with MY_Config and MY_language and it worked.

Thank you anyway.

June 3, 2010
#62
Thank you for your work on this plugin, it is much appreciated.
Carlton
June 11, 2010
#63
How could I use the array $lang in a controller ?, 'cuz when I put echo $this->lang->lang('example'); in the controller, la output is empty, and that should be "example", "Beispiel", "ejemplo", "exemplo" ...
Shaikiro Hoshamoto
June 16, 2010
#64
Just wanted to say thanks for a great library! Good job mate!
Erik Brännström
June 17, 2010
#65
Ohh, I know, sorry ...


$this->lang->line('key');

That's. So I can use the array's key ($lang) in the controller.


Thanks for this great library ...
Shaikiro Hoshamoto
June 17, 2010
#66
Constantin had fixed the problem with the function switch_uri(). I've modified this function to use a default language without letters, for example:


http://www.domain.com/controller/method -> Default language
http://www.domain.com/es/controller/method -> Spanish
http://www.domain.com/pt/controller/method -> Portugues


function switch_uri($lang)
{
$CI =& get_instance();
$uri = $CI->uri->uri_string();

if ($uri != "")
{
$exploded = explode('/', $uri);

// If we have an URI with a lang --&gt; es/controller/method
if($exploded[0] == $this->lang())
$exploded[0] = $lang;

// If we have an URI without lang --&gt; /controller/method
// "Default language"
else if (strlen($exploded[0]) != 2)
$exploded[0] = $lang . "/" . $exploded[0];

$uri = implode('/',$exploded);
}

return $uri;
}


Shaikiro Hoshamoto
June 17, 2010
#67
I added this to the MY_Language constructor (after the global variable decalarations):

$this->default_uri = $RTR->default_controller;
Mei
June 22, 2010
#68
Mei, and what does it do ? I don't know whether it helps something ...
Shaikiro Hoshamoto
June 25, 2010
#69
Thanks a lot, it worked perfectly. I'll try to use it with a database with English and spanish content, I'll let you know,
Juan Carlos
July 9, 2010
#70
Hi,

i just try to implement this cool feature to codeigniter 2.0.

unfortunately i can not get it work. any idea what i have to modify?

Ludwig
July 25, 2010
#71
Hey there, great one.
I don't really understand why if no language is set in the URI MY_Language() would fallback to the default controller instead of calling the appropriate controller with the default language.

Could be useful in case your website was already online before i18n was added.

If anyone should need this, here's a quick hack to prevent default controller

(line 62 of MY_Language.php )
change

header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);

to

header("Location: " . $CFG->site_url($this->localized($URI->uri_string)), TRUE, 302);

This way if somebody went to yoursite.com/news/read/70 this would basically redirect to yoursite.com/en/news/read/70

Hope makes sense...
Pierlo
August 11, 2010
#72
Hi.. all.. can i get the language from the session not from uri segment.. thanks.. cause on the MY_Language class the get_instance() function is not found..
steevenz
August 12, 2010
#73
???????? ??????!???????!
Vladimir, Russia
August 29, 2010
#74
Hello Jerome

Thank you very much for this very useful library!

I tried to extract form a database the languages for the array $languages from file MY_Language.php but it returns an error. Do you have any ideea?

Thank you!
Mihai
September 15, 2010
#75
Hi all,

at my site the first redirect to http//websites/website/en doesnt work. when add /en by hand everything works well. even the site_url() links. but at start when i add nothing, theres nothing happening...

can someone help me out?

regards, bart
Bart
September 17, 2010
#76

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

may I know what is en and fr on the link above.. Is it a folder inside the language folder?

please help me. I'm just new to CI..

Thanks for this wonderful library..
andoy
September 20, 2010
#77
now i get it..
im sorry for the noob question..

^^,

God bless!
andoy
September 21, 2010
#78
Hi,
Does anyone had a problem when gzip compression is active in codeigniter config? If I turn on the compression I can't switch to english version.. but in the default language (portuguese) all works fine. If i change the URL (mysite/pt/ to mysite/en/) browsers (FF, Chrome) return an error: Error 330 (net::ERR_CONTENT_DECODING_FAILED)... It may be related with the headers.. Did anyone had this problem before?
Cheers
Pedro Gil
September 23, 2010
#79
After some hours of debugging I realized that (for some unexplained reason) one of my English text files printed a strange symbol before <php start declaration. I just removed that symbol and all works as it should be!

Thanks anyway
Pedro Gil
September 23, 2010
#80
Hello Jerome,
Thanks for a wonderful library. However I am struggling with getting this working with modular separation provided by Phil.
I need some guidance on the changes that need to be made. The above posts weren't that clear.

thanks in advance

Shikhar
SHIKHAR SACHAN
September 28, 2010
#81
hi Jérôme,

nice work,
is there any idea to make it work with codeigniter 2.0 ????
yosvel
October 8, 2010
#82
and we can do something like that in routers.php

$route['^(ar|de|en|es|fr)/(.+)$'] = "$2";
$route['^(ar|de|en|es|fr)$'] = $route['default_controller'];

instead of

$route['^ar/(.+)$'] = "$1";
$route['^de/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
$route['^es/(.+)$'] = "$1";
$route['^fr/(.+)$'] = "$1";

$route['^ar$'] = $route['default_controller'];
$route['^de$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
$route['^es$'] = $route['default_controller'];
$route['^fr$'] = $route['default_controller'];
Khaled Attia
October 20, 2010
#83
I just test this in Codeigniter2+HMVC+Datamapper. To work you have to extends class from MX_ or MY_, copie the files to application/core and require the files on third_party. Then rename MY_Language to MY_Lang file and class. This only work if your controllers extends from MX_ or MY_. Tested with anchor and site functions and works like expected.
Miguel Ramos
October 28, 2010
#84
Hello, I'm having trouble when I'm trying to make a custom route.
Here's an example:

$route['pt/testimonials/(:any)']='testimonials/index/$1';

When I acess http://www.mywebsite.com/pt/testimonials/1
I get a 404 error.

Can anyone help me out?

Thanks. ^^
Camila Miguel
November 8, 2010
#85
I am having the same issue as Camila here.. None of my custom routes are working.. any help would be great.
OOB
November 8, 2010
#86
First use config like Khaled.

$route['^(ar|de|en|es|fr)/(.+)$'] = "$2";
$route['^(ar|de|en|es|fr)$'] = $route['default_controller'];

Second use :

$route['testimonials/(:any)']='testimonials/index/$1';

You don´t need to use pt or other language!
Camila are you portuguese?
Miguel Ramos
November 10, 2010
#87
To make it work with the latest CI 2.0 build use this:
class MY_Config extends CI_Config {

function site_url($uri = '')
{
if (is_array($uri))
{
$uri = implode('/', $uri);
}

// make it compatible with CI 2.0
if (class_exists('CI_Controller'))
{
$uri = get_instance()->lang->localized($uri);
}

return parent::site_url($uri);
}

}
n0xie
November 18, 2010
#88
I tried to add what Shaikiro Hoshamoto did, but I see no change hier are his codes again.. What could be a problem with my ci site? So I want to remove my deafult language in url like http://websiteplaats.nl/ fr/create_account to be websiteplaats.nl/ create_account
function switch_uri($lang)
{
$CI =& get_instance();
$uri = $CI->uri->uri_string();

if ($uri != "")
{
$exploded = explode('/', $uri);

// If we have an URI with a lang --&gt; es/controller/method
if($exploded[0] == $this->lang())
$exploded[0] = $lang;

// If we have an URI without lang --&gt; /controller/method
// "Default language"
else if (strlen($exploded[0]) != 2)
$exploded[0] = $lang . "/" . $exploded[0];

$uri = implode('/',$exploded);
}

return $uri;
}
Davor Radic
November 29, 2010
#89
Can somebody tell me, how to get the following to work:

If the user enters an invalid or not supported language in the URI, I want the request being processed with the default language, e.g.:

http://example.com/bad_lang/controller/function --&gt; http://example.com/default_lang/controller/function
ano
December 7, 2010
#90
Hi,

First, THANK YOU for this great library!
I'm quite new with framework and with MVC-model and I would like to implement the fuction described in #56 but I don't know where to add this function.
Can I have some explications about what to do ?

Regards,
Librius
December 20, 2010
#91
Hello, Miguel. Happy New Year! ^_^

I tried the way you showed, but I'm still having problems.
When I try to access http://www.mywebsite.com/pt/testimonials
I get the following error:

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Testimonials::index()
Filename: controllers/testimonials.php

And when I try http://www.mywebsite.com/pt/testimonials/1, I get a 404 error page.

I really don't know what to do with it.
And, by the way, I'm Brazilian.

Thanks! ^^
Camila Miguel
January 3, 2011
#92
How do I make this library to work with Codeigniter 2.0? It there a tutorial?
Edwin Mugendi
January 4, 2011
#93
Here you go http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n/
Hansu
January 8, 2011
#94
Coooooooooool its working................!
Ashish Chuadhary
January 14, 2011
#95
I had quit some trouble to get this one working on Codeigniter 2.0 in combination with HMVC extension. Also the modified version for 2.0 by Juris Malinens posted on http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n/ has the same bugs.

In short:
- By using site_url() in the constructer in combination with the localized method in site_url() causes a invenent loop.
- In the 2.1 post there was a bug introduced that caused double language code in the url: The last line of the localized method should return '$uri' instead of "return $this->lang() . '/' . $uri;"
- the constructor should use parent::__construct(); instead of parent::CI_Lang();
- the used explode function in this class gives different result depending on if a uri will start with or without a forward slash.
- if the "special" uri array stays empty it causes the constructor not to redirect where it is supposed to.

I was not able to modify the code posted on the previous url. So instead I posed the classes as an attachment in the first comment post:

http://codeigniter.com/forums/viewthread/179036/

You should be able to download it from there. Instructions are incleded in case you want to use it in combination with the HMVC extension.
Webwerken
January 24, 2011
#96
Interesting :) Your code doesn't work as well.

bueh
January 28, 2011
#97
@buah
Not sure if you commented on my post. In case you had trouble with it. There was a bug in it that caused the "special" uri array not to work. This should be fixed with the latest upload.
webwerken
February 1, 2011
#98
Hello n0xie,
Then man, I'm trying to use Internationalization (i18n) library for CodeIgniter 2.0, but when I load my application, it gives the following error: unable to load the class Language. Went to check in CI 2.0, I do not have the language class. Dude, can you help me?
Fabricio Rodrigues
February 1, 2011
#99
@Fabricio, the solution n0xie is suggesting doesn't not work. It just disables the whole function.
In this post on the forum where the i18n class for codeigniter 2.0 is discussed you will find the working files.

http://codeigniter.com/forums/viewthread/179036/

It would be nice if someone could upload them here also.

Good luck
webwerken
February 3, 2011
#100
hello.

Thank you for this this library. I 've integrate it with CI2 and hmvc. and it's working like a charm.


I only have 1 question.

Currently the structure i have is the following:

mysite.com/en/blog
mysite.com/admin
mysite.com/en/blog/admin

and i cannot find the route to have

mysite.com/en/blog
mysite.com/admin
mysite.com/admin/blog


would someone have an idea?


thank you
daweed
February 4, 2011
#101
webwerken, Thanks Dude!!
Fabricio Rodrigues
February 7, 2011
#102
This was a great tutorial ! I'm using an older version of CI, 1.7 and I'm happy with it.

Got a question:
I have a dynamic page, where the results come out of the database.
I want to split the results so that for each language only the related data is displayed.
It seems the query is not being passed to the lang-view page,

In my controller I have:

$data['query']=$this->db->query('SELECT * FROM albums ORDER BY releaseOrder DESC');
$this->lang->load('disco');
$this->template->load('template', 'disco',$data );

In my sys/lang/english/disco_view.php I have:

&lt;? foreach($query->result() as $row): ?&gt;
&lt;?=$row->myTitle;?&gt;
&lt;? endforeach?&gt;

But when I try to access $data I get this error:

Message: Undefined variable: query

This does work with one language....
Any ideas?

Thanks in advance

Kamy
March 15, 2011
#103
Dear Kamy,

I think you are trying to use one variable that it's not defined anywhere ($query). Maybe you have copy&paste; your code.

$query=$this->db->query('SELECT * ....');

Will define $query variable, while
$data['query']=$this->db->query('SELECT * ...);
not because the variable it's $data, not $query

Hope it helps

Alex
March 20, 2011
#104
Hey Jérôme Jaglale,
Is there a new version that I can download to use in my project.
YAXAR
April 7, 2011
#105
how can we pass multile array from language file to CI controller..
Mahendra Tyagi
April 16, 2011
#106
http://domain.com/CodeIgniter2/index.php/home/testTemplate/htht.html
Fatal error: Call to undefined method CI_Lang::switch_uri() ???
cuase i use index.php/ in my url how can i solve this problem ??!!
medo
April 21, 2011
#107
Thank you!
For CodeIgniter 2.x see
http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n/, sources attached to the first post
volodiak
May 5, 2011
#108
Hello, Could you explain to me how to insert cookie ?

Thanks
Kris
June 6, 2011
#109
Does anyone know how to grab for example under loaded english language french language string?
blacky
June 10, 2011
#110
sorry... but in my CI I don't find the folder:
system/application/libraries

i must to create the folder libraries?
henry
June 16, 2011
#111
how to do breadcrumb with ci?
sophal
August 8, 2011
#112
how to do multi sub category by ajax?
sophal ngang
August 8, 2011
#113
@henry: This reflects a change in CodeIgniter 2.x from 1.x. To make this library functional in 2.x, you will have to change the class names ("MY_Lang extends CI_Lang" in MY_Language.php and "MY_Config extends CI_Config" in MY_Config.php), that is, add "CI_" to the CodeIgniter class names and change the Lang class name, and change the name of MY_Language.php to MY_Lang.php. Then place these files in application/core/, instead of where this guy says to.
Dakota Schneider
August 20, 2011
#114
hello, I have ci 2.0 and translations are working but my custom routes not.

/* custom routes */
// URI like '/en/about' -> use controller 'about'
$route['^(en|de)/(.+)$'] = "$2";
// '/en' and '/de' URIs -> use default controller
$route['^(en|de)$'] = $route['default_controller'];

$route['register'] = 'auth/register';
$route['login'] = 'auth/login';
$route['logout'] = 'auth/logout';

'register', 'login' and 'logout' are not routed to auth/something. Any idea why? I am getting 404 error (when I open en/login it wants to use login controller instead of auth)


what's wrong with this?
Peter
August 21, 2011
#115
I found solution:
$route[’^(en|de)/register’] = ‘auth/register’;
$route[’^(en|de)/login’] = ‘auth/login’;
$route[’^(en|de)/logout’] = ‘auth/logout’;
$route[’^(en|de)/(.+)$’] = “$2”;
$route[’^(en|de)$’] = $route[‘default_controller’];

But I have one more trouble. When I try to login on en/login after form submition it redirects me at de/page instead of en/page. Why does it change language from en to de? thanks in advance
Peter
August 22, 2011
#116
I am having trouble by indexing pages gogole.
How to make default controller without redirekto to lang

September 2, 2011
#117
Thanks to webwerken! It works great. I've spent all night trying to figure why it's not working in CI 2.x. The solution was right in front of me :)
hRvojed
September 12, 2011
#118
thanks,,, its work... in CI 1.7.2
but, i want to make route with URI 'about/en' or like 'mywebsite.com/controller/class/en/id/' , how can i change route configuration?

and how can i want not display uri segmen language in my other controller link, like mywebsite.com/controller/class/en => mywebsite.com/controller/class
hendra
November 15, 2011
#119
thanks for taking the time to explain how the Special URIs really really helped me

<a href="http://eelsecreto.com/category/desarrollo-personal/">Desarrollo Personal</a>

http://eelsecreto.com/category/desarrollo-personal/
Desarrollo Personal
January 2, 2012
#120
Hi, Just added a small function to the MY_Lang library that checks if a language code is supported in the website. it gets the language code and return TRUE/FALSE

function is_valid_lang($langCode)
{
if(array_key_exists($langCode, $this->languages))
{
return TRUE;
}
else
{
return FALSE;
}
}
Sagee Lupin
January 30, 2012
#121