I18N For Attribue Labels How to translate attribute Labels
Posted 30 October 2013 - 10:17 AM
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?
Posted 30 October 2013 - 01:12 PM
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
'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.
Posted 31 October 2013 - 09:51 AM
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?
Posted 31 October 2013 - 11:00 AM
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
3) The language id is something like 'de_DE', not just 'de'.
4) Information about database structure when using DbMessageSource is missing.
Posted 31 October 2013 - 01:57 PM
3) It can be just de if you want.
4) It's right in DbMessageSource phpdoc.
Posted 31 October 2013 - 02:07 PM
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.
Posted 01 November 2013 - 07:32 AM
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.
Posted 02 November 2013 - 03:47 AM
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'.
4) 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.