Internationalization (i18n) library for CodeIgniter 2

What it does

Language in 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

Configuration

In application/config/routes.php add

// example: '/en/about' -> use controller 'about'
$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
 
// '/en' and '/fr' -> use default controller
$route['^fr$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];

Use

Let's build a bilingual English/French page.

language files

application/language/english/about_lang.php

<?php
 
$lang['about.gender'] = "I'm a man";
 
/* End of file */

application/language/french/about_lang.php

<?php
 
$lang['about.gender'] = "Je suis un homme";
 
/* End of file */

controller

application/controllers/about.php

<?php
class About extends CI_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 */

view

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 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(): add language segment (when appropriate) to generated URLs. Also used by 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 in just one language.

In application/core/MY_Lang.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 application/core/MY_Lang.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

  • application/core/MY_Lang.php: add new language to $languages array
// example: German (de)
'de' => 'german',
  • application/config/routes.php: add new routes
// example: German (de)
$route['^de/(.+)$'] = "$1"; 
$route['^de$'] = $route['default_controller'];
  • create corresponding language folder in 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
Apr 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
Apr 16, 2009
#2
BS, please remove the hook in config/hooks.php, it's not needed anymore.
Jérôme Jaglale
Apr 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
Apr 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
Apr 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!

Apr 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
Apr 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
Apr 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
Jul 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
Aug 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
Aug 15, 2009
#14
Amazing, you saved my life bud!!!
Jorch
Aug 20, 2009
#15
Just integrated with CI with HMVC, worked fine till now. :)
Mahbubur Rahman
Aug 24, 2009
#16
Very nice. It's possible to remove the language from the URI ?
Ed
Aug 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
Aug 25, 2009
#18
this looks very good... I will try it out merci beaucoup Jerome...
Luca
Aug 26, 2009
#19
Hi, just to mention that it worked like a charm... indeed saved me hours of work :-) thanks a lot
Luca
Aug 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
Aug 31, 2009
#21
great work man!!
Leandro Gilioli
Sep 18, 2009
#22
This is great! Really usefull!
demogar
Sep 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
Oct 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
Oct 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
Oct 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
Oct 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
Oct 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
Nov 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
Nov 3, 2009
#30
Cristian, you could use the second parameter:
$this->lang->load('about', 'fr');
Jérôme Jaglale
Nov 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
Dec 31, 2009
#32
Yes, your helper sure helps!! Thanks again for the great work. :)
chamila
Dec 31, 2009
#33
This is great !! Is this library working with HMVC library?
James
Jan 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
Jan 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
Jan 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
Jan 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
Jan 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
Jan 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
Feb 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
Feb 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
Feb 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
Feb 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
Feb 10, 2010
#44
* Thanks again for this really useful! :)
Constantin
Feb 10, 2010
#45
** Thanks again for this really useful feature!

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

you could use the uri class in your controller;
$data['curr_lang'] = $this->uri->segment(1);
remote
Mar 19, 2010
#48
thanks you
khacntt
Mar 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
Apr 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
Apr 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
Apr 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
Apr 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
Apr 5, 2010
#54
Hey Jerome,
Is there a new version that I can download to use in my project.
Mugendi
Apr 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
Apr 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
Apr 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.
Jun 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.

Jun 3, 2010
#62
Thank you for your work on this plugin, it is much appreciated.
Carlton
Jun 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
Jun 16, 2010
#64
Just wanted to say thanks for a great library! Good job mate!
Erik Brännström
Jun 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
Jun 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
Jun 17, 2010
#67
I added this to the MY_Language constructor (after the global variable decalarations):

$this->default_uri = $RTR->default_controller;
Mei
Jun 22, 2010
#68
Mei, and what does it do ? I don't know whether it helps something ...
Shaikiro Hoshamoto
Jun 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
Jul 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
Jul 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
Aug 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
Aug 12, 2010
#73
???????? ??????!???????!
Vladimir, Russia
Aug 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
Sep 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
Sep 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
Sep 20, 2010
#77
now i get it..
im sorry for the noob question..

^^,

God bless!
andoy
Sep 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
Sep 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
Sep 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
Sep 28, 2010
#81
hi Jérôme,

nice work,
is there any idea to make it work with codeigniter 2.0 ????
yosvel
Oct 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
Oct 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
Oct 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
Nov 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
Nov 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
Nov 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
Nov 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
Nov 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
Dec 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
Dec 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
Jan 3, 2011
#92
How do I make this library to work with Codeigniter 2.0? It there a tutorial?
Edwin Mugendi
Jan 4, 2011
#93
Here you go http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n/
Hansu
Jan 8, 2011
#94
Coooooooooool its working................!
Ashish Chuadhary
Jan 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
Jan 24, 2011
#96
Interesting :) Your code doesn't work as well.

bueh
Jan 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
Feb 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
Feb 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
Feb 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
Feb 4, 2011
#101
webwerken, Thanks Dude!!
Fabricio Rodrigues
Feb 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
Mar 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
Mar 20, 2011
#104
Hey Jérôme Jaglale,
Is there a new version that I can download to use in my project.
YAXAR
Apr 7, 2011
#105
how can we pass multile array from language file to CI controller..
Mahendra Tyagi
Apr 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
Apr 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
Jun 6, 2011
#109
Does anyone know how to grab for example under loaded english language french language string?
blacky
Jun 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
Jun 16, 2011
#111
how to do breadcrumb with ci?
sophal
Aug 8, 2011
#112
how to do multi sub category by ajax?
sophal ngang
Aug 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
Aug 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
Aug 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
Aug 22, 2011
#116
I am having trouble by indexing pages gogole.
How to make default controller without redirekto to lang

Sep 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
Sep 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
Nov 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
Jan 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
Jan 30, 2012
#121
how to redirect the page to domain.com/en if I land to the page first time. My default controller is 'site' so now my uri looks like after landing: domain.com/en/site
normally without i18n in this situation url is domain.com (even 'site' is the def. controller)
any idea? thanks
subd
Feb 26, 2012
#122
I'm getting problems with this lib.
My forms are not sending post data.
Jérôme Jaglale, do you have any public e-mail? I need some resolution. My problems started when i added this library. :-)
Mauricio de Abreu Antunes
Mar 7, 2012
#123
This March 29, 2012 update makes this library work with CodeIgniter 2.
Jérôme Jaglale
Mar 29, 2012
#124
this is rally a gud framework!!!!!!!!!11
amit singh
Mar 31, 2012
#125
I like this plugin - it does exactly what I need for my multilingual site. Except for one thing - the "Remove splash page" instructions don't work for me - if I remove ”” from the $special array I get a blank page every time.

I'm running the latest Codeigniter (2.1.0) - could this be the source of the problem. Anybody know any workarounds?

Mais, quoi, qu'il arrive, merci Jérôme pour un bout de code bien utile !
Tim Akeroyd
Apr 5, 2012
#126
Hello,
this good lib only work for me if i change the declaration of My_Lang default constructor:
from function MY_Lang()
to function __construct()

Is it right?
Thanks
Vincenzo
Apr 11, 2012
#127
My problem just like Mauricio de Abreu Antunes, (post data not exist when swicth to another language). sigerhost@gmail.com
sigerhost
Apr 19, 2012
#128
I'm having the same problem as Tim Akeroyd. If I comment out the empty string, I get the following error:

"Fatal error: Class 'CI_Controller' not found"
John
Apr 28, 2012
#129
Hello..

I have one problem:

If I try to access an URI where the first segment is not a valid language prefix (in my case 'pt-br', 'en' or 'es'), it returns an error. For example:

http://localhost/sitename/en/controller
Works fine

http://locahost/sitename/foo
Return "Fatal error: Class 'CI_Controller' not found"

How could I resolve this error?

Thanks,

Felipe
Felipe Ribeiro
Apr 30, 2012
#130
Hello!

