How to obtain a grammatically correct translation of the model name in giix-generated view files

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.


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

  1. change the order of the words as I like and
  2. 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...


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);


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)

4 0
1 follower
Viewed: 19 181 times
Version: 1.1
Category: How-tos
Written by: c@cba
Last updated by: c@cba
Created on: Feb 10, 2012
Last updated: 12 years ago
Update Article


View all history

Related Articles