Yii 2 多语言网站

Yii 2 MULTILINGUAL WEBSITE

我正在尝试在 Yii2

中实现多语言小部件 like this

我裁判了this

但我卡在了构建选择器小部件的最后一部分。谁能列出 Yii2 中的分步过程?

下面是我的语言选择器代码,returns 什么都没有,现在打破了布局

<?php //languageselector.php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

?>

<div id="language-select">
<?php 
if(sizeof($languages) < 4) {
    // Render options as links
    $lastElement = end($languages);
    foreach($languages as $key=>$lang) {
        if($key != $currentLang) {
           echo  $owners->createMultilanguageReturnUrl($key);
            echo Html::a(
                 $lang,
                 $owners->createMultilanguageReturnUrl($key));
        } else echo '<b>'.$lang.'</b>';
        if($lang != $lastElement) echo ' | ';
    }
}
else {
    // Render options as dropDownList
    echo Html::form();
    foreach($languages as $key=>$lang) {
        echo Html::hiddenField(
            $key, 
            $this->createMultilanguageReturnUrl($key));
    }
    echo Html::dropDownList('language', $currentLang, $languages,
        array(
            'submit'=>'',
        )
    ); 
    echo Html::endForm();
    }
    ?>
    </div>

经过大量试验,我找到了解决问题的办法。 希望在 yii2 中构建多语言小部件的人请参考下面

<?php
namespace app\components\widgets;
use Yii;
use yii\helpers\Html;
use yii\base\Widget;
use app\components\views;

class LanguageSelectorPortlet extends Widget
{
public function init(){
    $this->renderContent();
}

public function renderContent()
{
    $currentLang = Yii::$app->language;

    $languages = Yii::$app->params['languages'];
    echo $this->render('languageSelector', array('currentLang' => $currentLang,    'languages'=>$languages));
}
}

其视图文件如下

   <?php
   use yii\helpers\Html;
   use yii\bootstrap\ActiveForm;

  ?>
<div id="language-select">
<?php 

    // Render options as links
    $lastElement = end($languages);
    foreach($languages as $key=>$lang) {
        if($key != $currentLang) {
            echo Html::a($lang,Yii::$app->getUrlManager()->createmultiUrl($key));

        } else echo '<b>'.$lang.'</b>';
        if($lang != $lastElement) echo ' | ';
    }


 ?>
</div>

希望对在 YII2 中使用多语言小部件苦苦挣扎的人有所帮助