Enforcing translation of message in Yii

Submitted by boaz on Tuesday, May 15, 2012.

I had it all working nice, but I couldn't get the translation of messages in the module (my Yii Framework module). In fact, that's the first place where it was important to me so its the first time that I paid attention to the translation service. 
So, I used the following syntax when calling t():
Yii::t("PollsModule.general", "Polls Widget") (this is a polls widget defined inside a polls module I'm developing as well).
Also, I had all the needed directory tree and files under the module's messages directory, but it wasn't working!

Problem was caused by two reasons:

  1. Everything in my Yii application was default including the source language and the target language (both set to en_us). This is nice but it also means that by default, no translation will take place since if both source and target language are equal - translation is bypassed and the source message is taken as the translated message as well. This is easily overcome by setting 'forceTranslation' => true, in the 'messages' component of the application config file (under component 'messages').
  2. Somehow, I had my module and widget working fine without the following lines:
    'import' => array(
    But the translation didn't. Yii exploded, complaining that "include(PollsModule.php): failed to open stream: No such file or directory". It is only when I introduced this line that everything started to sing together.
I Hope this helps someone to save a few debugging/learning hours.

Leave a Comment

Fields with * are required.