CakePHP:使Web应用程序国际化 [英] CakePHP: Internationalizing Web Application
问题描述
根据以下文章,我能够在一定程度上将我的CakePHP应用程序国际化 -
Based on the below article, I was able to internationalize my CakePHP application to certain extent -
http://puskin.in/blog/2010/08/cakephp-manage-multiple-language-in-application/
我要扩展此功能,并提供用户可以在其用户个人资料中保存首选语言的功能。每次用户登录时,我想使用首选语言,并以该语言显示网站的内容。 Ex - Facebook语言设置。一旦更改语言,网站中的静态内容就会更改为该语言。
I want to extend this functionality and provide the ability where user can save the preferred language in his User profile. And everytime the user logs in, I want to use the preferred language and display the content of the site in that language. Ex - Facebook language setting. As soon as you change the language, the static content in the website is changed to that language.
此外,我还有一些静态数据,例如查找信息,下来。
Also, I have some static data like look-up information for drop-downs. How should my table design be in order to support i18n.
任何代码片段或想法?
推荐答案
使用用户语言首选项设置很简单。只需向用户表中添加一个字段 language
,并允许用户在某处设置一个值。在您的应用程序中,您可以使用 Configure :: write('Config.language','eng')设置
core.php
code>:覆盖 AppController :: beforeFilter
:
Having a user language preference setting is trivial. Just add a field language
to the user table and allow the user somewhere to set a value to it. In your app, you set the default language in core.php
using Configure::write('Config.language', 'eng')
and override it someplace else, best in the AppController::beforeFilter
:
public function beforeFilter() {
if ($this->Auth->user('language')) {
Configure::write('Config.language', $this->Auth->user('language'));
}
}
现在Cake的所有L10n功能都将输出
Now all of Cake's L10n functionality will output content in the user's selected language.
如果您需要Cake没有涵盖的任何其他语言逻辑,您可能需要自己实现它。我建议您针对特定问题提出新的具体问题。通常,您将基于 Configure :: read('Config.language')
中的值定制任何自定义语言逻辑。如果您有具有多种语言描述的产品,您可以拥有产品
hasMany 描述
关系,其中每个描述具有您过滤的
语言
属性。
If you need any additional language logic that's not covered by Cake, you might have to implement it yourself. I'd suggest you open a new specific question about specific concerns you have. Generally, you will base any custom language logic on the value in Configure::read('Config.language')
. If you have, say, products with descriptions in multiple languages, you can have a Product
hasMany Description
relationship, where each Description
has a language
attribute which you filter by.
这篇关于CakePHP:使Web应用程序国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!