PHPackages                             omgdef/yii2-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. [Localization &amp; i18n](/categories/localization)
4. /
5. omgdef/yii2-multilingual-behavior

ActiveYii2-extension[Localization &amp; i18n](/categories/localization)

omgdef/yii2-multilingual-behavior
=================================

Port of the yii-multilingual-behavior for yii

2.1.2(10y ago)143174.9k↓10.7%61[22 issues](https://github.com/OmgDef/yii2-multilingual-behavior/issues)[2 PRs](https://github.com/OmgDef/yii2-multilingual-behavior/pulls)9BSD-4-ClausePHP

Since Oct 7Pushed 3y ago18 watchersCompare

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

READMEChangelog (8)Dependencies (3)Versions (10)Used By (9)

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

[](#yii2-multilingual-behavior)

Yii2 port of the [yii-multilingual-behavior](https://github.com/belerophon/yii-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 ProductLang) for each model, so you can add or remove a language easily, without modifying your database.

**!!! IMPORTANT !!! Docs for vesions 1.* [here](https://github.com/OmgDef/yii2-multilingual-behavior/blob/f91d63403f02c8a3266796b41d197068d7ef7fbb/README.md)*\*

**In vesion 2.* forceOverwrite property is deprecated*\*

Examples
--------

[](#examples)

Example #1: current language translations are inserted to the model as normal attributes by default.

```
//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, ...).

```
$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 omgdef/yii2-multilingual-behavior

```

or add

```
"omgdef/yii2-multilingual-behavior": "~2.0"

```

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 'post' table :

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

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

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

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

**Hint:** `$model` has to be populated with `translations` relative data otherwise translations will not be updated after `$form` send.

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 86.9% 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.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~104 days

Total

8

Last Release

3748d ago

Major Versions

1.0.3 → 2.02015-02-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/f2bc222dd7fb13d52e7da6dbea12da3332bfd5366e9acb1e120b2061e011d880?d=identicon)[OmgDef](/maintainers/OmgDef)

---

Top Contributors

[![OmgDef](https://avatars.githubusercontent.com/u/6762337?v=4)](https://github.com/OmgDef "OmgDef (73 commits)")[![padlyuck](https://avatars.githubusercontent.com/u/630610?v=4)](https://github.com/padlyuck "padlyuck (4 commits)")[![igorveremsky](https://avatars.githubusercontent.com/u/13087365?v=4)](https://github.com/igorveremsky "igorveremsky (3 commits)")[![Shura1oplot](https://avatars.githubusercontent.com/u/1537165?v=4)](https://github.com/Shura1oplot "Shura1oplot (1 commits)")[![matperez](https://avatars.githubusercontent.com/u/1917115?v=4)](https://github.com/matperez "matperez (1 commits)")[![madand](https://avatars.githubusercontent.com/u/2280844?v=4)](https://github.com/madand "madand (1 commits)")[![ddinchev](https://avatars.githubusercontent.com/u/1397692?v=4)](https://github.com/ddinchev "ddinchev (1 commits)")

---

Tags

multilingualyii2extensionBehaviori18 yii2-multilingual-behavior

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[lav45/yii2-translated-behavior

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

3742.2k](/packages/lav45-yii2-translated-behavior)[yeesoft/yii2-multilingual

Yii2 Multilingual Module

177.4k1](/packages/yeesoft-yii2-multilingual)[devgroup/yii2-multilingual

Allows building yii2 apps for multiple languages using regional URL's and domains

152.6k2](/packages/devgroup-yii2-multilingual)

PHPackages © 2026

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