配置

  1. <?php
  2. return [
  3. 'language' => 'zh-CN',
  4. 'components' => [
  5. 'i18n' => [
  6. 'translations' => [
  7. 'app*' => [
  8. 'class' => 'yii\i18n\PhpMessageSource',
  9. 'basePath' => '@common/i18n',
  10. // https://www.yiichina.com/doc/guide/2.0/tutorial-i18n#message-translation
  11. // 消息翻译服务用于将一条文本信息从一种语言(通常是 源语言 ) 翻译成另一种语言(通常是 目标语言)。
  12. // 源语言 sourceLanguage
  13. // 目标语言 language
  14. 'sourceLanguage' => 'en-US',
  15. 'fileMap' => [
  16. 'app' => 'app.php',
  17. 'app/error' => 'error.php',
  18. ],
  19. ],
  20. ],
  21. ],
  22. ],
  23. ];

翻译信息

common\i18n\zh-CN\app.php

  1. <?php
  2. return [
  3. 'welcome' => '欢迎'
  4. ];

使用

  1. echo \Yii::t('app', 'welcome');