darioo
(Timmy Dario)
November 27, 2014, 10:07pm
1
I’m using this code to set language in yii
if(isset($_GET['lang']))
{
setcookie("lang", $_GET['lang'], time() + 31104000, "/"); // 86400 = 1 day
\Yii::$app->language =$_COOKIE['lang'];
}
else if(isset($_COOKIE['lang']))
{
\Yii::$app->language =$_COOKIE['lang'];
}
else
{
\Yii::$app->language ="en";
}
I’ve put it in \vendor\yiisoft\yii2\web\Controller.php
But I kinnda think it is not the best solution. What would be best solution to set language of my app.
I’m looking for something where I can execute my code before any action of any controller
@darioo , Did you got the solution for this problem you described. I’m also getting same problem. If you have any solution update it here it will helpful all others.
patrickm
(Yiiframework)
October 5, 2016, 8:43am
3
Derive your own controller from "\yii\web\Controller", implement its "beforeAction" and then derive every other controller from that base controller.
weluse
(Jboehnke)
October 5, 2016, 12:02pm
4
Hi darioo here ist very good Tutorial from samdark:
Selecting application language
==============================
When developing applications or websites for global marked, supporting multiple languages is always a requirement.
Yii has built in solution for handling message translations but doesn’t provide anything about selecting a language
because implementation depends of requirements.
In this recipe we’ll describe some typical cases of language selection and provide ideas and code snippets so you’ll
be able to pick what’s required and implement it in your project.
## How to set application language
Setting application language is pretty simple. It can be done either via code like the following:
```php
Yii::$app->language = 'ru_RU';
```
or via application config such as `config/main.php`:
This file has been truncated. show original
schmunk
(Schmunk)
October 6, 2016, 8:48am
5