CakePhp:如何知道模型中当前的本地?

发布于 2024-12-06 23:49:18 字数 881 浏览 1 评论 0原文

我必须开发一个多语言的小型网站。该网站使用 ERP 数据库,因此我无法选择数据库结构,...

我的应用程序在 core.php 中有默认语言,并且我正在使用 I18N Through URL 指定要显示的语言

对于每种不同的语言,我有一个不同的表,其中包含正确语言的产品描述。

所以我需要在构造函数中将cakePhp的“$useTable”设置为正确的表。但要做到这一点,我需要知道应用程序的当前区域设置。

我尝试了几件事:

  • 在 I18n 类上执行 getInstance ,但我总是将“english”作为“lang”
  • 执行Configure::read('Config.language'),但这只显示 cakePhp 的默认语言
  • 我尝试读取 $GLOBALS['Dispatcher']->$params['language'] 但如果用户没有指定任何本地语言,我不会得到任何
  • 我尝试导入组件会话并读取Config.language 值(我在 app_controller 上写的),但我似乎如果我在法语页面上,我单击链接以德语显示页面(现在所有文本都是德语),但是我在这里收到的值仍然是法语,如果我刷新页面,我终于得到了德语,但我现在需要获取该值,而不是在下一页重新加载

所以我不知道如何检索这种语言,有没有包含此的隐藏字段var,或者我可以检索此信息的任何地方?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-12-13 23:49:18

我没有找到任何简单的方法来检索这个变量,并且因为我们在 _beforeFilter 之前被调用,所以我必须检查参数、会话、cookies 和默认变量。

所以我创建了一个方法,将其放入“工具”组件中,然后将其导入模型中。

如果您感兴趣,这里是模型:

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');      
}

我这样称呼它:

    App::import('component','Tools');
    $tools = new ToolsComponent();
    $locale = $tools->GetCurrentLocale();

如果有人找到最好的方法,我将非常有兴趣看看如何:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文