conquer / i18n
此包已被弃用且不再维护。未建议替代包。
Yii2 框架的 I18n 自动翻译扩展
1.0.1
2015-06-18 11:41 UTC
Requires
- conquer/helpers: ~2.0.6
- yiisoft/yii2: >=2.0.4
This package is auto-updated.
Last update: 2022-02-01 12:48:32 UTC
README
描述
此扩展允许使用外部服务自动翻译消息。
##Google 通过 Google 翻译,您可以在数千种语言对之间动态翻译文本。Google 翻译 API 允许网站和程序以编程方式集成 Google 翻译。Google 翻译 API 作为一项付费服务提供。有关详细信息,请参阅 定价 和 常见问题解答 页面。
##Yandex API 提供了对 Yandex 在线机器翻译服务的访问。它支持40多种语言,可以翻译单个单词或完整文本。API 允许在移动应用或网络服务中嵌入 Yandex.Translate,或翻译大量文本,例如技术文档。
安装
安装此扩展的首选方式是通过 composer。
要安装,请运行
$ php composer.phar require conquer/i18n "*"
或添加
"conquer/i18n": "*"
到您的 composer.json
文件的 require
部分。
要创建数据库表,请运行迁移命令
$ yii migrate --migrationPath=@conquer/i18n/migrations
使用方法
您需要配置模块和 i18n 组件
$config = [ 'bootstrap' => ['translate'], 'language' => 'ru', 'modules' => [ 'translate' => 'conquer\i18n\Module' ], 'components' => [ 'i18n' => [ 'class' => 'yii\i18n\I18N', 'translations' => [ '*' => [ 'class' => 'conquer\i18n\MessageSource', 'translator' => [ // 'class'=>'conquer\i18n\translators\GoogleTranslator', // 'apiKey' => 'obtain API key in the google developer console', 'class'=>'conquer\i18n\translators\YandexTranslator', 'apiKey' => '[yandex form](https://tech.yandex.com/keys/get/?service=trnsl)', ], ], ], ], ], ];
使用 \Yii::t 函数来翻译消息
<h1><?= \Yii::t('app', 'Congratulations!') ?></h1>
"*" 翻译类别默认用于所有未翻译的消息。
您可以为外部翻译者的消息翻译配置一个单独的类别。
'components' => [ 'i18n' => [ 'class' => 'yii\i18n\I18N', 'translations' => [ 'yandex' => [ 'class' => 'conquer\i18n\MessageSource', 'translator' => [ 'class'=>'conquer\i18n\translators\YandexTranslator', 'apiKey' => '[yandex form](https://tech.yandex.com/keys/get/?service=trnsl)', ], ], ], ], ],
使用 \Yii::t 函数在 "yandex" 类别中翻译消息
<h1><?= \Yii::t('yandex', 'Congratulations!') ?></h1>
许可
conquer/i18n 采用 MIT 许可证发布。有关详细信息,请参阅附带的 LICENSE.md
文件。