Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 translated to some other language? Or do I have to do something manually to work?

like image 212
Pentium10 Avatar asked Dec 09 '10 09:12

Pentium10


3 Answers

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

like image 125
Rodrigo Soares Avatar answered Nov 12 '22 07:11

Rodrigo Soares


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',
like image 31
gevik Avatar answered Nov 12 '22 05:11

gevik


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.

like image 1
davidfurber Avatar answered Nov 12 '22 06:11

davidfurber