📜 ⬆️ ⬇️

Multilanguage widget in YII2 without using a database

An example of the internationalization of the site on Yii2 in two languages: ru and en. The component responsible for internationalization is already built into Yii2, it is called i18n. In order to be able to start using it, just add it to the application configuration in the components section.

I use the advanced template so the location of the files may vary.

purpose
')
  1. switching a site between two languages: ru and en;
  2. display language in the address bar as site.com/en/;
  3. automatic redirection of the user to the language most suitable for him, if he went to the site without specifying the language;
  4. translation storage should be carried out in PHP files as arrays;

Configuration

Edit the configuration file, in my case it is \ frontend \ config \ main.php

return [ 'language'=>'en', //   ,                    . 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'class' => 'frontend\widgets\MultiLang\components\UrlManager', //   .  . 'languages' => ['ru', 'en'], //      'enableDefaultLanguageUrlCode' => true, //     ,  false,           www.site.com/ ,  true – www.site.com/ru 'rules'=>[ '/' => 'site/index', '<controller:\w+>/<action:\w+>/'=>'<controller>/<action>', ], ], 'i18n' => [ 'translations' => [ 'app*' => [ // app   php      app.php (  ) 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@frontend/messages', //      frontend/messages/ru/app.php 'sourceLanguage' => 'en', //    ,  ,        ], ], ], ], 

'class' => 'frontend \ widgets \ MultiLang \ components \ UrlManager', the contents of the file are taken from here , you can follow the developer’s instructions and use the composer, but we do the widget, so just copy the UrlManager.php into our widget.

The file with the list of frontend / messages / ru / app.php transfers should contain an array

 return [ ... 'Example text...' => ' ...', ... ]; 

We use the built-in method t

 <?= Yii::t('app', 'Example text...') ?> 

In the first argument we indicate the category, we have one - the app, you can create many translation files. In the second argument, we write the English text as it should be displayed on the site.

Switch languages.

Create a folder MultiLang in the folder with widgets, it looks like this for me:

 Frontend\ Widgets\ MultiLang\ Components\ UrlManager.php Views\ View.php MultiLang.php 

To show the language switch in any place, call

 <?= MultiLang::widget(['cssClass'=>'pull-right language']); ?> 

Do not forget to set the path to the widget.

 use frontend\widgets\MultiLang\MultiLang; 


Content class frontend \ widgets \ MultiLang \ MultiLang.php

 <?php namespace frontend\widgets\MultiLang; use yii\helpers\Html; class MultiLang extends \yii\bootstrap\Widget { public $cssClass; public function init(){} public function run() { return $this->render('view', [ 'cssClass' => $this->cssClass, ]); } } 

Content view frontend \ widgets \ MultiLang \ views \ view.php

 <?php namespace frontend\widgets\MultiLang; use yii\helpers\Html; use Yii; ?> <div class="btn-group <?= $cssClass; ?>"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> <span class="uppercase"><?= Yii::$app->language; ?></span> <span class="caret"></span> </a> <ul class="dropdown-menu"> <li class="item-lang"> <?= Html::a('Go to English', array_merge( \Yii::$app->request->get(), [\Yii::$app->controller->route, 'language' => 'en'] )); ?> </li> <li class="item-lang"> <?= Html::a('  ', array_merge( \Yii::$app->request->get(), [\Yii::$app->controller->route, 'language' => 'ru'] )); ?> </li> </ul> </div> 

Conclusion

In general, the implementation of internationalization in Yii2 is not difficult, it turned out to be a simple widget, with three files.

UrlManager.php is taken from here . MultiLang.php simply renders the presentation. View.php view itself.

Source: https://habr.com/ru/post/317828/


All Articles