I18N(國際化)是設計一種能夠適應於各種語言的應用程式的過程。 Yii 中提供了國際化支持。
語言環境(Locale)是一組參數用來指定用戶的語言和國家。例如,en-US代表的是英語語言環境和國家是美國。
Yii提供兩種類型的語言:源語言和目標語言。源語言是在應用程式中的所有文本消息的語言。目標語言是應用於顯示內容到最終用戶的語言。
消息轉換組件是可從源語言到目標語言翻譯(轉換)的文本消息。
要翻譯文本消息,文本消息翻譯服務必須在消息源中找到它。
要使用文本消息翻譯服務,應該 -
-
包裹想要翻譯文本消息在 Yii::t() 方法中
- 配置文本消息源
- 儲存文本消息在消息源中
第1步 - Yii::t() 方法可以像這樣使用,如下所示:
echo \Yii::t('app', 'This is a message to translate!');
在上面的代碼片段中,'app' 代表文本消息的類別
第2步 - 現在,修改 config/web.php 檔。
<?php $params = require(__DIR__ . '/params.php'); $config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this //is required by cookie validation 'cookieValidationKey' => 'the-cookie-validate-key-of-xuhuhu.com', ], 'cache' => [ 'class' => 'yii\caching\FileCache', ], 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'fileMap' => [ 'app' => 'app.php' ], ], ], ], 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, ], 'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], 'log' => [ 'flushInterval' => 1, 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'exportInterval' => 1, 'logVars' => [], ], ], ], 'db' => require(__DIR__ . '/db.php'), ], // set target language to be Chinese 'language' => 'zh-CN', // set source language to be English 'sourceLanguage' => 'en-US', 'modules' => [ 'hello' => [ 'class' => 'app\modules\hello\Hello', ], ], 'params' => $params, ]; if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii\debug\Module', ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', ]; } return $config; ?>
在上面的代碼中,我們定義了源和目標語言。
還指定是由 yii\i18n\PhpMessageSource 來支持消息源。
app* 模式表示所有消息開始程式類別必須使用這種特殊的消息源進行翻譯(轉換)。
在上面的配置中,所有中文翻譯將設在 messages/zh-CN/app.php 檔。


第3步 - 現在,創建 messages/zh-CN 的目錄結構。在 zh-CN 檔夾中創建一個檔:app.php 。這將存儲所有 EN → CN 的翻譯文本消息。
<?php return [ 'This is a string to translate' => '這是一個翻譯的字串' ]; ?>
第4步 - 在SiteController 中創建一個 actionTranslation() 函數。
public function actionTranslation() { echo \Yii::t('app', 'This is a string to translate'); }
該文本消息會被翻譯成中文,因為設定的目標語言是 zh-CN。我們也可以動態地改變應用程式的語言。
第6步 - 修改 actionTranslation() 方法,如下代碼。
public function actionTranslation() { \Yii::$app->language = 'en-US'; echo \Yii::t('app', 'This is a string to translate!'); }
現在,以英文顯示文本消息 -


第7步 - 在一個轉換(翻譯)的消息,可以插入一個或多個參數。
public function actionTranslation() { $username = 'Username1'; // display a translated message with username being "Vladimir" echo \Yii::t('app', 'Hello, {username}!', [ 'username' => $username, ]), "<br>"; $username = 'username2'; // display a translated message with username being "John" echo \Yii::t('app', 'Hello, {username}!', [ 'username' => $username, ]), "<br>"; $price = 150; $count = 3; $subtotal = 450; echo \Yii::t('app', 'Price: {0}, Count: {1}, Subtotal: {2}', [$price, $count, $subtotal]); }
以下是輸出結果:


可以翻譯整個視圖腳本,而不是單獨的翻譯文本消息。例如,如果目標語言是zh-CN,想翻譯是 views/site/index.php 視圖檔,
那麼應該翻譯視圖並保存在 views/site/zh-CN 目錄下。
第8步 - 創建 views/site/zh-CN 目錄。
然後,zh-CN 檔夾中創建一個 index.php 檔並使用下麵的代碼。
<?php /* @var $this yii\web\View */ $this->title = 'My Yii Application'; ?> <div class = "site-index"> <div class = "jumbotron"> <h1>歡迎您訪問!</h1> </div> </div>