After creating a new site with YII, I added a folder 'fr' in protected/messages and added a file 'site.php' which contains:
return array('hello' => 'bonjour');
in the view/layout/main.php, I added following code:
<?php
// I change the language to english and french using session.
// This is just for example.
Yii::app()->language = 'fr';
// I also used Yii::app()->setLanguage('fr');
echo Yii::t('site','hello');
?>
But the language is not translated.. Where am I wrong. Please suggest
You can set the default languages in the config/main.php as
and change your language everywhere you like:
also for more experience, maybe:
You should set language in the controller if you want translations to work properly in all views.
In order for language to be applied to all Controllers, create in components folder new
Controller.php
file withclass Controller
which extendsCController
, and then all your controllers should extendController
class. inController
class overrideinit()
method (don't forget to callparent::init()
) and set language there. For example:This way you can add additional things which should apply to all Controllers at one place
You forgot to set source language.
Into config:
Or app: