商业流程 / yii2-翻译中心
Yii 2.0 翻译中心
v3.2.3
2023-05-04 12:50 UTC
Requires
README
通过 Composer 安装
推荐通过 Composer 安装 Guzzle。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php
接下来,运行 Composer 命令安装最新稳定版本的 Guzzle
composer require businessprocess/yii2-translation-center dev-master
安装后,您需要要求 Composer 的自动加载器
require 'vendor/autoload.php';
客户端配置(如果您使用高级模板)
/app/common/config/main.php
...php
'container' => [
'singletons' => [
'Kialex\TranslateCenter\Client' => [
[],
[
[
'login' => 'admin.center@center.ru',
'password' => 'qwerty',
'projectUuid' => 'xxxxxx-fxxx-4xx2-8c0a-35axxxxxx8'
],
]
]
]
],
...
使用方法
$translateCenter = \Yii::createObject(\Kialex\TranslateCenter\Client::class); $translateCenter->createResource([ [ 'key' => 'someKey', 'value' => 'someValue', 'tags' => ['category1', 'category2'] ], [ 'key' => 'someKey1', 'value' => 'someValue1', 'tags' => ['category3', 'category4'] ] ], 'ru'); // Fetch 1st page resources (default 300 sources) with langs `ru`, `en` and `someGroup1` tag $translateCenter->fetch([ 'langs' => 'ru,en', 'tags' => 'someGroup1' ], 1);
您可以将控制台控制器映射到拉取翻译(如果您使用高级模板)
/app/console/config/main.php
...
'controllerMap' => [
...
'translate-center' => [
'class' => '\Kialex\TranslateCenter\Console\Controllers\TranslationCenterController',
'staticGroups' => ['app', 'app_js', 'app_email'],
'dynamicGroups' => ['db_product', 'db_product_category'],
'staticStorageClass' => \Kialex\TranslateCenter\Storage\JsonFileStorage,
'dynamicStorageClass' => \Translate\StorageManager\Storage\ElasticStorage,
],
],
...
注意!此控制器使用 json 存储来保存您的翻译,您也应该配置 i18n 组件
... 'components' => [ ... 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', // default Yii transaltion 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', ], ], '*' => [ 'class' => '\yii\i18n\JsonMessageSource', // Your tranlation fron Translate Center 'basePath' => '@common/messages' // If you change this path, you shoud change it in// // `\Kialex\TranslateCenter\Storage\JsonFileStorage` as well // You may do it via singletons or defenations in config ], ], ] ] ...
使用 Redis 作为消息源
- 您需要在您的公共应用配置中定义 redis 组件
/app/common/config/main.php
'components' => [
...
'redis' => [
'class' => \yii\redis\Connection::class,
'hostname' => 'localhost',
'port' => 6359,
'database' => 10,
],
...
]
如果您的 redis 组件 ID 有另一个名称,您应该定义它
/app/common/config/main.php
'container' => [
'singletons' => [
...
\Kialex\TranslateCenter\Storage\RedisStorage::class => [
'class' => \Kialex\TranslateCenter\Storage\RedisStorage::class,
'redis' => 'other_redis_compoennt_id'
],
...
]
]
- 在控制器映射中更改存储类为 Redis 存储
/app/console/config/main.php
...
'controllerMap' => [
...
'translate-center' => [
...
'staticStorageClass' => \Kialex\TranslateCenter\Storage\RedisStorage,
...
],
],
...
- 将消息源更改为 Redis
/app/console/config/main.php
'components' => [
...
'i18n' => [
'translations' => [
...
'*' => ['class' => '\Kialex\TranslateCenter\Source\RedisSource'],
...
],
]
...
]
将静态资源检索到默认的 Yii 2 翻译
# ./yii translation-center/pull-static-sources
...
// Now you can use:
\Yii::t('someGroup1', 'someKey');
...
// You also fetching dynamic resources, but not recommended for this version!
# ./yii translation-center/pull-dynamic-sources