PHPackages                             noname9/yii2-multilanguages - 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. noname9/yii2-multilanguages

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

noname9/yii2-multilanguages
===========================

The translateable behavior for the Yii2 framework

1.1.2(6y ago)030BSD-3-ClausePHPCI failing

Since Jan 29Pushed 6y ago1 watchersCompare

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

READMEChangelog (5)Dependencies (3)Versions (6)Used By (0)

yii2-multilanguages
===================

[](#yii2-multilanguages)

A translateable behavior for the Yii2 framework.

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

[](#installation)

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

Either run

```
$ composer require noname9/yii2-multilanguages
```

or add

```
"noname9/yii2-multilanguages": "~1.0"

```

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

Migrations
----------

[](#migrations)

Run the following command

```
$ yii migrate/create create_post_table
```

Open the `/path/to/migrations/m_xxxxxx_xxxxxx_create_post_table.php` file, inside the `up()` method add the following

```
$this->createTable('{{%post}}', [
    'id' => $this->primaryKey(),
]);
```

Run the following command

```
$ yii migrate/create create_post_translation_table
```

Open the `/path/to/migrations/m_xxxxxx_xxxxxx_create_post_translation_table.php` file, inside the `up()` method add the following

```
$this->createTable('{{%post_translation}}', [
    'id' => $this->primaryKey(),
    'post_id' => $this->integer()->notNull(),
    'language' => $this->string(8)->notNull(),
    'title' => $this->string(1024)->notNull(),
    'body' => $this->text(),
]);
```

Configuring
-----------

[](#configuring)

Configure model as follows

```
use creocoder\translateable\TranslateableBehavior;

/**
 * ...
 * @property string $title
 * @property string $body
 * ...
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'translateable' => [
                'class' => TranslateableBehavior::className(),
                'translationAttributes' => ['title', 'body'],
                // translationRelation => 'translations',
                // translationLanguageAttribute => 'language',
            ],
        ];
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_INSERT | self::OP_UPDATE,
        ];
    }

    public function getTranslations()
    {
        return $this->hasMany(PostTranslation::className(), ['post_id' => 'id']);
    }
}
```

Model `PostTranslation` can be generated using Gii.

Usage
-----

[](#usage)

### Setting translations to the entity

[](#setting-translations-to-the-entity)

To set translations to the entity

```
$post = new Post();

// title attribute translation for default application language
$post->title = 'Post title';

// body attribute translation for default application language
$post->body = 'Post body';

// title attribute translation for German
$post->translate('de-DE')->title = 'Post titel';

// body attribute translation for German
$post->translate('de-DE')->body = 'Post inhalt';

// title attribute translation for Russian
$post->translate('ru-RU')->title = 'Заголовок поста';

// body attribute translation for Russian
$post->translate('ru-RU')->body = 'Тело поста';

// save post and its translations
$post->save();
```

### Getting translations from the entity

[](#getting-translations-from-the-entity)

To get translations from the entity

```
$posts = Post::find()->with('translations')->all();

foreach ($posts as $post) {
    // title attribute translation for default application language
    $title = $post->title;

    // body attribute translation for default application language
    $body = $post->body;

    // title attribute translation for German
    $germanTitle = $post->translate('de-DE')->title;

    // body attribute translation for German
    $germanBody = $post->translate('de-DE')->body;

    // title attribute translation for Russian
    $russianTitle = $post->translate('ru-RU')->title;

    // body attribute translation for Russian
    $russianBody = $post->translate('ru-RU')->body;
}
```

### Checking for translations in the entity

[](#checking-for-translations-in-the-entity)

To check translations in the entity

```
$post = Post::findOne(1);

// checking for default application language translation
$result = $post->hasTranslation();

// checking for German translation
$result = $post->hasTranslation('de-DE');

// checking for Russian translation
$result = $post->hasTranslation('ru-RU');
```

Advanced usage
--------------

[](#advanced-usage)

### Collecting tabular input

[](#collecting-tabular-input)

Example of view form

```
