Internationalization - what to put on views for same source and target language

Hello all,

We are writing an application that will use i18N for internationalization.

From the docs, I understand that translation only happens when Source and Target language are different and from that angle I could make my app work as instructed.

I created the translations under /protectec/messages/my_language/main.php and on my view, I used

<?php echo Yii::t("main","FRONTPAGETITLE");?> which worked like a charm.

My query is about what to put on views if both source and target languages are the same as if I use the above code, the view will end up displaying FRONTPAGETITLE.

Many thanks,

Cass Surek

I have seen quite a few people using Yii::t() incorrectly.

If your front page title is “Hello World”, in your view you should put Yii::t(‘category’,‘Hello World’);, not Yii::t(‘category’,‘helloworld’);, the second parameter for Yii::t() is not a placeholder but the actual text that will be outputted for your source language.

To translate to other languages, in the file under protected/messages/locale Id/ where locale Id is the Id of your language eg: zh_cn, you’ll have something like


return array(

...............,

'Hello World'=>'世界您好',

.............);

I note that even if one uses the Yii::t() correctly - i.e. with real texts and not placeholders - the problem mentioned by Cass is still valid!

E.g. I am developing a product that will be installed in many instances (well, hopefully) and this may put the application into different contexts. Let’s assume that a funny guy installs it who wants to change the following system message:

{attribute} cannot be blank

this way:

{attribute} is mandatory! Enter something or else…

Note that this is not really a translation - this is customization. I want to make it possible that my end users can easily change the application messages even if they "translate" them just from English to English! The obvious way to change these messages if they find them in the source and change there - but it is neither easy, nor nice. I would call it "easy" when the messages were read from language files even if no translation occurs, but this is currently not so in Yii!

I have applied the following workaround to achieve my goal:

main.php




// CWebApplication properties can be configured here.

return array(

    ...

    'sourceLanguage'=>'xyz',

    'language'      =>'en',

);



‘xyz’ is a non-existing language - it is only necessary to “force” the translation from the real source language (English) to the target language which I have also explicitely specified above (English).

Now, if I create the ‘framework/messages/en/yii.php’ file with the following content:




return array(

    ...

    '{attribute} cannot be blank.'=>'{attribute} is mandatory! Enter something or else...',

    ...

);



it will "translate" the system message to the silly one. Of course, this trick will be also applicable to other application messages - not only to system messages.

So that someone can keep all the program messages in language files (or in DB) even if they are not a subject of any translation, it may be a useful feature. And a native support for that in Yii without the necessity of applying the above workaround might make sense in my opinion.

True. For me, the i18n implementation is the only negative part of Yii I’ve noticed. But it’s easy to write an own component, so no big deal. Still, would be cool if this could find the way into core…

Open a ticket http://code.google.com/p/yii/issues/list

Hello,

I’m trying for 2 days to get i18n working, read all posts, guides and api docs, and still the most simple case doesn’t work. I did this:

set the language and sourceLanguage in controller and main.php to ‘nl’ Created a general.php under protected/messages/nl with this content:

return array(

'Aanmelden'=&gt;'Aanmelden',

);

And used this in the login.php (from the generated skeleton app)

<h1><?php Yii::t(“general”, ‘Aanmelden’); ?></h1>

And still no label is displayed…

Please help, what’s missing? :frowning: