I18N For Attribue Labels


Yii offers some nice features to translate text to different languages using I18n.

Attribute labels are used heavily so it might be useful to define the translations in the model and use them by some kind of indexing instead of tranlating them.

What is your opinion or solution?

I translate my attributes in the attributeLabels() method. What do you mean by using them in some kind of indexing?

Widgets like DetailView or GridView are getting the labels using the attributeLabels() method. To return language depended labels you need to check

if (Yii::$app->language = ‘ge’) return array including German labels else return label with English labels.

This way you need to develope your own fallback system for other languages.

Indexing could be something like

return [

    'fields' => ['field1', 'field2', 'field3'],

    'en' => ['Label1', 'Label2', 'Label3'],

    'ge' => ['Text1', 'Text2', 'Text3']


where ‘fields’ represents the field list and ‘en’ or ‘ge’ represents the language with the corresponding labels.

First language could be the fallback language.

This way the widgets could detect the correct labels without any hassle for the programmer.

You can just use Yii::t and no such array configs. Message extraction tool will do the job for you.


I changed the attributeLabels() function in my model to translate labels like

‘abcfield’ => \Yii::t(‘app’, ‘Abcfield’),

which works like a charm.

I’m not sure if Yii stores the results somewhere in cache. If not, it might be a performance issue when all defined attributes are translated each time, regardless of how many of them are used in the view.

Or is it possible to pass an anonymous function which is executed only when needed?

From my experience performance hit is not significant.

Thank’s for your input which helps to set up a new project properly.

May I recommend some changes for GII and the documentation?

  1. GII should generate the attributesLabels() occordingly.

  2. The default path to the messages within the BasePath (like yii-advanced/frontend) is messages/LanguageID/CategoryName.php

and not messages/BasePath/LanguageID/CategoryName.php

  1. The language id is something like ‘de_DE’, not just ‘de’.

  2. Information about database structure when using DbMessageSource is missing.

  1. Translation is not required for lots of projects. Still we’ll consider it.

  2. ???

  3. It can be just de if you want.

  4. It’s right in DbMessageSource phpdoc.

I was also a bit confused at the beginning and got it mixed up with locale, but the language id is just the language part of locale. Locale contains language AND location (country). I was battling to set a locale as language id AND use the translations provided with yii at the same time, which uses just the language id.

That’s indeed a bit tricky. I understand and agree that for tranlations only the language part of the locale is required and should be used.

But Yii::$app->language and Yii::$app->sourceLanguage contains the locale, not just the language. To make it clear either the names should be changed or the type of content. What do you think?

Anyway - right now both returns (contains) the locale. Regarding to the documentation the default path to the translation files is the language id (that’s perfect). But yii uses the content of Yii::$app->language to detect the directory and not just the language part. Maybe this is a bug? If not the documentation should be enhanded to make it clear.

I use only the language id and haven’t run into any problems yet.

I did some additional tests and found the following:

  1. default language is set to: public $language = ‘en_US’;

  2. You can set the language to any string like: \Yii::$app->language = ‘fake language’;

  3. yii::t looks for a directory name which equals to the content of $app->language

Adopting the default content and settig the language attribute to something like ‘fr-CA’ (French for Canada) then the directory must be called ‘fr-CA’.

Assume to want to support ‘fr-FR’ (French for France) as well, then you need to have a second directory named ‘fr-FR’.

  1. This behavior is different to Yii version 1 where yii looked for a directory named ‘fr’.

I still believe that it should be clearly docuented what the Yii::$app->language should stand for (the language or locale).

It sholuld also made clear how this relates to the php class Locale which is used by the php intl extension to format dates and numbers.

Pls. help to clarify this issue.

plz tell me the language id for urdu language

just like for arabic ar is used…

plz reply me


when i change the source languge to ar its working but when i am using ur its not working .why any solution for this problem??