Detailed Explanation of Internationalization Automatic Configuration of Yii2 Language

  • 2021-11-01 02:41:03
  • OfStack

How to realize international automation of language, You may think that automation, Is it automatic switching without configuration? I have thought about this idea before. Can I judge the geographical location according to IP and then determine its language? I have found an "IpToCountry" related on the Internet. If you are interested, you can search it out and see. He will provide an ip comparison table, which will be updated once every 1 period of time, but this is not done for the time being. Consider trying it later

Here we talk about how to implement Yii2, because we shared the configuration of language internationalization in the above article, and also translated the corresponding language. The following is the need to switch according to the conditions

From the configuration file, we know that we only need to change the configuration value of language, but where to change it. My operation steps here are as follows

I want the whole of the project for all the content of the language internationalization, only 1 I can think of is the modification of the controller, some say is the modification of the entry file, I think the modification of the entry file is a bit of a damage to the framework structure.

Step 1 Create an AppController

Inherit yii/web/Controller and implement as follows


<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

class AppController extends Controller
{
  public function init()
  {
    if (isset(Yii::$app->session['_lang'])) {
      Yii::$app->language = Yii::$app->session['_lang'];
    }

    parent::init();
  }
}

Step 2 Inherit AppController from all your own controllers

To do something like the following, I just gave an example of BlogController here


class BlogController extends AppController

The third step is to implement the processing of setting multilingual

I have added the following Action to my controller SiteController here


public function actionLanguage($language)
{
  Yii::$app->session['_lang'] = $language;
  $redirectUrl = Yii::$app->request->headers['Referer'];
  if (!$redirectUrl) {
    $redirectUrl = Yii::$app->homeUrl;
  }
  return $this->redirect($redirectUrl);
}

Every time you need to modify the language, as long as the language is passed into actionLanguage, the value of _ lang in session will be changed, and then each controller will first change the language of the project when calling

Step 4 Modification of Front End UI

By adding a modified logic in the front end, it is convenient for front-end visitors to change language settings


<ul class="nav navbar-nav navbar-right">
 <li class="dropdown">
  <a href="j#" rel="external nofollow" data-target="#" class="dropdown-toggle" data-toggle="dropdown">
   {{ Yii.t('app', 'Language') }}
   <b class="caret"></b> 
  </a>
  <ul class="dropdown-menu">
   <li
    class="{% if app.session['_lang'] == 'zh-CN' %}active{% endif %}"
   >
    <a
     href="{{ url(['site/language'], { 'language': 'zh-CN' })}}" rel="external nofollow" 
    >{{ Yii.t('app', 'Chinese') }}</a>
   </li>
   <li
    class="{% if app.session['_lang'] == 'en-US' %}active{% endif %}"
   >
    <a 
     href="{{ url(['site/language'], { 'language': 'en-US' })}}" rel="external nofollow" 
    >{{ Yii.t('app', 'English') }}</a>
   </li>
  </ul>
 </li>
</ul>

The Twig template I use here.

It's all set up here, and you can switch languages normally. If you add new content later, you can directly execute the command mentioned above


./yii message/extract @app/config/i18n.php

If you don't understand something, you can add a group to know it in detail

ps: Generating a mapping directory using yii


./yii message/extract @app/config/i18n.php

Using the above commands is essentially a scan operation that takes out all xxx in statements like Yii:: t ('app', 'xxx') and puts them into the mapping file according to the parameters given in i 18n. php.

In this case, zh-CN folder will be generated in common/messages, and app. php is the mapping file.

app.php


return [
  'OpenId' => 'OpenId',
  'UserId' => ' Users id',
  'UserName' => ' User name ',
  'UserPortrait' => ' User avatar ',
  'About' => ' About us ',
  'Are you sure you want to delete this item?' => ' Are you sure you want to delete? ',
  'Contact' => ' Contact information ',
  'Create' => ' Create ',
  'Create Myuser' => ' Create a user ',
  'Delete' => ' Delete ',
  'Home' => ' Home page ',
  'Login' => ' Login ',
  'My Company' => ' My website ',
  'Myusers' => ' Users ',
  'Reset' => ' Reset ',
  'Search' => ' Search ',
  'Update' => ' Update ',
  'Update {modelClass}: ' => ' Update users -',
];

Related articles: