CakePHP TranslateBehavior is driving me crazy
I use CakePHP 1.3.2 with the integrated Translate Behavior.
The translations I save my several models in each table. Example:
class Page extends AppModel {
var $name = 'Page';
var $actsAs = array(
'Translate' => array('title', 'subtitle', 'menu')
);
var $translateModel = 'PageI18n';
...
}
Now after inserting of some data rows. Cake doesn't retrieve the i18n-data in the index-action anymore. The SQL-dump looks like this:
... WHERE I18n__title.locale = 'de_de' ...
But in the table "page_i18ns" the locale field is filled with 'deu'
Why is Cake mixing the locale attribute? 开发者_开发技巧Where should I set the locale attribute? Somewhere in Model class?
In my AppController I set the language with Configure::write('Config.language', $lang);
in the beforeFilter()
function .... 'eng', 'deu', 'chi'
I have the following code in my ArticlesController:
function beforeFilter() {
parent::beforeFilter();
$this->Article->locale = $this->Session->read('Config.language');
}
See if that helps.
It's indeed annoying CakePHP uses the locale instead of the language in the i18n table.
They might fix this bug, but chances are slim, because fixing this bug would break all existing i18n applications.
So i wrote a little helper function that translates a languageCode into a locale:
/**
* Retrieve the "locale" from the language code (for i18n table)
* Examples: en -> eng, en-us -> en_us, de -> deu, nl -> dut
*
* @param string $language
* @return string
*/
function convertLanguageToLocale($language) {
$i18n =& I18n::getInstance();
$i18n->l10n->get($language);
return $i18n->l10n->locale;
}
精彩评论