2017 © Pedro Peláez
 

yii2-extension yii2-translateable

The translateable behavior for the Yii framework

image

shirase55/yii2-translateable

The translateable behavior for the Yii framework

  • Monday, February 26, 2018
  • by shirase
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 22 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Translateable Behavior for Yii 2

Build Status Code Coverage Code Quality Packagist Version, (*1)

A modern translateable behavior for the Yii framework., (*2)

Installation

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

Either run, (*4)

$ composer require creocoder/yii2-translateable

or add, (*5)

"creocoder/yii2-translateable": "~1.0"

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

Migrations

Run the following command, (*7)

$ yii migrate/create create_post_table

Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_post_table.php file, inside the up() method add the following, (*8)

$this->createTable('{{%post}}', [
    'id' => Schema::TYPE_PK,
]);

Run the following command, (*9)

$ yii migrate/create create_post_translation_table

Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_post_translation_table.php file, inside the up() method add the following, (*10)

$this->createTable('{{%post_translation}}', [
    'post_id' => Schema::TYPE_INTEGER . ' NOT NULL',
    'language' => Schema::TYPE_STRING . '(16) NOT NULL',
    'title' => Schema::TYPE_STRING . ' NOT NULL',
    'body' => Schema::TYPE_TEXT . ' NOT NULL',
]);

$this->addPrimaryKey('', '{{%post_translation}}', ['post_id', 'language']);

Configuring

Configure model as follows, (*11)

use creocoder\translateable\TranslateableBehavior;

/**
 * ...
 * @property string $title
 * @property string $body
 * ...
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'translateable' => [
                'class' => TranslateableBehavior::className(),
                'translationAttributes' => ['title', 'body'],
                // translationRelation => 'translations',
                // translationLanguageAttribute => 'language',
            ],
        ];
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_INSERT | self::OP_UPDATE,
        ];
    }

    public function getTranslations()
    {
        return $this->hasMany(PostTranslation::className(), ['post_id' => 'id']);
    }
}

Model PostTranslation can be generated using Gii., (*12)

Usage

Setting translations to the entity

To set translations to the entity, (*13)

$post = new Post();

// title attribute translation for default application language
$post->title = 'Post title';

// body attribute translation for default application language
$post->body = 'Post body';

// title attribute translation for German
$post->translate('de-DE')->title = 'Post titel';

// body attribute translation for German
$post->translate('de-DE')->body = 'Post inhalt';

// title attribute translation for Russian
$post->translate('ru-RU')->title = 'Заголовок поста';

// body attribute translation for Russian
$post->translate('ru-RU')->body = 'Тело поста';

// save post and its translations
$post->save();

Getting translations from the entity

To get translations from the entity, (*14)

$posts = Post::find()->with('translations')->all();

foreach ($posts as $post) {
    // title attribute translation for default application language
    $title = $post->title;

    // body attribute translation for default application language
    $body = $post->body;

    // title attribute translation for German
    $germanTitle = $post->translate('de-DE')->title;

    // body attribute translation for German
    $germanBody = $post->translate('de-DE')->body;

    // title attribute translation for Russian
    $russianTitle = $post->translate('ru-RU')->title;

    // body attribute translation for Russian
    $russianBody = $post->translate('ru-RU')->body;
}

Checking for translations in the entity

To check translations in the entity, (*15)

$post = Post::findOne(1);

// checking for default application language translation
$result = $post->hasTranslation();

// checking for German translation
$result = $post->hasTranslation('de-DE');

// checking for Russian translation
$result = $post->hasTranslation('ru-RU');

Advanced usage

Collecting tabular input

Example of controller actions, (*16)

class PostController extends Controller
{
    public function actionCreate()
    {
        $model = new Post();

        foreach (Yii::$app->request->post('PostTranslation', []) as $language => $data) {
            foreach ($data as $attribute => $translation) {
                $model->translate($language)->$attribute = $translation;
            }
        }

        //...
    }

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

        if ($model === null) {
            throw new NotFoundHttpException('The requested page does not exist.');
        }

        foreach (Yii::$app->request->post('PostTranslation', []) as $language => $data) {
            foreach ($data as $attribute => $translation) {
                $model->translate($language)->$attribute = $translation;
            }
        }

        //...
    }
}

Example of view form, (*17)

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin();

foreach (['en-US', 'de-DE', 'ru-RU'] as $language) {
    echo $form->field($model->translate($language), "[$language]title")->textInput();
    echo $form->field($model->translate($language), "[$language]body")->textarea();
}

//...

ActiveForm::end();

Language specific translation attribute labels

Example of model attribute labels, (*18)

class PostTranslation extends \yii\db\ActiveRecord
{
    public function attributeLabels()
    {
        switch ($this->language) {
            case 'de-DE':
                return [
                    'title' => 'Titel',
                    'body' => 'Inhalt',
                ];
            case 'ru-RU':
                return [
                    'title' => 'Заголовок',
                    'body' => 'Тело',
                ];
            default:
                return [
                    'title' => 'Title',
                    'body' => 'Body',
                ];
        }
    }
}

Donating

Support this project and others by creocoder via gratipay., (*19)

Support via Gratipay, (*20)

The Versions

26/02 2018

dev-master

9999999-dev

The translateable behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov

yii2 translation translateable

26/02 2018

1.0.1

1.0.1.0

The translateable behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov

yii2 translation translateable

26/02 2015

1.0.0

1.0.0.0

The translateable behavior for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexander Kochetov

yii2 translateable