PHPackages                             rkit/translation-behavior-yii2 - 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. rkit/translation-behavior-yii2

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

rkit/translation-behavior-yii2
==============================

Translation Behavior for Yii2

2.0.0(7y ago)07MITPHP

Since Jul 26Pushed 7y ago1 watchersCompare

[ Source](https://github.com/rkit/translation-behavior-yii2)[ Packagist](https://packagist.org/packages/rkit/translation-behavior-yii2)[ Docs](https://github.com/rkit/translation-behavior-yii2)[ RSS](/packages/rkit-translation-behavior-yii2/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Translation Behavior for Yii2
=============================

[](#translation-behavior-for-yii2)

[![Build Status](https://camo.githubusercontent.com/0d630fdb650b7957832c89ae31977ca9ea812b06fc1527dd07e17f4421a4ebbc/68747470733a2f2f7472617669732d63692e6f72672f726b69742f7472616e736c6174696f6e2d6265686176696f722d796969322e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/rkit/translation-behavior-yii2)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5d38fb1b5181aa2b4e788fd2ea4ebe5c814fd757140daa3503dc0a79ae7f7b4f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f726b69742f7472616e736c6174696f6e2d6265686176696f722d796969322f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/rkit/translation-behavior-yii2/?branch=master)

Requirements
------------

[](#requirements)

PHP 7

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

[](#installation)

```
composer require rkit/translation-behavior-yii2

```

Configuration
-------------

[](#configuration)

For example, we have a `Post` model and we want to add translation capability.
Let's do it.

1. Add a `post_translation` table and a `PostTranslation` model for the translation

```
$this->createTable('{{%post_translation}}', [
    'id' => $this->primaryKey(),
    'post_id' => $this->integer()->notNull()->defaultValue(0),
    'language' => $this->string(2)->notNull()->defaultValue(''),
    'title' => $this->string()->notNull()->defaultValue(''),
]);
```

2. Add a `TranslationBehavior` behavior to the `Post` model

```
public function behaviors()
{
    return [
        'translationBehavior' => [
            'class' => 'rkit\translation\behavior\TranslationBehavior',
            'relationOne' => 'translation',
            'relationMany' => 'translations',
            'languageAttribute' => 'language',
            'defaultLanguage' => 'en',
            'attributes' => [ // attributes for translation
                'title',
            ],

        ],
    ];
}
```

3. Add `translation` and `translations` relations (see `relationOne` and `relationMany` options in the behavior)

```
/**
 * @return \yii\db\ActiveQuery
 */
public function getTranslation()
{
    return $this
        ->hasOne(PostTranslation::class, ['post_id' => 'id'])
        ->andWhere(['language' => \Yii::$app->language]);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getTranslations()
{
    return $this->hasMany(PostTranslation::class, ['post_id' => 'id']);
}
```

Usage
-----

[](#usage)

### Load translation

[](#load-translation)

```
$model = new Post();
$model->loadTranslations([
    'en' => ['title' => 'example'],
    'ru' => ['title' => 'пример'],
]);
$model->save();
```

### Get translation

[](#get-translation)

#### For current language

[](#for-current-language)

```
$model = Post::find()->with('translation')->where(['id' => $id])->one();

echo $model->title;
```

#### All translation

[](#all-translation)

```
$model = Post::find()->with('translations')->where(['id' => $id])->one();

echo $model->translate('en')->title;
echo $model->translate('ru')->title;
```

### Remove translation

[](#remove-translation)

```
$model = new Post();
$model->loadTranslations([]);
$model->save();
```

Tests
-----

[](#tests)

- [See docs](/tests/#tests)

Coding Standard
---------------

[](#coding-standard)

- PHP Code Sniffer ([phpcs.xml](./phpcs.xml))

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~12 days

Total

2

Last Release

2835d ago

Major Versions

1.0.0 → 2.0.02018-08-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/26f3097962081483e807941af9e4836adfe28c8c964b3972514e04deeeb10ed6?d=identicon)[rkit](/maintainers/rkit)

---

Top Contributors

[![rkit](https://avatars.githubusercontent.com/u/4242765?v=4)](https://github.com/rkit "rkit (5 commits)")

---

Tags

translationyii2translationyii2extensionBehavior

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/rkit-translation-behavior-yii2/health.svg)

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

###  Alternatives

[omgdef/yii2-multilingual-behavior

Port of the yii-multilingual-behavior for yii

143174.9k9](/packages/omgdef-yii2-multilingual-behavior)[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)[yiimaker/yii2-translatable

Translatable behavior aggregates logic of linking translations to the primary model

1534.0k1](/packages/yiimaker-yii2-translatable)[geertw/yii2-translatable-url-rule

A custom URL rule class for Yii 2 which allows to create translated URL rules

1420.3k](/packages/geertw-yii2-translatable-url-rule)

PHPackages © 2026

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