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>


