2017 © Pedro PelĂĄez
 

yii2-extension yii2-multilingual-behavior

Port of the yii-multilingual-behavior for yii

image

shirase55/yii2-multilingual-behavior

Port of the yii-multilingual-behavior for yii

  • Tuesday, November 22, 2016
  • by shirase
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 37 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Yii2 multilingual behavior

Yii2 port of the yii-multilingual-behavior., (*1)

Packagist Version Total Downloads Build Status Code Quality Code Coverage, (*2)

This behavior allows you to create multilingual models and almost use them as normal models. Translations are stored in a separate table in the database (ex: PostLang or ProductLang) for each model, so you can add or remove a language easily, without modifying your database., (*3)

!!! IMPORTANT !!! Docs for vesions 1.* here, (*4)

In vesion 2.* forceOverwrite property is deprecated, (*5)

Examples

Example #1: current language translations are inserted to the model as normal attributes by default., (*6)

//Assuming current language is english

$model = Post::findOne(1);
echo $model->title; //echo "English title"

//Now let's imagine current language is french 
$model = Post::findOne(1);
echo $model->title; //echo "Titre en Français"

$model = Post::find()->localized('en')->one();
echo $model->title; //echo "English title"

//Current language is still french here

Example #2: if you use multilingual() in a find() query, every model translation is loaded as virtual attributes (title_en, title_fr, title_de, ...)., (*7)

$model = Post::find()->multilingual()->one();
echo $model->title_en; //echo "English title"
echo $model->title_fr; //echo "Titre en Français"

Installation

Preferred way to install this extension is through composer., (*8)

Either run, (*9)

php composer.phar require shirase/yii2-multilingual-behavior

or add, (*10)

"shirase/yii2-multilingual-behavior": "dev-master"

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

Behavior attributes

Attributes marked as bold are required, (*12)

Attribute Description
languageField The name of the language field of the translation table. Default is 'language'
localizedPrefix The prefix of the localized attributes in the lang table. Is used to avoid collisions in queries. The columns in the translation table corresponding to the localized attributes have to be name like this: [prefix]_[name of the attribute] and the id column (primary key) like this : [prefix]_id
requireTranslations If this property is set to true required validators will be applied to all translation models.
dynamicLangClass Dynamically create translation model class. If true, the translation model class will be generated on runtime with the use of the eval() function so no additionnal php file is needed
langClassName The name of translation model class. Dafault value is model name + Lang
languages Available languages. It can be a simple array: ['fr', 'en'] or an associative array: ['fr' => 'Français', 'en' => 'English']
defaultLanguage The default language
langForeignKey Name of the foreign key field of the translation table related to base model table.
tableName The name of the translation table
attributes Multilingual attributes

Usage

Here an example of base 'post' table :, (*13)

CREATE TABLE IF NOT EXISTS `post` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `created_at` datetime NOT NULL,
    `updated_at` datetime NOT NULL,
    `enabled` tinyint(1) NOT NULL DEFAULT '1',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And its associated translation table (configured as default), assuming translated fields are 'title' and 'content':, (*14)

CREATE TABLE IF NOT EXISTS `postLang` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `post_id` int(11) NOT NULL,
    `language` varchar(6) NOT NULL,
    `title` varchar(255) NOT NULL,
    `content` TEXT NOT NULL,
    PRIMARY KEY (`id`),
    KEY `post_id` (`post_id`),
    KEY `language` (`language`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

ALTER TABLE `postLang`
ADD CONSTRAINT `postlang_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Attaching this behavior to the model (Post in the example). Commented fields have default values., (*15)

public function behaviors()
{
    return [
        'ml' => [
            'class' => MultilingualBehavior::className(),
            'languages' => [
                'ru' => 'Russian',
                'en-US' => 'English',
            ],
            //'languageField' => 'language',
            //'localizedPrefix' => '',
            //'requireTranslations' => false',
            //'dynamicLangClass' => true',
            //'langClassName' => PostLang::className(), // or namespace/for/a/class/PostLang
            'defaultLanguage' => 'ru',
            'langForeignKey' => 'post_id',
            'tableName' => "{{%postLang}}",
            'attributes' => [
                'title', 'content',
            ]
        ],
    ];
}

Then you have to overwrite the find() method in your model, (*16)

    public static function find()
    {
        return new MultilingualQuery(get_called_class());
    }

As this behavior has MultilingualTrait, you can use it in your query classes, (*17)

namespace app\models;

use yii\db\ActiveQuery;

class MultilingualQuery extends ActiveQuery
{
    use MultilingualTrait;
}

Form example:, (*18)

//title will be saved to model table and as translation for default language
$form->field($model, 'title')->textInput(['maxlength' => 255]);
$form->field($model, 'title_en')->textInput(['maxlength' => 255]);

The Versions

22/11 2016

dev-master

9999999-dev

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

12/02 2016

2.1.2

2.1.2.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

21/08 2015

2.1.1

2.1.1.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

02/06 2015

dev-issue22

dev-issue22

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

06/03 2015

2.1

2.1.0.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

27/02 2015

2.0

2.0.0.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

22/12 2014

1.0.3

1.0.3.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

13/11 2014

1.0.2

1.0.2.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

15/10 2014

1.0.1

1.0.1.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior

08/10 2014

1.0

1.0.0.0

Port of the yii-multilingual-behavior for yii

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar OmgDef

extension yii2 behavior multilingual i18 yii2-multilingual-behavior