- Yii 教程
- Yii - 主页
- Yii - 概述
- Yii - 安装
- Yii - 创建页面
- Yii - 应用程序结构
- Yii - 入口脚本
- Yii - 控制器
- Yii - 使用控制器
- Yii - 使用动作
- Yii - 模型
- Yii - 小部件
- Yii - 模块
- Yii - 视图
- Yii - 布局
- Yii - 资产
- Yii - 资产转换
- Yii - 扩展
- Yii - 创建扩展
- Yii - HTTP 请求
- Yii - 响应
- Yii - URL 格式
- Yii - URL 路由
- Yii - URL 规则
- Yii - HTML 表单
- Yii - 验证
- Yii - 临时验证
- Yii - AJAX 验证
- Yii - 会话
- Yii - 使用闪存数据
- Yii - cookie
- Yii - 使用 Cookie
- Yii - 文件上传
- Yii - 格式化
- Yii - 分页
- Yii - 排序
- Yii - 属性
- Yii - 数据提供者
- Yii - 数据小部件
- Yii - 列表视图小部件
- Yii - GridView 小部件
- Yii - 活动
- Yii - 创建事件
- Yii - Behave
- Yii - 创建Behave
- Yii - 配置
- Yii - 依赖注入
- Yii - 数据库访问
- Yii - 数据访问对象
- Yii - 查询生成器
- Yii - 活动记录
- Yii - 数据库迁移
- Yii - 主题化
- Yii - RESTful API
- Yii - RESTful API 的实际应用
- Yii - 字段
- Yii - 测试
- Yii - 缓存
- Yii - 片段缓存
- Yii - 别名
- Yii - 日志记录
- Yii - 错误处理
- Yii - 身份验证
- Yii - 授权
- Yii - 本地化
- Yii-Gii
- Gii – 创建模型
- Gii – 生成控制器
- Gii – 生成模块
- Yii 有用的资源
- Yii - 快速指南
- Yii - 有用的资源
- Yii - 讨论
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* 模式指示所有以 app 开头的消息类别都必须使用此特定消息源进行翻译。在上面的配置中,所有俄语翻译都将位于 messages/ru-RU/app.php 文件中。
步骤 3 - 现在,创建 messages/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 -在网络浏览器中输入 URL http://localhost:8080/index.php?r=site/translation ,您将看到以下内容。
当我们将目标语言设置为 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,您将看到带有俄语翻译的页面。
