问题
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/
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.
Any code snippets or ideas?
回答1:
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'));
}
}
Now all of Cake's L10n functionality will output content in the user's selected language.
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.
来源:https://stackoverflow.com/questions/8774933/cakephp-internationalizing-web-application