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

Implement Internationalization in Yii 2.0: Translate Part of an English Description to Chinese

Testing the translation result, "invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755" has been translated to: invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755. The blue box shows the English message before translation, and the red box shows the Chinese message after translation. Meets expectations

1. When requesting the WeChat API, the return code is 45028, and the returned English message is: has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c. See Figure 1.

When requesting the WeChat API, the return code is 45028, and the returned English message is: has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c
Figure 1
JSON

{
  "errcode": 45028,
  "errmsg": "has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c"
}

2. The code implementation is as follows.

PHP

$responseData = $wxServiceApi::wxMessageSendAll($requestData);
if (!isset($responseData['msg_id'])) {
    $errcode = array_key_exists('errcode',$responseData)?$responseData['errcode']:205100;
    throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '205100'), ['error_code' => $errcode, 'error_msg' => $responseData['errmsg']])), 205100);
}

3. I plan to translate the error message: has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c to: 没有群发配额。rid: 6103df2e-1505ba73-0eb14e1c. See Figure 2.

Plan to translate the error message: has no masssend quota rid: 6103df2e-1505ba73-0eb14e1c to: 没有群发配额。rid: 6103df2e-1505ba73-0eb14e1c
Figure 2

4. Add the files common/messages/en-US/wx.php, common/messages/zh-CN/wx.php, wx/messages/en-US/wx.php, and wx/messages/zh-CN/wx.php. See Figure 3.

Add the files common/messages/en-US/wx.php, common/messages/zh-CN/wx.php, wx/messages/en-US/wx.php, wx/messages/zh-CN/wx.php
Figure 3

common/messages/en-US/wx.php

PHP
<?php
return [
];

common/messages/zh-CN/wx.php

PHP
<?php
return [
    'has no masssend quota' => '没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。',
    'invalid content' => '无效内容(内容中包含音频)。',
];

wx/messages/en-US/wx.php

PHP
<?php
$commonMessages = require __DIR__ . '/../../../common/messages/en-US/wx.php';
$messages = [
];
return $commonMessages + $messages;

wx/messages/zh-CN/wx.php

PHP
<?php
$commonMessages = require __DIR__ . '/../../../common/messages/zh-CN/wx.php';
$messages = [
];
return $commonMessages + $messages;

5. The code implementation is as follows. The result of the code: substr($responseData[‘errmsg’], 0, $pos) is: has no masssend quota. The result of the code: substr($responseData[‘errmsg’], $pos) is: rid: 6103df2e-1505ba73-0eb14e1c. After translating “has no masssend quota” to “没有群发配额。”, it is then concatenated with: rid: 6103df2e-1505ba73-0eb14e1c.

PHP

$responseData = $wxServiceApi::wxMessageSendAll($requestData);
if (!isset($responseData['msg_id'])) {
    $errcode = $responseData['errcode'] ?? 205100;
    $pos = strpos($responseData['errmsg'], ' rid:');
    $errorMsg = Yii::t('wx', substr($responseData['errmsg'], 0, $pos)) . substr($responseData['errmsg'], $pos);
    throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '205100'), ['error_code' => $errcode, 'error_msg' => $errorMsg])), 205100);
}

6. For the returned English message: invalid content hint: [pzRHda0286d228] rid: 61041ffe-5cefd269-6c9611ff, the improved code is as follows. The root cause is that the source language messages do not support fuzzy matching.

PHP

$responseData = $wxServiceApi::wxMessageSendAll($requestData);
if (!isset($responseData['msg_id'])) {
    $errcode = $responseData['errcode'] ?? 205100;
    $errorMsg = '';
    $pos = strpos($responseData['errmsg'], ' hint:');
    if ($pos === false) {
        $pos = strpos($responseData['errmsg'], ' rid:');
    }
    if ($pos !== false) {
        $errorMsg = Yii::t('wx', substr($responseData['errmsg'], 0, $pos)) . substr($responseData['errmsg'], $pos);
    }
    throw new ServerErrorHttpException(Yii::t('error', Yii::t('error', Yii::t('error', '205100'), ['error_code' => $errcode, 'error_msg' => $errorMsg])), 205100);
}

7. Testing the translation result, “has no masssend quota rid: 610a50c5-26966176-7cceb46e” has been translated to: 没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。rid: 610a50c5-26966176-7cceb46e. This meets expectations. See Figure 4.

Testing the translation result, "has no masssend quota rid: 610a50c5-26966176-7cceb46e" has been translated to: 没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。rid: 610a50c5-26966176-7cceb46e。Meets expectations
Figure 4

8. Testing the translation result, “invalid content hint: [dN7TKA0474d139] rid: 610a5a7a-5907e6c6-270b8278” has been translated to: 无效内容(内容中包含音频)。 hint: [dN7TKA0474d139] rid: 610a5a7a-5907e6c6-270b8278. This meets expectations. See Figure 5.

Testing the translation result, "invalid content hint: [dN7TKA0474d139] rid: 610a5a7a-5907e6c6-270b8278" has been translated to: 无效内容(内容中包含音频)。 hint: [dN7TKA0474d139] rid: 610a5a7a-5907e6c6-270b8278。Meets expectations
Figure 5

9. Since “invalid image size” has not yet been added to the language pack, “invalid image size hint: [cl_6Oa0457mp13] rid: 6103c6d0-481a4bed-73e7120a” remains unchanged. This meets expectations.

PHP

 '没有群发配额(订阅号为每天 1 次,服务号为每月 4 次)。',
    'invalid content' => '无效内容(内容中包含音频)。',
    'invalid image size' => '图片尺寸太大(大小必须在10MB以下)。',
];


11. Testing the translation result, “invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755” has been translated to: invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755. The blue box shows the English message before translation, and the red box shows the Chinese message after translation. This meets expectations. See Figure 6.

Testing the translation result, "invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755" has been translated to: invalid image size hint: [flp8Ba0298w689] rid: 610a6969-72bb79c0-4b069755. The blue box shows the English message before translation, and the red box shows the Chinese message after translation. Meets expectations
Figure 6

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

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

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

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

关于我  ·  GitHub  ·  邮件联系