Difference between #6 and #5 of i18n for your model in just 1 line

unchanged
Title
i18n for your model in just 1 line
unchanged
Category
Tutorials
changed
Tags
i18n
unchanged
Content
A simple trick to get the localized version  of a  model field is to add this
little method to your models.

~~~
[php]
[...]
public function getNameLocalized()
{
    return Yii::t(strtolower(__CLASS__), $this->name);
}
~~~

What this snipped does is, that it looks for a translation with your model name
(in lowercase) as category and the database entry as the translation key.

* * *

## Just one simple example:
Imagine you have a __country__ table (and a model called __Country__)

<pre>
  ID  |  NAME
------|--------
   1  |  sweden
   2  |  italy
   3  |  norway
------|--------
</pre>

* * *

Use any of the offered translation methods. I simply use php-files in the
``protectec/messages`` folder.

Let's assume the language is set to ``'de'``. The translation
file would look like that:

__protected/messages/de.php:__
~~~
[php]
<?php
return array(
    'sweden' => 'Schweden',
    'italy'  => 'Italien',
    'norway' => 'Norwegen',
);
~~~

* * *

Now this is the fun part where PHP5 unfolds its magic (functions):
To easily create a localized drop down list where your visitor can select a
country you only need this simple line of php code in your view:

~~~
[php]
<?php
echo CHtml::activeDropDownList($model, 'country_id', 
     CHtml::listData(Country::model()->findAll(), 'id',
'nameLocalized'))
?>
~~~

* * *

This creates this kind of HTML-Code:
<pre>
``
<select [...]>
    <option value="1">Schweden</option>
    <option value="2">Italien</option>
    <option value="3">Norwegen</option>
</select>
``
</pre>

* * *

I hope this helps you saving time. If you have any suggestions just drop a
comment.