Home > Article > PHP Framework > How to achieve internationalization in yii2
Internationalization refers to designing software so that it can adapt to the needs of different languages and regions without making major changes. This is of particular importance to our website because potential users Probably on a global scale. The internationalization function (i18n component) provided by Yii supports comprehensive information translation, view translation, date and number formatting.
Because there is such a convenient setting of international service, when we need to implement a website to be displayed in different languages, but do not want to do too much processing It seems very convenient. Let’s talk about how to achieve this requirement. (Recommended learning: yii tutorial)
Configure i18n components into the configuration file
You need to use components in yii2, the first thing Of course, just add the configuration of the component in the configuration file.
'language' => 'ru-RU','components' => [ // ... 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', //'basePath' => '@app/messages', //'sourceLanguage' => 'en-US', 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', ], ], ], ],],
The above is the configuration in the development document. The key to the settings here lies in the two language settings, namely sourceLanguage (source language) and language (target language) settings. This translation service is the implementation of translating the website from the source language to the target language, and the target language can be changed at any time. .
// 改变目标语言为中文\Yii::$app->language = 'zh-CN';
The default source language is American English, and the basePath parameter is the location of the mapping file. @app refers to the root directory. If it is placed elsewhere, such as under the backend module, change it to
'basePath' => '@backend/messages',
The above is the detailed content of How to achieve internationalization in yii2. For more information, please follow other related articles on the PHP Chinese website!