I also encountered "Fatal error: Class 'CI_controller' .. " problem but I realised I've seen the error before.
After googling a little it got to me that get_instance() in MY_Config gets called before the Controller class is loaded. When I understood the problem I knew what to look for and here is the fix I found - http://codeigniter.com/forums/viewreply/825774/

Hope it helps,
Matiss
Matiss Bisofs
May 2, 2012
#131
Hello..
Now its working!!
Thanks a lot Matiss
Felipe Ribeiro
May 5, 2012
#132
Great library. I'm using this for my new project. Thank you for putting it up for us.

Merci.
Luis Milanese
May 7, 2012
#133
Salut,

Tu m'as l'air français alors, j'vais te remercier dans notre langue pour te dire un gros merci pour cette librairie.

Testé et approuvé. Par rapport à d'autres librairies d'internationalisation, tu utilises les fonctionnalités du core de CI, t'évites de ré-écrire la roue : c'est cool.

Ta doc est claire et nette. Pas une seconde de galère.

Nickel...

And if you are not French, I just say thank you for your good work.
Raphaël
May 9, 2012
#134
This May 10, 2012 update fixes the "Fatal error: Class 'CI_Controller' not found" problem reported above and solved by n0xie.
Jérôme Jaglale
May 10, 2012
#135
Hello, I found problem with special uri, form and csfr protection enabled. form helper doesnt generate csfr token, because its compare action uri with site_url(). site_url return localized uri, but form is on special not localized uri. this is solution:

