yiimaker / yii2-translatable
可翻译行为聚集将翻译链接到主模型的相关逻辑
v1.0.2
2019-08-11 12:39 UTC
Requires
- yiisoft/yii2: ^2.0.0
Requires (Dev)
- codeception/codeception: ~3.0
This package is auto-updated.
Last update: 2024-08-29 04:20:10 UTC
README
可翻译行为
可翻译行为聚集将翻译链接到主模型的相关逻辑。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令
$ composer require yiimaker/yii2-translatable
或将以下内容添加到您的 composer.json
文件的 require
部分。
"yiimaker/yii2-translatable": "~1.0"
使用方法
- 将行为添加到您的主模型
public function behaviors() { return [ // ... 'translatable' => [ 'class' => TranslatableBehavior::className(), // 'translationRelationName' => 'translations', // 'translationLanguageAttrName' => 'language', // 'attributeNamePattern' => '%name% [%language%]', 'translationAttributeList' => [ 'title', 'description', ], ], ]; }
- 然后使用
getTranslation()
或translateTo()
方法
// product is an active record model with translatable behavior $product = new Product(); // sets translation for default application language $product->title = 'PhpStrom 2018.1'; $product->description = 'Лицензия PhpStrom IDE версия 2018.1'; // gets translation for English language $translation = $product->getTranslation('en'); $translation->title = 'PhpStrom 2018.1'; $translation->description = 'License of the PhpStrom IDE version 2018.1'; // sets description for French language $product->translateTo('fr')->description = 'La licence de PhpStorm IDE la version 2018.1'; $product->insert();
translateTo()
只是 getTranslation()
方法的别名。
保存模型后,您可以从数据库中检索此模型,可翻译行为将自动检索所有翻译。
$product = Product::find() ->where(['id' => 1]) ->with('translations') ->one() ; // gets translation for English language $product->translateTo('en')->description; // License of the PhpStrom IDE version 2018.1 // gets translation for French language $product->translateTo('fr')->description; // La licence de PhpStorm IDE la version 2018.1 // check whether Ukrainian translation not exists if (!$product->hasTranslation('uk')) { $product->translateTo('uk')->description = 'Ліцензія PhpStrom IDE версія 2018.1'; } // update Enlish translation $product->translateTo('en')->title = 'PhpStorm IDE'; $product->update();
测试
您可以使用 composer 命令运行测试
$ composer test
或使用以下命令
$ codecept build && codecept run
贡献
有关贡献的信息,请阅读 CONTRIBUTING.md。
赞助
许可证
本项目在 BSD-3-Clause 许可证的条款下发布 LICENSE。
版权所有 (c) 2017-2022, Yii Maker