yiimaker/yii2-translatable
最新稳定版本:v1.0.2
Composer 安装命令:
composer require yiimaker/yii2-translatable
包简介
Translatable behavior aggregates logic of linking translations to the primary model
README 文档
README
Translatable behavior
Translatable behavior aggregates logic of linking translations to the primary model.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require yiimaker/yii2-translatable
or add
"yiimaker/yii2-translatable": "~1.0"
to the require section of your composer.json.
Usage
- Add behavior to the your primary model
public function behaviors() { return [ // ... 'translatable' => [ 'class' => TranslatableBehavior::className(), // 'translationRelationName' => 'translations', // 'translationLanguageAttrName' => 'language', // 'attributeNamePattern' => '%name% [%language%]', 'translationAttributeList' => [ 'title', 'description', ], ], ]; }
- And use
getTranslation()ortranslateTo()methods
// 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() it's just an alias for getTranslation() method.
After saving the model you can fetch this model from the database and translatable behavior will fetch all translations automatically.
$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();
Tests
You can run tests with composer command
$ composer test
or using following command
$ codecept build && codecept run
Contributing
For information about contributing please read CONTRIBUTING.md.
Sponsoring
License
This project is released under the terms of the BSD-3-Clause license.
Copyright (c) 2017-2022, Yii Maker
统计信息
- 总下载量: 32.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-04-21