2017 © Pedro Peláez
 

yii2-extension yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

image

lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  • Tuesday, July 24, 2018
  • by LAV45
  • Repository
  • 6 Watchers
  • 29 Stars
  • 7,396 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 9 Forks
  • 2 Open issues
  • 18 Versions
  • 11 % Grown

The README.md

yii2-translated-behavior

Latest Stable Version License Total Downloads Build Status Test Coverage Scrutinizer Code Quality Code Climate, (*1)

The Translated Behavior is a Yii2 extension for ActiveRecord models, that will help you add the possibility of transferring any entity., (*2)

You can see DEMO, (*3)

Installation

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

You can set the console, (*5)

$ composer require "lav45/yii2-translated-behavior:1.4.*"

or add, (*6)

"lav45/yii2-translated-behavior": "1.4.*"

in require section in composer.json file., (*7)

Settings

First you have to move all the attributes that are required for translation in a separate table. For example, imagine that we want to save the translation of the title and description of your post being. Your table schema should be brought to the following form:, (*8)

    +--------------+        +--------------+       +-------------------+
    |     post     |        |     post     |       |     post_lang     |
    +--------------+        +--------------+       +-------------------+
    | id           |        | id           |       | post_id           |
    | title        |  --->  | created_at   |   +   | lang_id           |
    | description  |        | updated_at   |       | title             |
    | updated_at   |        +--------------+       | description       |
    | created_at   |                               +-------------------+
    +--------------+

After you change the table schema, we now need to determine the ratio of our ActiveRecord objects and adding behavior:, (*9)

Post

use yii\db\ActiveRecord;
use lav45\translate\TranslatedTrait;
use lav45\translate\TranslatedBehavior;

/**
 * ...
 * @property string $title
 * @property string $description
 */
class Post extends ActiveRecord
{
    use TranslatedTrait;

    public function rules()
    {
        return [
            // ...

            [['title'], 'required'],
            [['title'], 'string', 'max' => 128],

            [['description'], 'required'],
            [['description'], 'string'],
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TranslatedBehavior::className(),
                'translateRelation' => 'postLangs', // Specify the name of the connection that will store transfers
//                'languageAttribute' => 'lang_id' // post_lang field from the table that will store the target language
                'translateAttributes' => [
                    'title',
                    'description',
                ]
            ]
        ];
    }

    public function attributeLabels()
    {
        return [
            // ...
            'title' => 'Title',
            'description' => 'Description',
        ];
    }

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

Language model

migrate

migrate/m151220_112320_lang.php, (*10)

Apply with the console command:, (*11)

~$ yii migrate/up --migrationPath=vendor/lav45/yii2-translated-behavior/migrate

Lang ActiveRecord model cite completely

\lav45\translate\models\Lang, (*12)

Using

Backend

backend/config/bootstrap.php, (*13)

Yii::$container->set('lav45\translate\TranslatedBehavior', [
    'language' => isset($_GET['lang_id']) ? $_GET['lang_id'] : null
]);

backend/controllers/PostController.php, (*14)

namespace backend\controllers;

use yii\web\Controller;
use yii\data\ActiveDataProvider;

use common\models\Post;
use common\models\Lang;

class PostController extends Controller
{
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Post::find()
                ->with([
                    'currentTranslate', // loadabing data associated with the current translation
                    'hasTranslate' // need to display the status was translated page
                ]),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'langList' => Lang::getList(),
        ]);
    }
// ...
}

backend/view/post/index.php, (*15)

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [
                'class' => 'lav45\translate\grid\ActionColumn',
                'languages' => $langList,
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{delete}'
            ],
        ],
    ]);
    ?>

As a result, after the creation of a new page will get a few buttons to edit the content in different languages, (*16)

Translate button, (*17)

So you can get the current language of the model, (*18)

/**
 * @var $this yii\web\View
 * @var $model common\models\Page
 */

$this->title = 'Create Post ( ' . $model->language . ' )';

Frontend

frontend/config/main.php, (*19)

use lav45\translate\models\Lang;

return [
    'components' => [
        'urlManager' => [
            'class' => 'lav45\translate\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                [
                    'class' => 'yii\web\UrlRule', // If there is no need to substitute the language, you can use the base class
                    'pattern' => '',
                    'route' => 'post/index',
                ],
                [
                    'pattern' => '<_lang:' . Lang::PATTERN . '>/<id:\d+>',
                    'route' => 'post/view',
                ],
                [
                    'pattern' => '<_lang:' . Lang::PATTERN . '>',
                    'route' => 'post/index',
                ]
            ],
        ],
    ],
];

frontend/controllers/PostController.php, (*20)

namespace frontend\controllers;

use yii\web\Controller;
use common\models\Post;
use lav45\translate\models\Lang;

class PostController extends Controller
{
    public function behaviors()
    {
        return [
            [
              // ContentNegotiator will be determined from a URL or browser language settings and install it in
              // Yii::$app->language, which uses the class TranslatedBehavior as language translation
                'class' => 'yii\filters\ContentNegotiator',
                'languages' => Lang::getLocaleList()
            ],
        ];
    }

or you can add for all controllers, for this you need to add in frontend/config/bootstrap.php, (*21)

\yii\base\Event::on('yii\base\Controller', 'beforeAction', function($event) {
    /** @var yii\filters\ContentNegotiator $negotiator */
    $negotiator = Yii::createObject([
        'class' => 'yii\filters\ContentNegotiator',
        'languages' => \common\models\Lang::getLocaleList(),
    ]);
    /** @var yii\base\ActionEvent $event */
    $negotiator->attach($event->action);
    $negotiator->negotiate();
});

License

yii2-translated-behavior it is available under a BSD 3-Clause License. Detailed information can be found in the LICENSE.md., (*22)

The Versions

24/07 2018

dev-master

9999999-dev https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

07/11 2017

1.4.4

1.4.4.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

17/10 2017

dev-travis

dev-travis https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

21/08 2017

13859.x-dev

13859.9999999.9999999.9999999-dev https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

07/07 2017

dev-cascade_delete

dev-cascade_delete https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

07/07 2017

dev-loadDefaultValues

dev-loadDefaultValues https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

10/03 2017

1.4.3

1.4.3.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexey Loban

extension yii2 behavior translated

06/03 2017

1.4.2

1.4.2.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexey Loban

extension yii2 behavior translated

06/03 2017

dev-update_trait

dev-update_trait https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexey Loban

extension yii2 behavior translated

28/07 2016

1.4.1

1.4.1.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

27/02 2016

1.4.0

1.4.0.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

20/12 2015

1.3.3

1.3.3.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

30/11 2015

1.3.2

1.3.2.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

26/09 2015

1.3.1

1.3.1.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

20/08 2015

1.3.0

1.3.0.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexey Loban

extension yii2 behavior translated

20/07 2015

1.2.0

1.2.0.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexey Loban

extension yii2 behavior translated

20/07 2015

1.1.0

1.1.0.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexey Loban

extension yii2 behavior translated

24/05 2015

1.0.0

1.0.0.0 https://github.com/lav45/yii2-translated-behavior

This extension allows you to quickly and simple enough to add translations for any ActiveRecord models.

  Sources   Download

BSD-3-Clause

The Requires

 

by Alexey Loban

extension yii2 behavior translated