// add language segment to $uri (if appropriate)
function localized($uri)
{
global $URI;

if($uri == '') $uri = $URI->uri_string();

if($this->has_language($uri)
|| $this->is_special($uri)
|| preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
{
// we don't need a language segment because:
// - there's already one or
// - it's a special uri (set in $special) or
// - that's a link to a file
}
else
{
$uri = $this->lang() . '/' . $uri;
}

return $uri;
}
Kristian Feldsam
May 17, 2012
#136
update

if($uri == '' && $this->is_special($URI->uri_string())) return $uri;
Kristian Feldsam
May 17, 2012
#137
Hi everybody,

I have seen that some people has been asking about the possibility to translate URI segments and not just change the first segment.
Since I needed it as well I created a hack around it and thought that I should share it.

Here is what you can do:

1. In MY_Lang.php, add these variable and function:

// Define segments that are to be translated

public $seg1 = array(
'en' => array('home', 'about'),
'fr' => array('acceuil', 'a-propos'),
'de' => array('heim', 'ueber-uns')
);

// translate() creates a dictionary with array_combine() and translates with strtr()

function translate($what,$from,$to)
{
$what = strtr($what,array_combine($this->seg1[$from],$this->seg1[$to]));
return $what;
}

2. Inside the switch_uri function, add:

$uri = $this->translate($uri,$this->lang(),$lang);


just before

}
else
{

Now you should be good to go. One more thing, when configuring your routing, everywhere you would have put 'about' or 'home', you can put:

(about|a-propos|ueber-uns)

This hack works perfectly on my website, if it doesn't work for you I must have made a typo mistake recopying it. Please give it a look and post it here for the others!

Cheers
Joan Rodriguez
May 21, 2012
#138
Very usefull and so simple as usual with CI.
Thks
Babel
May 24, 2012
#139
Hi Guys,

I am building a website with an admin page. I am using this plugin to build the multilingual admin page. I am using HMVC also.

Is it possible to have the "http://localhost/mydomain/admin/login" a special page ( no language indicator in the url ) but when you are inside the admin page ( http://localhost/mydomain/en/admin/dashboard ) you will then have the language indicator in the url?


Thanks,
Dolpox
Dolpox
Jun 21, 2012
#140
I have the problem like someone above has, when I land on my page, e.g. I type 'example.com' in my browser it shows:

example.com/en/home

, where 'home' is my default controller.

But, normally (without using i18n library) it would be without the default controller like:

example.com/en/

Tahnks in advance for any advice
klokyioka
Jul 15, 2012
#141
Bonjour,

Comment faire pour que la langue par défaut soit à la racine / sans indication de langue, et garder une seconde langue avec accès classique par /en par exemple?

Merci de votre aide.
Ludo
Aug 2, 2012
#142
Hi ppl,

I am trying to convert a portuguese website in to english language using steps mentioned in http://maestric.com/doc/php/codeigniter_i18n .. I followed all the steps correctly but in the TEST step when go to the link http://localhost/en/about ... the output is correct but when I try to go to the link http://localhost/pt/about, the page gets redirected to http://localhost/en/home automatically and the display on the screen comes :


404 Page Not Found

The page you requested was not found.



Please help
Thanks

Sep 27, 2012
#143
Same problem as #143, please help
Azriel
Oct 1, 2012
#144
Hi,

The redirect issue:
====================

Regarding your previous comment related to dx_auth, I just wanna talk about a similar authentication code which is tank auth.

In order to make tank auth working with your i18n library, you have to:

1- remove the "" in MY_Lang:
// special URIs (not localized)
var $special = array (
);
2- strip out any '/' from the redirect/anchor ... functions in the tank auth controller and the view pages:
Ex:
anchor('auth/forgot_password/'
redirect('auth ...
jcargoo
Oct 1, 2012
#145
Hi,

I'm using Codeigniter_i18n, when get current language:
$this->lang->load('default');
echo $this->lang->lang();

Output error:
Fatal error: Call to undefined method MX_Lang::lang() in .... echo $this->lang->lang();

Please to fix! Thanks
Steve Toan
Oct 1, 2012
#146
if you are used modules HMVC the class MY_Lang must extends from MX_Lang, don't from CI_Lang
Carlos
Oct 11, 2012
#147
it got error at about.php with message "Fatal error: Call to undefined method CI_Lang::localized() in <b>C:\xampp\htdocs\codeigniter\application\core\MY_Config.php</b> on line <b>19</b><br />"
tuanbv
Oct 11, 2012
#148
Hi,

site_url() doesn't add language segment to the generated URL. Please help.

Thanks,
Nao
Oct 13, 2012
#149
Good presentation,

Thank you
Rachana
Oct 24, 2012
#150
thanx. you da man!
Catalin Buletin
Oct 24, 2012
#151
Hi !

Thanks for this, it works great.

I only wanted to share the fix I did to it to avoid a infinite redirect loop when you have a 404_overide like this:

$route['404_override'] = 'erreur/page_non_trouvee';

By default, if I don't do this, I get infinite loop if I type URL "/anything_with_no_language_in_front"


In MY_Lang Constructor:


Below "$segment = $URI->segment(1);" add:
$rsegment = $URI->rsegment(1);

Then use it in the if:

else if($this->is_special($rsegment) || $this->is_special($segment)) // special URI -> no redirect
{
return;
}


Finally, add the controller in the $special array:

// special URIs (not localized)
var $special = array (
'erreur'
);

Thanks!
Tommy Lebel
Nov 1, 2012
#152
how to make current_url() return the url after localization code?
in my case, when I call current_url() it returns me

en/path/to/string/

what I want is

path/to/string/

how to hack the current_url() function to make it works like I want?

thanks.
lisa
Nov 27, 2012
#153
When uri_string is empty we can't change language.
Any one solve this problem?
Hamed
Dec 3, 2012
#154
How I must to change routes for working that:

http://mysite.ru/test/hello => http://mysite.ru/{default_language or language i will set}/test/hello ?
santaklauz
Dec 4, 2012
#155
I got this to work with one controller and one view but when I add a new controller and view the link takes me always to my first page.
I have a test_controller.php and a test_view.php.
I have added in the routes.php the following according to the instructions:
$route['default_controller'] = test_controller';
$route['^el/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
$route['^el$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
In My_Lang.php I have deleted the empty quotation marks in the Special arrays (not localized) section (no splash screen).
So far the site works OK and I can switch between two languages.
The address shows example.com/en and it displays my test_controller.php etc content.
Then I tried to add a new controller and view: new_controller.php and new_view.php
But when I click on the link to take me to example.com/en/new_controller I am directed to example.com/en.
When I hover over the link I can see the link displayed at the bottom of the browser (Firefox) example.com/new_controller but I am taken to example.com/en.
I suppose I must add sth to the routes to make it work.
I tried $route[‘new_controller’] = ‘new_controller’; but didn’t work. The only way to load my new controller is to set it as default controller in the routes.php file instead of the test_controller.
I am missing something very basic I think but I can’t think of anything. Please, can anyone tell me how to add a new controller when this i18n is added?

phaethon
Dec 18, 2012
#156
Update: When I add manualy in the url
example.com/en/new_controller
then my new page loads and everything works well from then on. I can change languages and go to the test_controller etc. But this en is not being added when I click the link.
phaethon
Dec 18, 2012
#157
Update2: Solved, I forgot to set the $default_uri. It should be $default_uri = 'test_controller'
It was sth stupid. Now everything works except that some elements of my page disappeared! When I set $default_uri='' the elements appear again!
phaethon
Dec 18, 2012
#158
I added in My_Lang.php

function load()...

/*
* Disabled: Otherwise $this->lang->load('general', 'other_language_key'); doesn't work
if (in_array($langfile, $this->is_loaded, TRUE))
{
return;
}
*/
Dominik
Dec 19, 2012
#159
Everything's ok, but I'd like to take the languages list from database. That $language class variable would load languages from a sql table. But seems like I can't make any db query in MY_Lang constructor. I tried making another method and load it in a controller but it's not the same... Anyone has a good idea how to implement multi-language with db support?
Danielsen
Dec 31, 2012
#160
How Can i create inner pages for that current language??
Sharan
Jan 2, 2013
#161
Hi there, for the example I have http://mysite.com as the default site use en language, and I want at index of http://mysite.com change to http://mysite.com/nl/?

thank you in advance
ranggadablues
Feb 6, 2013
#162
This website design style is very good!
Liuping
Mar 27, 2013
#163
Hi, I having problems with route.

$route['default_controller'] = "cc_main/showLogin";
$route['^(es|en)/(.+)$'] = "$1";
$route['^(es|en)$'] = $route['default_controller'];

$route['welcome'] = "cc_main/welcome";
$route['^(es|en)/(.+)$'] = "$2";
$route['^(es|en)$'] = $route['welcome'];

in my view I have:

<div><a href="&lt;?php echo site_url('welcome'); ?&gt;">Welcome</a></div>
when i do click on Welcome this does work, I am getting 404 Not Found. The requested URL /app_template/es/welcome was not found on this server.

I tried $route[’^(es|en)/welcome’] = ‘cc_main/welcome’; and not working.

how can i solve this problem?
Luckys
Mar 30, 2013
#164
I like and use your library, it works fine.
There is a little bug, within
function MY_Lang()
....
it doesn't return default language. When changed it returns the same value.
$new_url = $CFG->config['base_url'].'au'.$uri;
instead of
$new_url = $CFG->config['base_url'].$this->default_lang().$uri;

Regards,
Frank
May 22, 2013
#165
Hello,
i am using this awesome library but i have a problem.
I want to add my sitemap like this in the routes

$route['sitemap\.xml'] = "sitemap"; // not working
$route['sitemap\.xml'] = "sitemap/index"; // not working
$route['^(de|gr|en)/sitemap\.xml'] = "sitemap"; //this working

but i do not want a language on this url.
I want the first one to get work.
Any ideas ?
allos
Jul 22, 2013
#166
In .htaccess, add a rule to redirect /sitemap.xml to /sitemap or /en/sitemap
Jerome Jaglale
Jul 22, 2013
#167
Still doesn't work (here is my .htaccess with the new line commented)

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|favicon\.ico|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ /index.php/$1 [L]
# RewriteRule ^.*sitemap\.xml$ /sitemap [L]
php_flag display_errors on
php_value error_reporting 7
allos
Jul 22, 2013
#168
Sorry, I couldn't get the .htaccess to work but this works:

In routes.php
$route['sitemap.xml'] = 'sitemap';
In MY_Lang.php, add it to $special:
// special URIs (not localized)
var $special = array (
 "",
 "sitemap.xml"
);
(Basically the i18n library needs to know it's acceptable for this URI not to contain a language segment)
Jerome Jaglale
Jul 22, 2013
#169
that works great :D thanks a lot
allos
Jul 22, 2013
#170
great work thnx...:)
aasfs
Oct 9, 2013
#171
Thank you very much! Nice tuto.
Mafi
Oct 9, 2013
#172
I have similar problem like in #85.

Here's an example:

$route['news/(:any)']='news/show/$1';

When I access http://www.mywebsite.com/en/news/title-new-1
I get a 404 error.


I put it in

http://stackoverflow.com/questions/19376539/codeigniter-internationalization-i18n-404-page-not-found
Ing. Michal Hudak
Oct 15, 2013
#173
Just for reference, translating an anchor works like this:
&lt;?=anchor('link',lang('link_var'))?&gt;
James
Oct 20, 2013
#174
I have been using the i18n library, which works great for my project.

But since I need to write separate modules, I recently added CodeIgniter Modular Extensions ( https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc ) and the navigation breaks.

How can I solve this issue please, I would love to use both the i18n library & Modular Extensions.

I think my application navigation fails to work because i18n library introduces adds a language segment in the site url, in my case my url is http://localhost/index.php/en/home and after adding Modular Extensions, my navigation/links stop working.

Thank you in advance.
Themba
Nov 26, 2013
#175
hi please help urgent

I am worked on wamp server it is working fine.

I moved production that is linux ubuntu 404 page error comes.

Please help me asap.

Route :

$route['default_controller'] = "Main";
$route['404_override'] = '';
// example: '/en/about' -> use controller 'about'
$route['^te/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";

// '/en' and '/te' -> use default controller
$route['^te$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
karthik7452@gmail.com
Dec 30, 2013
#176
Great Work! Thank You for sharing.
Amaury
Feb 14, 2014
#177
This library has some security issue. Please check this.
http://www.slideshare.net/abiusx1/codeigniter-i18n-security-flaw
webgrity67@gmail.com
Feb 26, 2014
#178
Hi!
how i can do this:
http://www.domain.com/controller/method -> Default language (en)
http://www.domain.com/es/controller/method -> Spanish

not show default language /en/ in the url
Juan Nina
Mar 6, 2014
#179
Edit for auto check browser language
#Open MY_Lang.php
#Change default_lang() function this code
#Edit for your website languages

function default_lang()
{
$Auto_Check_Language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if($Auto_Check_Language = 'tr') {
return 'tr';
} elseif($Auto_Check_Language = 'en') {
return 'en';
} elseif($Auto_Check_Language = 'de') {
return 'de';
} elseif($Auto_Check_Language = 'ru') {
return 'ru';
}
}
Yusuf Ulusoy, Turkey(?stanbul)
Mar 18, 2014
#180
I saw you can internationalize CodeIgniter with https://poeditor.com/ with this library https://github.com/joelvandal/codeigniter-gettext-poeditor
Valerie
Apr 9, 2014
#181
Hi please help urgent !

how i can do this:
http://www.domain.com/controller/method -> Default language (en)
http://www.domain.com/es/controller/method -> Spanish

not show default language /en/ in the url

Thank you
Leonard
Apr 11, 2014
#182
Hi,I implemented your solution Jerome, the translation worked just fine. I have three pages, when I navigate to these pages with the language code on the address bar(localhost/master/en/home,localhost/master/fr/about,localhost/master/ar/contact), everything works just fine, but when I use the normal link created in my view page(e.g:<a href="&lt;?php echo base_url(); ?&gt;">Home</a>), it returns me to the default language(en) even when I have selected another language. Please is there any sample code to solve this?
Davis
May 20, 2014
#183
Davis, use site_url('home') instead of base_url()
Jérôme Jaglale
May 21, 2014
#184
Hi Jerome,
first thanks for the plugin, it saved me hours :)
is there a way to access the set languages from the url somehow with php?
I tried:
&lt;?php echo($this->lang->lang()); ?&gt;
but this always returns the language which is set in cms (pyro) for the whole website.
thx
Patrik
Jun 12, 2014
#185
Halo..
I implement MY_lang and MY_Config.. on 2.1.4, but i got error..
Call to undefined method CI_Lang::localized, how to fixit
thx
Rusmana
Jun 13, 2014
#186
Fuck that shit :D after 4 hours I got the any num thing....

Take routes with any num etc over the general routes and language routes ....
Just segement renames can below the stuff ....

I hope its will help someone, cause I didnt find a direct answer


$route['^(en|de)/products'] = "products/search"; // search/error page for me
$route['^(en|de)/products/(:num)'] = "products/id/$2"; // products/123
$route['^(en|de)/products/(:num)/(:any)'] = "products/id/$2"; //Fix to have after id/ names (products/123/t-e-a)

$route['default_controller'] = "index";
$route['404_override'] = '';

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

$route['^(en|de)/a-b-c'] = "abc";
$route['^(en|de)/news-blog'] = "newsblog";
Sanjo Elstak
Jun 21, 2014
#187
Hello,
I am using codeigniter-boilerplate, I have put MY_Lang.php and MY_Config.php in application/core and then I have configured the routes.
When I am doing site.com/en/page the page is showing up but the langage is not changing, it is still the one of "$config['language'] = 'french';".
Any idea ? What I am missing ?
Thank you.
Toto
Aug 21, 2014
#188
It works like charm !!!
Thankx a-lot author...
you made my day ....
Neil
Sep 9, 2014
#189
Hello,

hanks a lo, i is exactly what I was looking for!!

Just a question: is this possible to to set up package? Like &lt;?= lang("module.input.name"), it will look through currentlang/module/input.php

Thank you
Mims
Sep 12, 2014
#190
After upgrading to Codeigniter 3.0.0, i found that whichever form i submitted, the $_POST was always empty in the controller. I tracked the issue down to this library which i've used for many years. It seems that when adding the language to the url, the post data gets lost. I'm not sure what happened between 2.x and 3.0.0 to make this happen. Otherwise the library works fine.

Any ideas on what i could edit to keep the post data intact on 3.0.0?

Many thanks!
Andy
Jul 19, 2015
#191
I think problem is in .htaccess file
Anatoly
Aug 17, 2015
#192
Andy, I was facing the same issue, then I went and changed the status code in MY_Lang file to be 200 instead of 302
header("Location: " . $new_url, TRUE, 302);
This worked for me
Hunter
Nov 11, 2015
#193
Instead of 302, try 307 like this:
header("Location: " . $new_url, TRUE, 307);

Source: http://stackoverflow.com/a/21821236/1650966
Rado K
Feb 27, 2016
#194
I was wondering, when someone switch language, I would like to save the new language into a session var, so when the language is not specified in the URI, I would like to take the saved language and insert it in the URI instead of using the default language, can you help me with that ?
JF
Sep 15, 2016
#195
i was use your library and i try to route this url but failed
$route["home"] = 'home_page';

any special rule for route if my project use this library?
Rahat
Mar 25, 2017
#196
Hi! I just wanted to say that for localization projects, you can try a software localization tool and my advice is to evaluate https://poeditor.com
Xan J.
Mar 27, 2017
#197
here is an updated version of this library https://github.com/waqleh/codeigniter-language-in-url-internationalization
waqleh
Jul 4, 2017
#198
good
goodf
Jul 13, 2017
#199
I think it is possible to create a multi language international website using CodeIgniter's own library class, instead of il8n. Like done here: https://www.cloudways.com/blog/multi-language-codeigniter/ The process is not that difficult if you know how to use hooks.
olidev
Feb 20, 2018
#200