Take the 2-minute tour ×

I've to develop a small website which is multi-lingual. This website is using an ERP database, so I cannot choose my database structure, ...

My application has the default language in the core.php, and I'm using I18N Through URL to specify which language I've to display

For every different language, I've a different table containing the description of products in the right language.

So I need to set the "$useTable" of cakePhp in the constructor to the right table. But to do this, I need to know the current locale of the application.

I tried several things:

  • do a getInstance on the I18n class, but I've always "english" as "lang"
  • doing a Configure::read('Config.language'), but this only displays me the default language of the cakePhp
  • I tried to read the $GLOBALS['Dispatcher']->$params['language'] but if the user didn't specify any local, I don't get any
  • I tried to import the Component Session and read the Config.language value(which I'm writing on the app_controller), but I seems that If I'm on a french page, I click on the link to display the page in german(all texts are in german now), but the value I receive here is still in french, if I refresh the page, I finally got German, but I need to get the value now, not on the next page reload

So I don't see how to retrieve this language, is there an hidden field containing this var, or anywhere I could retrieve this info?

share|improve this question
Could you add your code of your AppController, and an example controller you're working with? –  Ivo Sep 27 '11 at 9:14
My controller is exactly the same than specified in my link. And I need to get the current locale in the model, not in the controller –  J4N Sep 27 '11 at 9:38
You can specify the table in the controller: $this->$model->useTable = 'rus';, etc. Surely your controller AppController can't be exactly the same - you are writing a custom application, right? –  Ivo Sep 27 '11 at 10:45
add comment

1 Answer

up vote 0 down vote accepted

I didn't found any easy way to retrieve this var, and because we are called before the _beforeFilter, I've to check parameter, session, cookies and default var.

So I created a method, I put it in my "Tools" component, and I import it in my model.

Here is the model if you're interessted:

function GetCurrentLocale(){
    if(isset($GLOBALS['Dispatcher']->params['language'])) return $GLOBALS['Dispatcher']->params['language'];
    if($this->Session->check('Config.language')) return $this->Session->read('Config.language');
    if($this->Cookie->read('lang')!==false)return $this->Cookie->read('lang');
    return Configure::read('Config.language');      

And I'm calling it like this:

    $tools = new ToolsComponent();
    $locale = $tools->GetCurrentLocale();

If someone find a best way, I will be strongly interessted to see how :)

share|improve this answer
Behaviours are best to add to models: book.cakephp.org/view/1071/Behaviors –  Ivo Sep 27 '11 at 10:46
add comment

Your Answer


