M I
M I

Reputation: 3682

Display multilingual error messages

I am using Yii framework to develop a multilingual web site. Right now I have English and French versions. I have created a fr folder and placed views and controllers related to French version, but using same models for both versions.

My database tables structure (articles table) is:

 id, title_en, title_fr, title_ru, detail_en, detail_fr, detail_ru and     
 so on.

The issue I am facing is that I need to display error messages to the users in respective languages like in French version error message should be in French. How can I do while using same model for all languages.

Where do I need to put the French messages?

Upvotes: 1

Views: 555

Answers (2)

trejder
trejder

Reputation: 17505

I don't know, if I'm getting you correct, but if I understand your question, your problem is already solved by core Yii 1.x engine and there isn't much work left to do.

In detail, you have to:

  1. Create any file (for example app.php) in /protected/messages/fr/ folder.

  2. Put 'coreMessages'=>array('basePath'=>'../protected/messages'), to your application's configuration array -- route Yii translation system to folder with your customized translations.

  3. Put 'sourceLanguage'=>'en' to your configuration array -- make sure, that your applications's core language is set to English.

  4. Fill app.php file with your translation strings (see below).

  5. Translate parts of your application with Yii::t('app', 'string'); (see below).

Your translation files should be in format:

return array
(
    //Translations from Google Translate! :> I don't speak French at all! :>
    'This page is not yet translated into your language.'=>'Cette page n'a pas encore été traduit dans votre langue.',
    'Language'=>'Langue',
    'read more'=>'en savoir plus'
);

So, for example, but in your blog list view string like:

<?php Yii::t('app', 'read more'); ?>

When you set your application language to fr, either by forcing it in code (Yii::app()->language = 'fr';) or presetting it in appliction's configuration array ('language'=>'fr'), you should see en savoir plus (or whatever you put to your translation file) in place of read more in above view.

If your application's language is set back to English or if given translation string is not found, you'll see text written in Yii::t. That's why it is so important to use fully qualified English strings in this method (and in translation files) and to avoid "patterns", like 'core.app.language'=>'Langue' etc.

Note, that you can also use parameter placeholders:

'Error no {code}:'=>'Erreur n {code}:'

that will be replaced with the actual parameter values:

Yii::t('app', 'Error no {code}:', array('{code}'=>$code));

This way you can provide variable values to otherwise constant message text.

There's a great topic about internationalization in "The Definitive Guide to Yii". You should read it through for more information on this matter or, if you want to use Yii translation mechanism in a bit more advanced way.

Upvotes: 1

Harikrishnan
Harikrishnan

Reputation: 9979

You can override CPhpMessageSource::loadMessages() with something like following:

protected function loadMessages($category,$language)
{
    if ($category == 'yii') 
        return array();
    else
        return parent::loadMessages($category,$language);
}

Also You can change the language, set CApplication::language appropriately. This can be done at runtime as in

Yii::app()->language = 'fr';

Upvotes: 0

Related Questions