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

Implement Internationalization Support for Attribute Labels in Yii 2.0

如果[[yii\base\Application::$language|目标语言]]设置为zh-CN,则 打印出"用户手机"

1. Because I am generating some shared models in the common directory and have configured multiple database connection components, all models for kcucDb are prefixed with Kcuc to prevent naming conflicts (otherwise, adding the corresponding table prefix to the model name is not recommended), as shown in Figure 1:

All models for kcucDb are prefixed with Kcuc to prevent naming conflicts
All models for kcucDb are prefixed with Kcuc to prevent naming conflicts

2. Use Table Prefix: Check this to use a table prefix; Generate Labels from DB Comments: Do not check this (because manual translation will be handled later); Enable I18N: Check this to enable internationalization; Message Category: Set the message category to commonAttributeLabels/kcucMembers, which is the category used by Yii::t() for the attribute labels under the current model class when internationalization is enabled. After generation, it looks like Figure 2:

Generate Labels from DB Comments: Do not check this (because manual translation will be handled later)
Generate Labels from DB Comments: Do not check this (because manual translation will be handled later)

3. Configure the message translation in the application using the following code, as shown in Figure 3:

The pattern commonAttributeLabels* means that all message category names starting with commonAttributeLabels use this translation message source.

The pattern attributeLabels* means that all message category names starting with attributeLabels use this translation message source. Its root path is the current application.

The pattern * specifies the default translation. Its root path is the current application.

Configure the message translation in the application using the following code
Configure the message translation in the application using the following code

4. Create \common\messages\zh-CN\commonAttributeLabels\kcucMembers.php and \common\messages\zh-CN\not_specified_category.php, as shown in Figure 4:

Create \common\messages\zh-CN\commonAttributeLabels\kcucMembers.php and \common\messages\zh-CN\not_specified_category.php
Create \common\messages\zh-CN\commonAttributeLabels\kcucMembers.php and \common\messages\zh-CN\not_specified_category.php

5. Edit \common\messages\zh-CN\commonAttributeLabels\kcucMembers.php, as shown in Figure 5:

Edit the message translation file
Edit the message translation file

6. Tip: For zh-CN, you can first check Generate Labels from DB Comments, copy the public function attributeLabels() from the model class during Preview, and then edit it, as shown in Figure 6;

Tip: For zh-CN, you can first check Generate Labels from DB Comments, copy the public function attributeLabels() from the model class during Preview, and then edit it
Tip: For zh-CN, you can first check Generate Labels from DB Comments, copy the public function attributeLabels() from the model class during Preview, and then edit it

7. Test,
$model = new \common\models\KcucMembers;

// Displays “Mobile”
echo $model->getAttributeLabel(‘mobile’);

The default [[yii\base\Application::$sourceLanguage|source language]] value is en-US, meaning American English, which remains unchanged. If the [[yii\base\Application::$language|target language]] is not set, or is set to en-US, then
it prints “Mobile”, as shown in Figure 7:

If the [[yii\base\Application::$language|target language]] is not set, or is set to en-US, then it prints "Mobile"
If the [[yii\base\Application::$language|target language]] is not set, or is set to en-US, then
it prints “Mobile”

8. The default [[yii\base\Application::$sourceLanguage|source language]] value is en-US, meaning American English, which remains unchanged. If the [[yii\base\Application::$language|target language]] is set to zh-CN, then
it prints “用户手机”, as shown in Figure 8:

If the [[yii\base\Application::$language|target language]] is set to zh-CN, then it prints "用户手机"
If the [[yii\base\Application::$language|target language]] is set to zh-CN, then
it prints “用户手机”

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

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

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

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

关于我  ·  GitHub  ·  邮件联系