https://github.com/yiimaker/yii2-translatable
Translatable behavior aggregates logic of linking translations to the primary model
https://github.com/yiimaker/yii2-translatable
i18n translatable translations yii2 yii2-behaviors yii2-extension
Last synced: about 1 year ago
JSON representation
Translatable behavior aggregates logic of linking translations to the primary model
- Host: GitHub
- URL: https://github.com/yiimaker/yii2-translatable
- Owner: yiimaker
- License: bsd-3-clause
- Created: 2018-03-25T19:35:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-06-29T18:39:52.000Z (almost 4 years ago)
- Last Synced: 2025-04-15T10:56:39.695Z (about 1 year ago)
- Topics: i18n, translatable, translations, yii2, yii2-behaviors, yii2-extension
- Language: PHP
- Homepage:
- Size: 78.1 KB
- Stars: 15
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://stand-with-ukraine.pp.ua)
Translatable behavior
[](https://travis-ci.org/yiimaker/yii2-translatable)
[](https://scrutinizer-ci.com/g/yiimaker/yii2-translatable/?branch=master)
[](https://packagist.org/packages/yiimaker/yii2-translatable)
[](https://packagist.org/packages/yiimaker/yii2-translatable)
[](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)
Translatable behavior aggregates logic of linking translations to the primary model.
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
$ composer require yiimaker/yii2-translatable
```
or add
```
"yiimaker/yii2-translatable": "~1.0"
```
to the `require` section of your `composer.json`.
Usage
-----
1. Add behavior to the your primary model
```php
public function behaviors()
{
return [
// ...
'translatable' => [
'class' => TranslatableBehavior::className(),
// 'translationRelationName' => 'translations',
// 'translationLanguageAttrName' => 'language',
// 'attributeNamePattern' => '%name% [%language%]',
'translationAttributeList' => [
'title',
'description',
],
],
];
}
```
2. And use `getTranslation()` or `translateTo()` methods
```php
// 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.
```php
$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](CONTRIBUTING.md).
Sponsoring
----------
License
-------
[](LICENSE)
This project is released under the terms of the BSD-3-Clause [license](LICENSE).
Copyright (c) 2017-2022, Yii Maker
