Configuration Tutorial for Internationalization of Yii2 Language

  • 2021-10-27 06:44:38
  • OfStack

Preface

Recently want to make a blog to support multi-language, but also Yii2 support this function, so looked at the next official document, hey, see half a day do not know why to use, so a variety of Baidu, Google search, finally understand the original is very simple, but the official writing is too complex

Below the specific use of the steps, specific introduction I do not write, the official write than I am clear, I write how to use

Step 1 Create an i18n configuration file


./yii message/config @app/config/i18.php // yii  In the project directory  Yii2 Automatically generated when created 

After executing the command, an i18n. php file is created under the project root config

Why do you want to create this file, because we need to generate a corresponding mapping file for multilingual processing, just generate it, and the configuration program will automatically call the processing later

Step 2 Modify Configuration Rules

Open config/i18n. php and look at the code for the generated configuration file, as follows:


return [
 'color' => null,
 'interactive' => true,
 'help' => null,
 'sourcePath' => '@yii',
 'messagePath' => '@yii/messages',
 'languages' => [],
 'translator' => 'Yii::t',
 'sort' => false,
 'overwrite' => true,
 'removeUnused' => false,
 'markUnused' => true,
 'except' => [
  '.svn',
  '.git',
  '.gitignore',
  '.gitkeep',
  '.hgignore',
  '.hgkeep',
  '/messages',
  '/BaseYii.php',
 ],
 'only' => [
  '*.php',
 ],
 'format' => 'php',
 'db' => 'db',
 'sourceMessageTable' => '{{%source_message}}',
 'messageTable' => '{{%message}}',
 'catalog' => 'messages',
 'ignoreCategories' => [],
 'phpFileHeader' => '',
 'phpDocBlock' => null,
];

The modified code is as follows:


return [
 'color' => null,
 'interactive' => true,
 'help' => null,
 'sourcePath' => '@app',
 'messagePath' => '@app/messages',
 'languages' => ['zh-CN', 'ru-RU'],
 'translator' => 'Yii::t',
 'sort' => false,
 'overwrite' => true,
 'removeUnused' => false,
 'markUnused' => true,
 'except' => [
  '.svn',
  '.git',
  '.gitignore',
  '.gitkeep',
  '.hgignore',
  '.hgkeep',
  '/messages',
  '/BaseYii.php',
  'vendor',
 ],
 'only' => [
  '*.php',
 ],
 'format' => 'php',
 'db' => 'db',
 'sourceMessageTable' => '{{%source_message}}',
 'messageTable' => '{{%message}}',
 'catalog' => 'messages',
 'ignoreCategories' => [],
 'phpFileHeader' => '',
 'phpDocBlock' => null,
];

I've only changed two places here


'sourcePath' => '@app', //  Will @yii Replace with @app  Only deal with the code in our own application 
'messagePath' => '@app/messages', //  Will @yii/messages Replace with @app/messages  Extract the fields to be translated and put them in the directory 
'languages' => ['zh-CN', 'ru-RU'], //  To translate into the target language, I define here 1 A " Chinese " And " Russian "

And


'except' => [
 '.svn',
 '.git',
 '.gitignore',
 '.gitkeep',
 '.hgignore',
 '.hgkeep',
 '/messages',
 '/BaseYii.php',
 'vendor', //  Will vendor Filter out the directory, otherwise there may be too many 
],

Step 3 Generate the translation configuration file

Execute the following command


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

After execution, the following directory structure will be obtained under the messages directory (if there is no messages directory, it needs to be created manually)

-ru-RU
The-app. php
zh-CN
app. php

Before doing this again, you need to have a call like Yii: t () in your own project, for example, in the file components/HeaderWidget. php


Yii::t('app', 'Home')

The function of app here is used to classify files. I have no plan to put all the fields that need to be translated in the php file named at the beginning of app. php file

If you call it like this


Yii::t('appp', 'Home')

A file of appp. php will be generated

Step 4 Translate the configuration file

Look at the Chinese translation file messages/zh-CN/app. php. Mine is the following


return [
 'Archive' => '',
 'Autokid' => '',
 'Blog' => '',
 'Ctime' => '',
 'IP Address ' => '',
 'UserAgent' => '',
 ' Theme ' => '',
 ' Content ' => '',
 ' Name ' => '',
 ' Date ' => '',
 ' Email address ' => '',
 ' Page path ' => '',
 'Home' => ' Home page ', //  The key value on the right corresponds to  Yii::t('app', 'Home') In Home , just need to be in value Write the required Chinese characters in. 
];

Step 5 Modify the target internationalization language

Modify the configuration file


'language' => 'zh-CN', //  Specify as the language to translate 

If you open the web page, you can see that it has been translated into the corresponding language. Of course, this configuration is very inflexible. If you deploy polymorphic machines and implement them through domain names or other ways, it is also possible. Here I suggest the following ways

Create your own Controller, then put the language configuration in Session, and replace the language of the whole station by obtaining the language in Session. See the sharing later for details

Tip, many places on the website mention that it is necessary to add a configuration, and so is the official


return [
 'color' => null,
 'interactive' => true,
 'help' => null,
 'sourcePath' => '@yii',
 'messagePath' => '@yii/messages',
 'languages' => [],
 'translator' => 'Yii::t',
 'sort' => false,
 'overwrite' => true,
 'removeUnused' => false,
 'markUnused' => true,
 'except' => [
  '.svn',
  '.git',
  '.gitignore',
  '.gitkeep',
  '.hgignore',
  '.hgkeep',
  '/messages',
  '/BaseYii.php',
 ],
 'only' => [
  '*.php',
 ],
 'format' => 'php',
 'db' => 'db',
 'sourceMessageTable' => '{{%source_message}}',
 'messageTable' => '{{%message}}',
 'catalog' => 'messages',
 'ignoreCategories' => [],
 'phpFileHeader' => '',
 'phpDocBlock' => null,
];
0

My side did not add when configuring, and the operation is normal. If you encounter problems, you can communicate with each other.

Summarize


Related articles: