简体   繁体   English

CakePhp:如何知道模型中的当前局部?

[英]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, ... 该网站使用的是ERP数据库,因此我无法选择数据库结构,...

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 我的应用程序在core.php中具有默认语言,并且我正在使用I18N通过URL来指定要显示的语言

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. 因此,我需要在构造函数中将cakePhp的“ $ useTable”设置为正确的表。 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" 在I18n类上执行getInstance,但我始终将其“ english”为“ lang”
  • doing a Configure::read('Config.language'), but this only displays me the default language of the cakePhp 做一个Configure :: read('Config.language'),但这只会向我显示cakePhp的默认语言
  • I tried to read the $GLOBALS['Dispatcher']->$params['language'] but if the user didn't specify any local, I don't get any 我试图读取$ GLOBALS ['Dispatcher']-> $ params ['language'],但如果用户未指定任何本地语言,则不会得到任何信息
  • 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 我试图导入Component Session并读取Config.language值(我在app_controller上写),但是似乎如果我是法语页面,则单击链接以显示德语页面(所有文本现在都是德语),但是我在这里收到的值仍然是法语,如果刷新页面,我最终会得到德语,但是我需要立即获取值,而不是在下一页重新加载

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. 我没有找到任何简单的方法来检索此var,并且由于在_beforeFilter之前调用了我们,因此我必须检查参数,会话,cookie和默认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 :) 如果有人找到最好的方法,我将强烈地关注如何做:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM