Yii2-制作超链接以在语言之间切换 [英] Yii2 - Make hyperlink to toggle between languages

查看:127
本文介绍了Yii2-制作超链接以在语言之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

在Yii1.1中,我可以在siteController中执行一个操作,然后使用:Yii::app()->controller->createUrl('actionname', array('language'=>'new language to toggle'));

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

我可以用CHTML在索引文件中创建链接,当单击这些链接时,整个网站的翻译都从葡萄牙语翻译成了其他语言.

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.

现在我从Yii2开始,控制器不再具有createUrl()了.其他方法也不能那样工作.我尝试运行,运行动作,尝试使用Url ::类,什么也没做.

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.

此外,在Yii2中制作<?php echo Html::a('Portuguese', Yii::$app->language = "pt"); ?> 什么也没做!单击时,超链接不会更改网站的语言.

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.

是否有人知道如何在Yii 2中创建可完全切换整个网站语言的hiperlink或其他方式.我需要该网站有两个版本->英语和葡萄牙语.

我需要执行以下操作-> 当单击英语单词时,它将翻译为英语,当单击葡萄牙语单词时,它将把站点更改为葡萄牙语.

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.

任何想法?

非常感谢.

问题的新编辑 我在siteController中编写了此代码,现在这些站点可以切换语言,但是只有在第二次单击鼠标时,切换才会发生,并且内容会刷新.有人知道为什么吗?

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?

这是我的动作

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');
}

我选择了render('index'),因为重定向未找到要显示的视图.

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

非常感谢您的解决方案...

推荐答案

您可以在Yii2中执行以下操作:

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']]);

或者:

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

更新

操作代码:

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

然后在控制器的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);
}

通过以上代码,在每个操作中,它都会检查是否在会话中设置了语言.如果是,它将更改语言.如果不是,则默认将语言设置为us.在更改语言操作中,它只是将在$_GET请求中指定的新语言设置为会话.然后,它将用户重定向到另一个页面. 请注意,这只是一个建议,其他人可能会使用不同的方式,例如创建引导程序组件,使用控制器init()方法,将lang存储到cookie或数据库中等等.

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.

这篇关于Yii2-制作超链接以在语言之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