Internationalization issue

Hello everybody,

I’m currently developing a multi language website using Yii (which, by the way, is an amazing framework!), but I’m having some issues on managing the current language. Maybe I’m missing some basic step, as it’s my first try at this, and I couldn’t find any other problem alike in the forums, so let me explain it in detail.

To perform the language change, I’ve created a controller, to which I pass the destination language via GET, and then set the app language, like this:




Yii::app()->setLanguage($_GET['lang']);

Yii::app()->session['lang'] = Yii::app()->language;



Then, following some advices I found on other topics, I manage the language in my main layout page, with this line:




<?php Yii::app()->setLanguage(isset(Yii::app()->session['lang']) ? Yii::app()->session['lang'] : 'pt_br'); ?>



All this works nicely. I can translate all my text in the layout file via Yii::t command, and if I echo the language via Yii::app()->language it gives me the correct language code.

The problem I’m facing is with the controller I’m displaying. If I echo the app language from it, or from its view file, I always get the default language, even though the layout is displaying the user-set language. Curiously enough, if I echo the session variable it shows me the right language, but not in the app language. I know I could set it right in each controller, or even create a base class and extend my controllers from it, but it doesn’t seem right, as I’m already setting the language like I showed you above. Why aren’t my controllers getting the right language from the app?

Any thoughts on this are very much apreciated!

Thanks!

Where are you setting the language in your controller? If you’re not already doing so try putting your code in the beforeAction() method of the controller.

BeforeAction()

Hope this helps.

Move this piece of code:




Yii::app()->setLanguage(isset(Yii::app()->session['lang']) ? Yii::app()->session['lang'] : 'pt_br'); 



In the init() method of protected/controllers/Controller.php , the one all your controllers extends from.

Also, don’t just




Yii::app()->setLanguage($_GET['lang']);

Yii::app()->session['lang'] = Yii::app()->language;



Validate and sanitize the $_GET before you use it .

Thank you so much for your replies!

EdoFre: In the controller I was just setting it and redirecting back to the home page. That line checking the session and setting the language back was in the main layout page.

twisted1919: Thanks a lot! Your suggestion worked right away. The only correction I need to make is that the Controller class is not on protected/controllers, but in protected/components. I hadn’t noticed that the controllers were already extending another class that isn’t part of the framework itself. Also, thanks for pointing out about the $_GET validation.

Ah, my bad about the file location, i was in a bit of hurry when i wrote the reply.

Remember that you need to sanitize everything that comes from the users($_POST/$_GET/$_COOKIE)

Hi

I tried doing this

it looks all good in the Controler side as I put logs to see the language,

but when it reaches the view the language seems to stick to the system default language

I tried forcing it inside the view copying

Yii::app()->setLanguage(isset(Yii::app()->session[‘sel_lang’]) ? Yii::app()->session[‘sel_lang’] : ‘en’);

that works but is not a solution

any ideas are welcome

Hi twisted1919!

Two questions:

  • Why are there to modify the Controller.php in /protected/componentes and why not in the main controller in /protected/controllers? Besides, I don´t see no init() method in this file.

  • How are they done validations about $_POST/$_GET/$_COOKIE?

Thanks in advance!