CakePhp: How to know the current local in the model?
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_contro开发者_StackOverflowller), 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?
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:
App::import('component','Tools');
$tools = new ToolsComponent();
$locale = $tools->GetCurrentLocale();
If someone find a best way, I will be strongly interessted to see how :)
精彩评论