i18N for labels

Hi,

Is there a way so Yii translates all labes generated by CHtml::activeLabel automatically or is it not possible to use activeLabel when the application requires internationlization?

In your label definitions, you can use Yii::t().

I do the translation in the active record class in attributeLabels().

This is a bit long winded, because you have to write Yii::t() again and again.




public function attributeLabels() {

    return array(

        'name' => Yii::t('myproject','Employee name'),

        'comment' => Yii::t('myproject','Comment'),

        ...

    );

}



Instead you could write your own CHtml (e.g. MyCHtml) class which does the translation of attributes in a single place (e.g. in activeLabel). Since PHP<5.3 does not support late static binding you cannot simple extend CHtml and override activeLabel. You will have to copy CHtml completely and do your changes.

I recently wrote the above comment in another thread. What if we use the second approach and declare source language as “dummy”? (I haven’t tried.) Then we can keep all attribute labels in one place.

I’m also thinking about creating a base model with an i18nLabels() method to be used instead of attributeLabels. The latter could be overriden to incorporate the Yii::t() call given the return array from i18nLabels().

Edit:




public function i18nLabels() {

  return array(

    'attribute1',

    'attribute2',

    ...

  }



/Tommy

I think the most reasonable approach would be to patch CHtml::activeLabel to do translations automatically.

Not sure if we should submit that as a ticket to have the framework itself patched by qiang or whether this should be left up to the individual developer

CHtml::activeLabel isn’t the only method presenting translated attribute labels. CSort::link comes to my mind. Perhaps there are others.

/Tommy

Edit: Thus, it’s probably a good idea to use attributeLabels() for translation. If it can be considered safe to translate all attributes, that could be done automatically in a base models attributeLabels(). No need to define a virtual i18Labels method (as I pictured in a previous post). Of course my idea about a “dummy” source language setting apply. (All labels will reside under the messages directory).

I have the same opinions of yours. All text outputs from CHtml should be automatically translated.

Qiang believes that most applications do not need multilingual support, which makes this feature request unlikely to be implemented.

Use your text editor wisely, add snippets or templates or whatever it is called. With NetBeans I’m able to set up a shortcut like ‘_t(’ to have replaced by echo Yii::t(‘myCategory’, {cursor});.