Yii 本地化
I18N(国际化) 是设计可适用于各种语言的应用程序的过程。Yii提供全系列的I18N功能。
区域设置是一组指定用户的语言和国家的参数。例如,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' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
],
'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 Russian
'language' => 'ru-RU',
// 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 / ru-RU / app.php文件中。
第3步 - 现在,创建消息/ ru-RU目录结构。 在RU-RU文件夹内创建一个名为app.php的文件。这将存储所有EN→RU翻译。
<?php
return [
'This is a string to translate!' => 'Эта строка для перевода!'
];
?>
第4步 - 在SiteController中创建一个名为actionTranslation()的函数。
public function actionTranslation() {
echo \Yii::t('app', 'This is a string to translate!');
}
第5步 - 在Web浏览器中输入URL http:// localhost:8080 / index.php?r =网站/翻译 ,您将看到以下内容。

当我们将目标语言设置为RU-RU时,该消息被翻译成俄语。我们可以动态更改应用程序的语言。
第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 = 'Vladimir';
// display a translated message with username being "Vladimir"
echo \Yii::t('app', 'Hello, {username}!', [
'username' => $username,
]), "<br>";
$username = 'John';
// 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]);
}
以下将是输出。

您可以翻译整个视图脚本,而不是翻译单个文本消息。例如,如果目标语言是RU-RU,并且您想翻译views / site / index.php视图文件,则应该翻译视图并将其保存在views / site / ru-RU目录下。
第8步 - 创建views / site / ru-RU目录结构。 然后,在RU-RU文件夹内用下面的代码创建一个名为index.php的文件。
<?php
/* @var $this yii\web\View */
$this->title = 'My Yii Application';
?>
<div class = "site-index">
<div class = "jumbotron">
<h1>Добро пожаловать!</h1>
</div>
</div>
第9步 - 目标语言是ru-RU,所以如果输入URL http:// localhost:8080 / index.php?r = site / index ,您将会看到带有俄语翻译的页面。

下一章:Yii Gii
Gii是扩展,它提供了一个基于Web的代码生成器来生成模型,表单,模块,CRUD等等。默认情况下,以下生成器可用 -模型生成器 - 为指定的数据库表生成一个ActiveRecord类。CRUD Generator - 生成一 ...
AI 中文社