2017 © Pedro PelĂĄez
 

yii2-extension yii2-mongodb-multilingual-behavior

yii2-multilingual-behavior for mongodb

image

bessonov87/yii2-mongodb-multilingual-behavior

yii2-multilingual-behavior for mongodb

  • Thursday, March 10, 2016
  • by bessonov87
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yii2 multilingual behavior

Yii2 MongoDb port of the yii2-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 NewsLang) for each model, so you can add or remove a language easily, without modifying your database., (*3)

Example

If you use multilingual() in a find() query, every model translation is loaded as virtual attributes (title_en, title_fr, title_de, ...)., (*4)

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

Either run, (*6)

php composer.phar require --prefer-dist bessonov87/yii2-mongodb-multilingual-behavior

or add, (*7)

"bessonov87/yii2-mongodb-multilingual-behavior": "*"

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

Behavior attributes

Attributes marked as bold are required, (*9)

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 'news' table:, (*10)

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

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

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

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

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

namespace app\models;

use yii\mongodb\ActiveQuery;

class MultilingualQuery extends ActiveQuery
{
    use MultilingualTrait;
}

Form example:, (*14)

//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

10/03 2016

dev-master

9999999-dev

yii2-multilingual-behavior for mongodb

  Sources   Download

BSD-4-Clause

The Requires

 

by Sergey Bessonov

extension yii2 behavior multilingual i18 yii2-mongodb-multilingual-behavior