Yii 2 Advanced Project Template, the bug of internationalized messaging service Analysis of @app/messages/en-us/app.php)
1. The configuration code of the I18N application components is as follows:
'components' => [
'i18n' => [
'translations' => [
'model/*' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true,
'basePath' => '@common/messages',
],
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true,
'basePath' => '@app/messages',
'fileMap' => [
'app' => 'app.php',
'error' => 'error.php',
'success' => 'success.php',
],
],
],
],
],
2. Request the interface in Postman, in headers, accept-language:zh-cn, when the target language is Simplified Chinese, and the category name of the file is: success, it is mapped to the php file @app/messages/zh-cn/success.php, as shown in Figure 1
return ['code' => 10000, 'message' => Yii::t('success', '127016'), 'data' => ['items' => array_values($models)]];
3. Request the interface in Postman, in headers, accept-language:zh-cn, when the target language is Simplified Chinese, and the category name of the file is: APP, it is mapped to the PHP file @app/messages/zh-cn/app.php, as shown in Figure 2
$planLog->opinion = Yii::t('app', Yii::t('app', Yii::t('app', 302016), ['accepted_group_name' => $acceptedGroupName, 'accepted_user_nick' => $identity->user_nick]));
4. Request the interface in Postman, in headers, accept-language:en-us, when the target language is American English, and the category name of the file is mapped to: success, it is mapped to the php file @app/messages/en-us/success.php, as shown in Figure 3
5. Request the interface in Postman, in headers, accept-language:en-us, when the target language is American English, and the category name of the file is: APP, it is not mapped to the PHP file @app/messages/en-us/app.php, the information to be translated is not found in the language file, and the original untranslated information will be returned, namely: 302011. as shown in Figure 4
6. Edit e:\wwwroot\pcs-api\vendor\yiisoft\yii2\i18n\messagesource.php, when the target language is simplified Chinese, response: 11, When the target language is American English, the response: 21, that is, when the target language is American English and the file-mapping category is: APP, the step of translating the specified message is skipped. As shown in Figure 5, Figure 6
Yii::t('app', '302011');
exit;
public function translate($category, $message, $language)
{
if ($this->forceTranslation || $language !== $this->sourceLanguage) {
echo 1;
exit;
return $this->translateMessage($category, $message, $language);
}
echo 2;
exit;
return false;
}
7. When the target language is American English, and the category name of the file map is: APP, the value of $this->FORCETranslation is FALSE, as shown in Figure 7
public function translate($category, $message, $language)
{
var_dump($this->forceTranslation);
exit;
if ($this->forceTranslation || $language !== $this->sourceLanguage) {
echo 1;
exit;
return $this->translateMessage($category, $message, $language);
}
echo 2;
exit;
return false;
}
8. When the target language is American English and the category name of the file map is: SUCCESS, the value of $this->FORCETranslation is true, as shown in Figure 8
Yii::t('success', '302011');
exit;
9. Check \vendor\yiisoft\yii2\i18n\i18n.php, the category name of the file mapping: yii, app, always define the category yii and app . The former refers to the message framework code used in the YII core, while the latter refers to the default message category of the custom application code. Its judgment category: whether the app exists, if it does not exist, it will be covered, but the hierarchy of its judgment is only the first level, and there is no deeper level. Therefore, the custom category: the app will be overwritten, as shown in Figure 9
/**
* @var array list of [[MessageSource]] configurations or objects. The array keys are message
* category patterns, and the array values are the corresponding [[MessageSource]] objects or the configurations
* for creating the [[MessageSource]] objects.
*
* The message category patterns can contain the wildcard `*` at the end to match multiple categories with the same prefix.
* For example, `app/*` matches both `app/cat1` and `app/cat2`.
*
* The `*` category pattern will match all categories that do not match any other category patterns.
*
* This property may be modified on the fly by extensions who want to have their own message sources
* registered under their own namespaces.
*
* The category `yii` and `app` are always defined. The former refers to the messages used in the Yii core
* framework code, while the latter refers to the default message category for custom application code.
* By default, both of these categories use [[PhpMessageSource]] and the corresponding message files are
* stored under `@yii/messages` and `@app/messages`, respectively.
*
* You may override the configuration of both categories.
*/
public $translations;
/**
* Initializes the component by configuring the default message categories.
*/
public function init()
{
parent::init();
if (!isset($this->translations['yii']) && !isset($this->translations['yii*'])) {
$this->translations['yii'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@yii/messages',
];
}
if (!isset($this->translations['app']) && !isset($this->translations['app*'])) {
$this->translations['app'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => Yii::$app->sourceLanguage,
'basePath' => '@app/messages',
];
}
}
10. The solution is as follows. The category name of the file mapping: the app is adjusted to the application, which is mapped to the php file application.php, and the configuration code of the adjustment i18n application component is as follows:
'components' => [
'i18n' => [
'translations' => [
'model/*' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true,
'basePath' => '@common/messages',
],
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true,
'basePath' => '@app/messages',
'fileMap' => [
'application' => 'application.php',
'error' => 'error.php',
'success' => 'success.php',
],
],
],
],
],
11. Rename the file \common\messages\en-us\app.php for \common\messages\en-us\application.php, rename the file \common\messages\zh-cn\app.php is \common\messages\zh-cn\application.php, rename the file \api\messages\en-us\app.php For \api\messages\en-us\application.php, rename the file \api\messages\zh-cn\app.php for \api\messages\zh-cn\application.php. This is done in other application directories. will yii::t(app, batch is replaced with: yii::t(Application, . As shown in Figure 10

Rename the file \common\messages\en-us\app.php for \common\messages\en-us\application.php, rename the file \common\messages\zh-cn\app.php is \common\messages\zh-cn\application.php, rename the file \api\messages\en-us\app.php For \api\messages\en-us\application.php, rename the file \api\messages\zh-cn\app.php for \api\messages\zh-cn\application.php. This is done in other application directories. will yii::t(app, batch is replaced with: yii::t(Application,
12. When the target language is American English and the category name of the file map is: application, it has been mapped to the php file @app/messages/en-us/application.php, as shown in Figure 11









