Difference between #13 and #14 of How to obtain a grammatically correct translation of the model name in giix-generated view files

unchanged
Title
How to obtain a grammatically correct translation of the model name in giix-generated view files
unchanged
Category
How-tos
unchanged
Tags
i18n, grammatical cases, translation, giix, How to
changed
Content
The [giix](http://www.yiiframework.com/extension/giix/ "giix") code
generator has out of the box i18n support for model names and attribute labels
(besides other cool features > check it out!) .
<br>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.
<br>Currently, in the automatically generated code in
views/object/admin.php, it is done like this:
~~~
[php]
echo Yii::t('app', 'Manage') . ' ' . $model->label(2);
~~~

Since Object translates to 'Obje', and Objects translates to 'Objeler' in
turkish, 
<br>it would look like: **"Yönet Objeler"**.
<br>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...
	
### SOLUTION
	
**a.** Define constants in the file
`\extensions\giix-components\GxActiveRecord.php`
~~~
[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`:
~~~
[php]
public static function label($n = 1, $case=self::NOMINATIVE) {
	if($n == 1)
		return Yii::t('app',
'Object','n==1#Object|n==2#Object|n==3#Object|n==4#Object',
$case);
	else if($n > 1)
		return Yii::t('app',
'Objects','n==1#Objects|n==2#Objects|n==3#Objects|n==4#Objects',
$case);
}
~~~
**c.** Add the translations to your message file (e.g. in this case
`messages\tr.php`):
~~~
[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','n==1#Object|n==2#Object|n==3#Object|n==4#Object'=>'n==1#Obje|n==2#Objenin|n==3#Objeye|n==4#Objeyi',	'n==1#Objects|n==2#Objects|n==3#Objects|n==4#Objects'=>'n==1#Objeler|n==2#Objelerin|n==3#Objelere|n==4#Objeleri',
~~~
**d.** Now let's use this in the view files, and take care of the order of the
words:
<br>In the view files, e.g. in `views/object/admin.php`, replace
~~~
[php]
echo Yii::t('app', 'Manage') . ' ' . $model->label(2);
~~~
with
~~~
[php]
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:
<br>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;?>','n==1#<?=$modelClass;?>|n==2#<?=$modelClass;?>|n==3#<?=$modelClass;?>|n==4#<?=$modelClass;?>',
$case);
	else if($n > 1)
		return Yii::t('app',
'<?=$modelClassP;?>','n==1#<?=$modelClassP;?>|n==2#<?=$modelClassP;?>|n==3#<?=$modelClassP;?>|n==4#<?=$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](http://www.yiiframework.com/doc/guide/1.1/en/topics.gii#extending-gii
"Extending Gii"))