开发者

How to create a multilanguage label in Yii

I am wondering if the Yii framework uses the defined Labels atttributes in a multilanguage process.

So if I have

 public function attributeLabels() {
        return array(
            'email' => 'Email address',
            'rememberMe' => 'Remember me next time',
            'password' => 'Password'
        );
    }

Will this be transl开发者_JAVA百科ated to some other language? Or do I have to do something manually to work?


Yii doesn't translate it automatically. You need to use the i18n built-in in Yii and manually add the translations and modify the labels as follow:

 public function attributeLabels() {
    return array(
        'email' => Yii::t('account','Email address'),
        'rememberMe' => Yii::t('account','Remember me next time'),
        'password' => Yii::t('account','Password')
    );
}

You can get more info about internationalize you app at Quick Start to Internationalize your application in Yii Framework


Well, you can use the built-in translation system to translate your attribute labels, for example:

 public function attributeLabels() {
    return array(
        'email' => Yii::t('myapp','Email address'),
    );
}

and then in messages folder create a directory for your language, for example:

messages\dk\myapp.php

myapp.php should return the translation, for example:

return array('Email address' => 'TRANSLATION...');

Next you need to set the language of your application in the config file for instance.

'language' => 'dk',


I had assumed that Yii AR would run getAttributeLabel through Yii::t. Not wanting to do all that copy and pasting on dozens of models, I added this function to my intermediate AR class:

public function getAttributeLabel($attribute)
{
    $baseLabel = parent::getAttributeLabel($attribute);
    return Yii::t(get_called_class(), $baseLabel);
}

Now to write a shell command that loops through the models and adds their labels to the message file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