PHPackages                             bessonov87/yii2-mongodb-multilingual-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. bessonov87/yii2-mongodb-multilingual-behavior

ActiveYii2-extension

bessonov87/yii2-mongodb-multilingual-behavior
=============================================

yii2-multilingual-behavior for mongodb

12PHP

Since Mar 10Pushed 10y agoCompare

[ Source](https://github.com/bessonov87/yii2-mongodb-multilingual-behavior)[ Packagist](https://packagist.org/packages/bessonov87/yii2-mongodb-multilingual-behavior)[ RSS](/packages/bessonov87-yii2-mongodb-multilingual-behavior/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 multilingual behavior
==========================

[](#yii2-multilingual-behavior)

Yii2 MongoDb port of the [yii2-multilingual-behavior](https://github.com/OmgDef/yii2-multilingual-behavior).

[![Packagist Version](https://camo.githubusercontent.com/f763ed5c0d6c0ca53fab8c4a9c0ceea8cd530b0897c1c5995e435814efe54075/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6d676465662f796969322d6d756c74696c696e6775616c2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/omgdef/yii2-multilingual-behavior)[![Total Downloads](https://camo.githubusercontent.com/6474e6f0d03ad72a4308d20a9763f0821e4580e3b2c99ae27d1e647f4adfbb4b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f6d676465662f796969322d6d756c74696c696e6775616c2d6265686176696f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/omgdef/yii2-multilingual-behavior)[![Build Status](https://camo.githubusercontent.com/f0f181c79f69c5325c5b2a5c5b78e23b86a10e5dcf54eac164f2ccaa17af6e62/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4f6d674465662f796969322d6d756c74696c696e6775616c2d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/OmgDef/yii2-multilingual-behavior)[![Code Quality](https://camo.githubusercontent.com/c451ccaf136e50ca13687c69b6c334ed84f93bf4023fdb5a22a40e78e47f4b41/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6f6d676465662f796969322d6d756c74696c696e6775616c2d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/OmgDef/yii2-multilingual-behavior)[![Code Coverage](https://camo.githubusercontent.com/f08f300b321509fd4103351a262fa9b317d26fdd1e78ae01f109ee14db383e48/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6f6d676465662f796969322d6d756c74696c696e6775616c2d6265686176696f722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/OmgDef/yii2-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 NewsLang) for each model, so you can add or remove a language easily, without modifying your database.

Example
-------

[](#example)

If you use `multilingual()` in a `find()` query, every model translation is loaded as virtual attributes (title\_en, title\_fr, title\_de, ...).

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

Installation
------------

[](#installation)

Preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

```

or add

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

```

to the require section of your `composer.json` file.

Behavior attributes
-------------------

[](#behavior-attributes)

Attributes marked as bold are required

AttributeDescriptionlanguageFieldThe name of the language field of the translation table. Default is 'language'localizedPrefixThe 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`requireTranslationsIf this property is set to true required validators will be applied to all translation models.dynamicLangClassDynamically 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 neededlangClassNameThe 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 attributesUsage
-----

[](#usage)

Here an example of base 'news' table:

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

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

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

As this behavior has `MultilingualTrait`, you can use it in your query classes

```
namespace app\models;

use yii\mongodb\ActiveQuery;

class MultilingualQuery extends ActiveQuery
{
    use MultilingualTrait;
}
```

Form example:

```
//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]);
```

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community3

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

---

Top Contributors

[![bessonov87](https://avatars.githubusercontent.com/u/8646356?v=4)](https://github.com/bessonov87 "bessonov87 (2 commits)")

### Embed Badge

![Health badge](/badges/bessonov87-yii2-mongodb-multilingual-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/bessonov87-yii2-mongodb-multilingual-behavior/health.svg)](https://phpackages.com/packages/bessonov87-yii2-mongodb-multilingual-behavior)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
