The giix code generator has out of the box i18n support for model names and attribute labels (besides other cool features > check it out!) .
But I want to factor in not only the plural form, but also the grammatical cases (especially the accusative) while translating 'Manage Objects', 'Create Object', 'Edit Object' etc.
PROBLEM DESCRIPTION ¶
Let's say I want to translate 'Manage Objects' into the turkish language.
Currently, in the automatically generated code in views/object/admin.php, it is done like this:
echo Yii::t('app', 'Manage') . ' ' . $model->label(2);
Since Object translates to 'Obje', and Objects translates to 'Objeler' in turkish,
it would look like: "Yönet Objeler".
But I want it to look like: "Objeleri Yönet"
That is, I want to
- change the order of the words as I like and
- use the correct accusative case of the word, so it makes sense in the translated language.
In english, incidentally the plural form is equal to the accusative case for most (if not all?) words. That's not always true in other languages.
Moreover, all four cases (nominative, genitive, dative and accusative) of a word are often equal in english, so I couldn't differenciate between them in my message files. After some trial and error I came up with a...
SOLUTION ¶
a. Define constants in the file \extensions\giix-components\GxActiveRecord.php
const NOMINATIVE = 1;
const GENITIVE = 2;
const DATIV = 3;
const ACCUSATIVE = 4;
b. Add the following function definition into your model file models\Object.php
:
public static function label($n = 1, $case=self::NOMINATIVE) {
if($n == 1)
return Yii::t('app', 'Object', $case);
else if($n > 1)
return Yii::t('app', 'Objects', $case);
}
c. Add the translations to your message file (e.g. in this case messages\tr.php
):
'Manage {model_entries}'=>'{model_entries} Yönet',
'Object'=>'1#Obje|2#Objenin|3#Objeye|4#Objeyi', 'Objects'=>'1#Objeler|2#Objelerin|3#Objelere|4#Objeleri',
d. Now let's use this in the view files, and take care of the order of the words:
In the view files, e.g. in views/object/admin.php
, replace
echo Yii::t('app', 'Manage') . ' ' . $model->label(2);
with
echo Yii::t('app', 'Manage {model_entries}', array('{model_entries}'=>$model->label(2,$model::ACCUSATIVE)));
e. Optionally, if you want to automatize this, edit the code in the giix-generator templates:
Put the following code into the file \extensions\giix-core\giixModel\templates\default\model.php
~~~
<?php $modelClassP = $this->pluralize($modelClass); ?>
public static function label($n = 1, $case=self::NOMINATIVE) {
if($n == 1)
return Yii::t('app', '<?=$modelClass;?>', $case);
else if($n > 1)
return Yii::t('app', '<?=$modelClassP;?>', $case);
}
~~~
Then work through the view files under \extensions\giix-core\giixCrud\templates\default\
and replace translations as described in step d.
That's it. If something is unclear/wrong/incomplete or you have other suggestions please let me know.
Note: If you use the gii code generator, you have to customize the standard gii-templates to add translation of attribute labels etc. (see: The Definitive Guide to Yii)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.