没有不值得去解决的问题,也没有不值得去学习的技术!

Best Practices for Configuring the Message Catalog When Implementing RESTful Web Services in the Yii 2 Advanced Project Template

1. The previous implementation was as follows. In the configuration file within the common directory, \common\config\main.php, the internationalization configuration was as follows:

PHP

    '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) within the common directory are as follows:

\common\messages\zh-CN\error.php

PHP
<?php
return [
    20000 => '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

PHP
<?php
return [
    10000 => 'success',
];

3. The files in the message directory (messages) within the API application are as follows:

\api\messages\zh-CN\error.php

PHP
<?php
return [
    20000 => '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

PHP
<?php
return [
    10000 => 'success',
    124001 => '获取日志列表成功',
    124002 => '获取日志详情成功',
    124003 => '获取页面列表成功',
    124004 => '获取页面详情成功',
    124005 => '创建页面成功',
    124006 => '更新页面成功',
    124007 => '删除页面成功',
    126001 => '获取用户列表成功',
    126002 => '获取用户详情成功',
    126003 => '创建用户成功',
    126004 => '更新用户成功',
    126005 => '删除用户成功',
];

4. The Yii::t() method implementation in the common directory is as follows:

PHP

	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 implementation in the API application is as follows:

PHP

	return ['code' => 226004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '226004'), ['first_error' => $firstError]))];

6. The problems with the previous implementation are as follows:

(1) The Yii::t() method implementation in the common directory uses Yii::t(‘common/error’, which is redundant; the expected implementation is Yii::t(‘error’.
(2) In the API application, to include messages to be translated from the common directory, the Yii::t() method must implement Yii::t(‘common/error’, and to include messages to be translated from the API application, the Yii::t() method must implement Yii::t(‘error’. This requires explicitly knowing the location of the message to be translated, which carries a high probability of error. Furthermore, the implementation is not clean and is somewhat mixed.

7. Based on the existing problems, the solution is adjusted as follows. In the configuration file within the common directory, the i18n application component configuration is adjusted (message category: remove common/*), \common\config\main.php, and the internationalization configuration is as follows:

PHP

    '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) within the common directory remain unchanged and are kept as is.

9. The files in the message directory (messages) within the API application are as follows:

\api\messages\zh-CN\error.php

PHP
<?php
$commonMessages = require __DIR__ . '/../../../common/messages/zh-CN/error.php';
$messages = [
    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}',
];
return $commonMessages + $messages;

\api\messages\zh-CN\success.php

PHP
<?php
$commonMessages = require __DIR__ . '/../../../common/messages/zh-CN/success.php';
$messages = [
    124001 => '获取日志列表成功',
    124002 => '获取日志详情成功',
    124003 => '获取页面列表成功',
    124004 => '获取页面详情成功',
    124005 => '创建页面成功',
    124006 => '更新页面成功',
    124007 => '删除页面成功',
    126001 => '获取用户列表成功',
    126002 => '获取用户详情成功',
    126003 => '创建用户成功',
    126004 => '更新用户成功',
    126005 => '删除用户成功',
];
return $commonMessages + $messages;

10. The Yii::t() method implementation in the common directory is as follows:

PHP

	throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '201001'), ['status_code' => $response->getStatusCode()])), 201001);

11. The Yii::t() method implementation in the API application is as follows:

PHP

	return ['code' => 226004, 'message' => Yii::t('error', Yii::t('error', Yii::t('error', '226004'), ['first_error' => $firstError]))];

12. The problems with the previous implementation have now been resolved, as follows:

(1) The Yii::t() method implementation in the common directory uses Yii::t(‘error’; it only needs to be ensured that the corresponding code exists in the message directory (messages) within the common directory.
(2) In the API application, to include messages to be translated from the common directory, the Yii::t() method must implement Yii::t(‘error’, and to include messages to be translated from the API application, the Yii::t() method must implement Yii::t(‘error’. There is no need to explicitly know the location of the message to be translated; both the message directory (messages) in the common directory and in the API application can be used.
(3) There is one minor issue: if the same code exists in both the common directory and the API application, the value in the API application will not overwrite the value in the common directory. The reason is that the message files in the API application do not use the array_merge method. Because the array indices are numeric, if the array_merge method were used, the numeric keys would be renumbered, and the index would start from 0. Therefore, to preserve the original array and only append the new array to the end, the + operator is used. When appending, if the same key name exists in both arrays, the element with that key name in the first array will be retained, and the element in the second array will be ignored.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系