yii translations and error messages

I’ve made my own custom folder under messages called fi for finnish. I took the base of de (german) and translated the most important messages to finnish the ones the user is likely to see and added @@ marks arround the ones that appear when shit hits the fan the ones I end up reading.

Then I’ve under main.php protected/config/main.php defined the language as fi and




Now if I try to submit a form that has rules like cannot be blank I would expect to see the translated error message but I get the english one.

'{attribute} cannot be blank.' =>'{attribute} ei saa olla tyhjä.',

But if I add something that will cause the application to crash I will get the translated error message:

'Property "{class}.{property}" is not defined.' =>'Ominaisuus "{class}.{property}" ei ole määritelty.',

I’m not going to actually use that translation since for me it makes no sense to display those errors localized. I just translated that since that error is easy to create to test it.

I got the error message:

So my custom language localization file yii.php under /messages/fi is clearly working but for some they don’t work for form validation rules. What am I missing ?

Thanks for any help I’ve been trying to figure this out for hours.

Just to make sure, is this the default message or one you added in a validation rule, and perhaps forgot to enclose in Yii:t().

'{attribute} cannot be blank.'


Thanks for the swift reply.

It’s the default message I somehow assumed that this would apply to them also. Could you give me an example of how to use the Yii:t() you are referring to ?

Yii:t('translation_file_to_look_in', 'your_message');

The Definitive Guide to Yii

The API reference.

There are also some wiki articles on i18n


Still haven’t figured out where to use that translation function.

But I might find it on my own.

On my form I have:

<?php echo $form->errorSummary($model); ?>

this outputs the validation errors.

on my form model I have for example one rule that is:

array('ht', 'length', 'is'=>11),

So those are the messages I would need Yii to translate but can’t figure out where to add the t();

This wiki article has an example of how to use the message property of a validator.

Spontaneously I think what you tried to do in the first place should work.

Of course you also have the option to post a complete translated yii.php for inclusion in the framework. It probably will be added to SVN almost immediately. This thread is quite old but may be the right place for adding translations (or you can create a ticket)


I’ll have a look at it again first thing monday. I’ve been reading everything I can find about translating yii messages and I just assumed that if I make the translation file + tell yii to use it it would be used globally without having to use the t() function. In this case this app I need to publish well 2 weeks ago but realistically asap so I might have to do custom messages for this one and revisit this translation issue on my next project.

But thanks for all the help. It will most likely take a year or so to really take full benefit from using a framework after developing for 8+ years without one. BUt all in all it looks very promising.

I had a look at the errorSummary function in CHthml.php

$header='<p>'.Yii::t('yii','Please fix the following input errors:').'</p>';

So the translate function is there and apparently it should just work if the translation file is in messages and the language is either set in the application or in the main config file. I’ll make a ticket.

For completeness of the thread, would the answer be to use Yii:t() in the rules definition of the model as follows:-

	public function rules()


		return array(

		    array('Size,', 'numerical', 'min'=>0, 'max'=>3, 

                         'tooBig'=>Yii::t('Validation','Size should be in meters in the format m.cm eg: 1.65'), 

                         'tooSmall'=>Yii::t('Validation',('Size should be in meters in the format m.cm eg: 1.65'), 

                          'message'=>Yii::t('Validation','Size should be a whole number')