问题
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...
回答1:
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.
来源:https://stackoverflow.com/questions/27233350/yii2-make-hyperlink-to-toggle-between-languages