2017 © Pedro Peláez
 

yii2-extension translation-behavior-yii2

Translation Behavior for Yii2

image

rkit/translation-behavior-yii2

Translation Behavior for Yii2

  • Thursday, July 26, 2018
  • by rkit
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Translation Behavior for Yii2

Build Status Scrutinizer Code Quality, (*1)

Requirements

PHP 7, (*2)

Installation

composer require rkit/translation-behavior-yii2

Configuration

For example, we have a Post model and we want to add translation capability.
Let's do it., (*3)

  1. Add a post_translation table and a PostTranslation model for the translation
$this->createTable('{{%post_translation}}', [
    'id' => $this->primaryKey(),
    'post_id' => $this->integer()->notNull()->defaultValue(0),
    'language' => $this->string(2)->notNull()->defaultValue(''),
    'title' => $this->string()->notNull()->defaultValue(''),
]);
  1. Add a TranslationBehavior behavior to the Post model
public function behaviors()
{
    return [
        'translationBehavior' => [
            'class' => 'rkit\translation\behavior\TranslationBehavior',
            'relationOne' => 'translation',
            'relationMany' => 'translations',
            'languageAttribute' => 'language',
            'defaultLanguage' => 'en',
            'attributes' => [ // attributes for translation
                'title',
            ],

        ],
    ];
}
  1. Add translation and translations relations (see relationOne and relationMany options in the behavior)
/**
 * @return \yii\db\ActiveQuery
 */
public function getTranslation()
{
    return $this
        ->hasOne(PostTranslation::class, ['post_id' => 'id'])
        ->andWhere(['language' => \Yii::$app->language]);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getTranslations()
{
    return $this->hasMany(PostTranslation::class, ['post_id' => 'id']);
}

Usage

Load translation

$model = new Post();
$model->loadTranslations([
    'en' => ['title' => 'example'],
    'ru' => ['title' => 'пример'],
]);
$model->save();

Get translation

For current language

$model = Post::find()->with('translation')->where(['id' => $id])->one();

echo $model->title;

All translation

$model = Post::find()->with('translations')->where(['id' => $id])->one();

echo $model->translate('en')->title;
echo $model->translate('ru')->title;

Remove translation

$model = new Post();
$model->loadTranslations([]);
$model->save();

Tests

Coding Standard

The Versions

26/07 2018

dev-master

9999999-dev https://github.com/rkit/translation-behavior-yii2

Translation Behavior for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Igor Romanov

extension yii2 behavior translation

26/07 2018

1.0.0

1.0.0.0 https://github.com/rkit/translation-behavior-yii2

Translation Behavior for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Igor Romanov

extension yii2 behavior translation