Can't get Yii::t() to translate!

Hi,

I can’t get Yii::t() to translate a text.

I have a file test.php under protected/messages/en_us

it contains the following text:


return array(

'key1'=>'Value 1',

'key2'=>'Value 2'

);

I tried to set language in my model:

$app = Yii::app();

$app->language = strtolower($this->lang);

then echo Yii::t(‘text’,‘key1’); will only print key1! what am I missing?

To make your example work, Yii::app()->defaultLanguage must be different from Yii::app()->language, and $this->lang must be "en_us".

What is the source language of your application and TO what language do you want to translate your messages?

defaultLanguage is not defined. I guess you meant: sourceLanguage

So I went on and added : $app->sourceLanguage = ‘ar_sy’; before setting language. Still didn’t work.

I tried adding

    'sourceLanguage'    =>'ar_sy',

to config/main.php

still didn’t work.

Yes, it’s sourceLanguage of course.

So, steps to make your example work:

  1. Create a file text.php under protected/messages/en_us.

  2. Set Yii::app()->sourceLanguage to ‘ar_sy’ in your main config file.

  3. Set Yii::app()->language to ‘en_us’ anywhere before using t().

  4. Use t() function: Yii::t(‘text’, ‘key1’).

That’s exactly what I did before, but no luck!

protected/config/main.php:




return array(

       	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

        'name'=>'My website's name',

        'sourceLanguage' =>'ru_ru',

        'preload'=>array('log'),

        ....



My model has the following method:




    public function display($thiss) {

        $this->setThis($thiss);

        $this->getBookId();


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

        $result = self::model()->findAll(array(

                'select'=>'verse_v, number',

                'condition'=>'bid=:bid AND chapter=:chapter',

                'params'=>array(':bid'=>$this->bid,':chapter'=>$this->chapter),

        ));

        $results = array(

            'bid'=>$this->bid,

            'bookName'=>Yii::t('BibleBooks','fulltTitles'.$this->bid), //$this->bid = book ID

            'chapter'=>$this->chapter);

        foreach ($result AS $row) {

            $results[] = array(

                    'number'=>$row['attributes']['number'],

                    'vowelled_verse'=>$row['attributes']['verse_v']);

        }

        return $results;

    }



and I have a file called BibleBooks.php under protected/messages/en_us/BibleBooks.php

and the file contains:




return array(

        'bibleBooks'=>'Bible books',

        'theoldTestament'=>'The old testament',

        'oldTestament'=>'Old Testament',

        'OT'=>'OT',

        'thenewTestament'=>'The new testament',

        'newTestament'=>'New Testament',

        'NT'=>'NT',

        'apocrypha'=>'Apocrypha',

        // Bible books full title

        'fullTitles1'=>'The book of Genesis',

        'fullTitles2'=>'The book of Exodus',

        'fullTitles3'=>'The book of Leviticus',

        'fullTitles4'=>'The book of Numbers',

        'fullTitles5'=>'The book of Deuteronomy',

        'fullTitles6'=>'The book of Joshua',

        'fullTitles7'=>'The book of Judges',

        ....



My controller calls the model’s results:




    public function _display(){

        $model = new Bible();

        $displayResults = $model->display((object)$this->options);

        if(empty($displayResults)) {

            throw new Exception('NO RESULTS');

            return false;

        }

        echo $displayResults['bookName'];

        foreach($displayResults AS $Res){

            $Res['number']." ".$Res['vowelled_verse']."<br />";

        }

    }



So, echo $displayResults[‘bookName’]; prints: ‘fullTitles2’ instead ‘The book of Exodus’

I feel like this should be a piece of cake, but I’ve been trying for hours!

Thanks.

It is the working example except this line:




'bookName' => Yii::t('BibleBooks','fulltTitles'.$this->bid);



tTitles ;)

Note: The correct source language is "ru", not "ru_ru".

Oops! typo while changing the one inside the example. In fact, the code has the correct and exact key. The issue is not this one at all.

And also, I’ve changed sourceLanguage to ru instead of ru_ru and still no luck!

Can you put the next code anywhere just for test?




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

echo Yii::t('BibleBooks', 'fullTitles2');

Yii::app()->end();



It works perfectly for me. No special settings, just created a file with translations and set sourceLanguage to "ru".

I posted it inside site/index

output is: fullTitles2

This is really strange!

I have Yii 1.1.1 by the way.

I noticed that when I set sourceLanguage and I get Exception errors in Russian, so that part works.

I digged a little into core files, nothing seems to be wrong! and I don’t think there should be any bug with the core just with a normal functionality such as Yii::t()! I really like this framework. But it bugs me a lot to get stuck at a silly issue like that :(

Maybe I’ve missed something when was reading your code, but just try to create an empty project and repeat steps I described above. It must work…

Are you sure that text.php is located in the correct place?

And are you sure that this file is returning a correct array?

you wrote


Yii::t('text','key1');

so the correct file should be named "text.php" and not "test.php"

or change the category to "test"

anyway, if you have a lot to translate, try out this extension module I made