Download the PHP package omgdef/yii2-multilingual-behavior without Composer
On this page you can find all versions of the php package omgdef/yii2-multilingual-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download omgdef/yii2-multilingual-behavior
More information about omgdef/yii2-multilingual-behavior
Files in omgdef/yii2-multilingual-behavior
Package yii2-multilingual-behavior
Short Description Port of the yii-multilingual-behavior for yii
License BSD-4-Clause
Informations about the package yii2-multilingual-behavior
Yii2 multilingual behavior
Yii2 port of the yii-multilingual-behavior.
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.
*!!! IMPORTANT !!! Docs for vesions 1. here**
*In vesion 2. forceOverwrite property is deprecated**
Examples
Example #1: current language translations are inserted to the model as normal attributes by default.
Example #2: if you use multilingual()
in a find()
query, every model translation is loaded as virtual attributes (title_en, title_fr, title_de, ...).
Installation
Preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Behavior attributes
Attributes marked as bold are required
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: and the id column (primary key) like this : |
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: or an associative array: |
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 :
And its associated translation table (configured as default), assuming translated fields are 'title' and 'content':
Attaching this behavior to the model (Post in the example). Commented fields have default values.
Then you have to overwrite the find()
method in your model
As this behavior has , you can use it in your query classes
Form example:
Hint: has to be populated with relative data otherwise translations will not be updated after send.