Best Practices Configured in Message Catalogs (Messages) when implementing RESTful Web Services in Yii 2 Advanced Project Template
1. The previous implementation is as follows, the configuration file in the public directory, \common\config\main.php, the international configuration is as follows:
'components' => [
'i18n' => [
'translations' => [
'common/*' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true,
'basePath' => '@common/messages',
'fileMap' => [
'common/app' => 'app.php',
'common/error' => 'error.php',
'common/success' => 'success.php',
],
],
'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. The files in the message directory (messages) in the public directory are as follows:
\common\messages\zh-cn\error.php
'error',
201001 => '框架服务控制台HTTP请求失败,状态码:{status_code}',
201002 => '框架服务控制台HTTP请求失败:{first_error}',
201003 => '框架服务控制台HTTP请求失败:{message}',
201004 => '框架服务接口HTTP请求失败,状态码:{status_code}',
201005 => '框架服务接口HTTP请求失败:{first_error}',
];
\common\messages\zh-cn\error.php
'success',
];
3. The files in the message directory (Messages) in the interface application are as follows:
\API\Messages\zh-cn\Error.php
'error',
224001 => '日志列表为空',
224002 => '日志ID:{id},不存在',
224003 => '数据过滤器验证失败:{first_error}',
224004 => '用户ID:{id},的状态为已禁用',
224005 => '页面列表为空',
224006 => '页面ID:{id},不存在',
224007 => '页面ID:{id},的状态为已删除',
224008 => '页面ID:{id},的状态为已禁用',
224009 => '页面ID:{id},的状态为草稿',
226001 => '用户列表为空',
226002 => '用户ID:{id},不存在',
226003 => '用户ID:{id},的状态为已删除',
226004 => '数据验证失败:{first_error}',
];
\API\Messages\zh-cn\success.php
'success',
124001 => '获取日志列表成功',
124002 => '获取日志详情成功',
124003 => '获取页面列表成功',
124004 => '获取页面详情成功',
124005 => '创建页面成功',
124006 => '更新页面成功',
124007 => '删除页面成功',
126001 => '获取用户列表成功',
126002 => '获取用户详情成功',
126003 => '创建用户成功',
126004 => '更新用户成功',
126005 => '删除用户成功',
];
4. The yii::t() method in the public directory is implemented as follows:
throw new ServerErrorHttpException(Yii::t('common/error', Yii::t('common/error', Yii::t('common/error', '201001'), ['status_code' => $response->getStatusCode()])), 201001);
5. The yii::t() method in the interface application is implemented as follows:
return ['code' => 226004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '226004'), ['first_error' => $firstError]))];
6. The problems existing in the previous implementation are as follows:
(1) The yii::t() method in the public directory implements yii::t(common/errorThere is redundancy, looking forward to yii::t(ErrorRealize
(2) If you want to include the message to be translated in the public directory in the interface application, the yii::t() method must implement yii::t(common/error, to include the message to be translated in the interface application, the yii::t() method must implement yii::t(Error, it is necessary to clearly know the location of the news to be translated, there is a great possibility of error, and the implementation is not pure enough, and it is mixed
7. Based on the existing problems, the solution is adjusted as follows, the configuration file in the public directory, the adjustment of the configuration of the I18n application component (message category: common/*), \common\config\main.php, the international configuration 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',
],
],
],
],
],
8. The files in the message directory (messages) in the public directory are not adjusted and remain as they are
9. The files in the message directory (messages) in the interface application are as follows:
\API\Messages\zh-cn\Error.php
'日志列表为空',
224002 => '日志ID:{id},不存在',
224003 => '数据过滤器验证失败:{first_error}',
224004 => '用户ID:{id},的状态为已禁用',
224005 => '页面列表为空',
224006 => '页面ID:{id},不存在',
224007 => '页面ID:{id},的状态为已删除',
224008 => '页面ID:{id},的状态为已禁用',
224009 => '页面ID:{id},的状态为草稿',
226001 => '用户列表为空',
226002 => '用户ID:{id},不存在',
226003 => '用户ID:{id},的状态为已删除',
226004 => '数据验证失败:{first_error}',
];
return $commonMessages + $messages;
\API\Messages\zh-cn\success.php
'获取日志列表成功',
124002 => '获取日志详情成功',
124003 => '获取页面列表成功',
124004 => '获取页面详情成功',
124005 => '创建页面成功',
124006 => '更新页面成功',
124007 => '删除页面成功',
126001 => '获取用户列表成功',
126002 => '获取用户详情成功',
126003 => '创建用户成功',
126004 => '更新用户成功',
126005 => '删除用户成功',
];
return $commonMessages + $messages;
10. The yii::t() method in the public directory is implemented as follows:
throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->getStatusCode()])), 201001);
11. The yii::t() method in the interface application is implemented as follows:
return ['code' => 226004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '226004'), ['first_error' => $firstError]))];
12. The problems existing in the previous implementation have been solved, as follows:
(1) The yii::t() method in the public directory implements yii::t(Error, only need to ensure that the corresponding code exists in the message directory (messages) in the public directory
(2) If you want to include the message to be translated in the public directory in the interface application, the yii::t() method must implement yii::t(Error, to include the message to be translated in the interface application, the yii::t() method must implement yii::t(Error, you don’t need to clearly know the location of the message to be translated, both the message directory (messages) in the public directory and interface applications can be used
(3) A small problem that exists, if there is the same code in the public directory and the interface application, the value in the interface application will not overwrite the value in the public directory, because the message file in the interface application is not used array_merge method, because the array index is a number, if the array_merge method is used, the numeric key name will be renumbered, and the index will start from 0, so keep the original array and only want the new array to be attached to the back, use + operator, if additional, when the same key name exists in the two arrays, the element with the same key name in the first array will be retained, and the element in the second array will be ignored