2017 © Pedro Peláez
 

yii2-extension yii2-translatable

Translatable behavior aggregates logic of linking translations to the primary model

image

yiimaker/yii2-translatable

Translatable behavior aggregates logic of linking translations to the primary model

  • Wednesday, May 23, 2018
  • by greeflas
  • Repository
  • 3 Watchers
  • 6 Stars
  • 465 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 4 Versions
  • 160 % Grown

The README.md

Stand With Ukraine, (*1)

, (*2)

Translatable behavior

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version StandWithUkraine, (*3)

Translatable behavior aggregates logic of linking translations to the primary model., (*4)

Installation

The preferred way to install this extension is through composer., (*5)

Either run, (*6)

$ composer require yiimaker/yii2-translatable

or add, (*7)

"yiimaker/yii2-translatable": "~1.0"

to the require section of your composer.json., (*8)

Usage

  1. 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',
            ],
        ],
    ];
}
  1. And use getTranslation() or translateTo() 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., (*9)

After saving the model you can fetch this model from the database and translatable behavior will fetch all translations automatically., (*10)

$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, (*11)

$ composer test

or using following command, (*12)

$ codecept build && codecept run

Contributing

For information about contributing please read CONTRIBUTING.md., (*13)

Sponsoring

Buy Me A Coffee, (*14)

License

License, (*15)

This project is released under the terms of the BSD-3-Clause license., (*16)

Copyright (c) 2017-2022, Yii Maker, (*17)

The Versions

23/05 2018

dev-master

9999999-dev

Translatable behavior aggregates logic of linking translations to the primary model

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Vladimir Kuprienko

translations yii2 behavior translation translatable ar

23/05 2018

v1.0.1

1.0.1.0

Translatable behavior aggregates logic of linking translations to the primary model

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Vladimir Kuprienko

translations yii2 behavior translation translatable ar

23/05 2018

1.0.x-dev

1.0.9999999.9999999-dev

Translatable behavior aggregates logic of linking translations to the primary model

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Vladimir Kuprienko

translations yii2 behavior translation translatable ar

22/04 2018

v1.0.0

1.0.0.0

Translatable behavior aggregates logic of linking translations to the primary model

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Vladimir Kuprienko

translations yii2 behavior translation translatable ar