2

Hello.

In Yii1.1 i could make an action in siteController and then using: Yii::app()->controller->createUrl('actionname', array('language'=>'new language to toggle'));

It was possible to me to create hiperlinks with CHTML in index file and when clicked those hiperlinks changed the entire tranlations of the website from Portuguese to other language and all way around.

Now i'm beginning with Yii2 and controller does not have createUrl() anymore. Also the other methods dont work that way. I tried run, runAction, tried with the Url:: class and nothing.

Also, in Yii2 making <?php echo Html::a('Portuguese', Yii::$app->language = "pt"); ?> Doesn't do anything !!! When clicked the hyperlink does not change the language of the site.


Does anyone know how to create an hiperlink or some other way in Yii 2 that toggles completely the entire website language. I need to have two versions of this website -> English and Portuguese.

I need to perform like this -> when click in the english word it will translate to English, and when click the portuguese word it will change the site to Portuguese language.

Any Ideas ??

Many thanks in advance.


NEW EDIT TO THE QUESTION I wrote this code in my siteController, and now the sites toggles languages, but only on second mouse click the toggle happens and the content is refreshed. Anybody knows why?

Here are my actions

public function beforeAction($action) {
    if (Yii::$app->session->has('lang')) {
        Yii::$app->language = Yii::$app->session->get('lang');
    } else {
        Yii::$app->language = 'us';
    }
    return parent::beforeAction($action);
}

public function actionLangus(){  
    Yii::$app->session->set('lang', 'us'); //or $_GET['lang']
    return $this->render('index');
}

  public function actionLangpt(){  
    Yii::$app->session->set('lang', 'pt'); //or $_GET['lang']
    return $this->render('index');
}

I opted for render('index'), because redirect was not finding the view to display.

Many thanks for a solution...

André Castro
  • 1,527
  • 6
  • 33
  • 60

1 Answers1

2

You can do like below in Yii2:

\yii\helpers\Html::a('Change Language',\yii\helpers\Url::toRoute(['controller/action',['language'=>'NEW_LANGUAGE']]));
\yii\helpers\Html::a('Change Language',['controller/action',['language'=>'NEW_LANGUAGE']]);

Or:

use yii\helpers\Html;
use yii\helpers\Url;

Html::a('Change Language',Url::toRoute(['controller/action',['language'=>'NEW_LANGUAGE']]));
Html::a('Change Language',['controller/action',['language'=>'NEW_LANGUAGE']]);

Then, in your action, you need to do:

//validation
Yii::$app->language= "NEW_LANGIAGE";
// store current language using state or cookie or database

UPDATE

action code:

//please put some validation on $_GET['lang']
Yii::$app->session->set('lang', 'us'); //or $_GET['lang']
$this->redirect('controller/action'); // redirecting user to somewhere

Then in your controller's beforeAction:

public function beforeAction($action) {
    if (Yii::$app->session->has('lang')) {
        Yii::$app->language = Yii::$app->session->get('lang');
    } else {
        //or you may want to set lang session, this is just a sample
        Yii::$app->language = 'us';
    }
    return parent::beforeAction($action);
}

By above code, in every action, it checks whether the language is set in session or not. If yes, it changes the language. If not, it sets language to us as default. In your change language action, it just sets the new language, which, is given from $_GET request, into the session. Then it redirects user to another page. Please note that, this is just a suggestion, others might use different ways, such as creating a bootstrap component, using controller init() method, storing lang into cookie or database and so on.

Ali MasudianPour
  • 14,329
  • 3
  • 60
  • 62
  • Bur now i don't now what to do in my action. I made this action: – André Castro Dec 01 '14 at 17:31
  • public function actionLang(){ Yii::$app->language= "us"; // store current language using state or cookie or database return $this->render('index'); } – André Castro Dec 01 '14 at 17:31
  • But obviously this does not validate anything. Can you give some more clues to write down the actionLang() ?? – André Castro Dec 01 '14 at 17:33
  • @AndréCastro You're welcome dear, please check my update, also it is notable that I used session to store current language. – Ali MasudianPour Dec 01 '14 at 17:42
  • I'm sorry, but the update only changes de adress bar between us or pt, and it does not change the website language. When i change to pt mode language in my login form it continues to send messages in us language. – André Castro Dec 01 '14 at 17:56
  • I think i resolved the issue based on your code. I created 2 actions in site controller. The first sets 'pt' language, and the second sets 'us' language. It's not very elegant doing things like this, but for now it is working. I will not mark this question as resolved for now, because it may appear a better solution to what i just did. – André Castro Dec 01 '14 at 18:37
  • I have a new EDIT to the question. Please feel free to check it. – André Castro Dec 01 '14 at 20:12