PHPackages                             neam/yii-i18n-columns - 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. neam/yii-i18n-columns

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

neam/yii-i18n-columns
=====================

Transparent attribute translation for ActiveRecords, without requiring lookup tables for translated field contents.

0.3.1(12y ago)71.4k3MITPHPPHP &gt;=5.0.0

Since Jul 9Pushed 11y ago5 watchersCompare

[ Source](https://github.com/neam/yii-i18n-columns)[ Packagist](https://packagist.org/packages/neam/yii-i18n-columns)[ Docs](https://github.com/neam/yii-i18n-columns)[ RSS](/packages/neam-yii-i18n-columns/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (2)Versions (7)Used By (0)

Yii Extension: I18nColumns
==========================

[](#yii-extension-i18ncolumns)

Transparent language/locale-dependent attributes and relations for ActiveRecords, without using lookup tables for translated field contents.

Ideal when translated attributes are supposed to be native in the active records' database tables, such as translated foreign keys, or multilingual look-up/search columns.

Features
--------

[](#features)

- Eases the creation of multilingual ActiveRecords in a project
- Automatically loads the application language by default
- Translations are stored directly in the model using separate columns for each language
- Console command automatically creates migrations for the necessary database changes
- Leverages Gii code generation to provide CRUD for translation work
- Not only translations - any attribute or relation that is dependent on language or locale can be managed with this extension
- Use when native attributes are desired, such as foreign keys, or to be able to transparently look up and search amongst translated attributes.

Limitations
-----------

[](#limitations)

Use only with a smaller number of attributes/languages or else database restrictions on row size and/or column counts will be exceeded. For the bulk of translated attributes, use  or [mike's translatable behavior](https://github.com/mikehaertl/translatable).

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

[](#requirements)

- Yii 1.1 or above
- Use of Yii console
- Use of Gii (preferably [Gtc](https://github.com/schmunk42/gii-template-collection/))
- MySQL 5.1.10+, SQL Server 2012 or similarly recent database (For the console command. The behavior itself works with any Yii-supported database)

Setup
-----

[](#setup)

### Download and install

[](#download-and-install)

Ensure that you have the following in your composer.json:

```
"repositories":[
    {
        "type": "vcs",
        "url": "https://github.com/neam/yii-i18n-columns"
    },
    ...
],
"require":{
    "neam/yii-i18n-columns":"dev-master",
    ...
},

```

Then install through composer:

```
php composer.phar update neam/yii-i18n-columns

```

If you don't use composer, clone or download this project into /path/to/your/app/vendor/neam/yii-i18n-columns

### Add Alias to both main.php and console.php

[](#add-alias-to-both-mainphp-and-consolephp)

```
'aliases' => array(
    ...
    'vendor'  => dirname(__FILE__) . '/../../vendor',
    'i18n-columns' => 'vendor.neam.yii-i18n-columns',
    ...
),

```

### Import the behavior in main.php

[](#import-the-behavior-in-mainphp)

```
'import' => array(
    ...
    'i18n-columns.behaviors.I18nColumnsBehavior',
    ...
),

```

### Reference the translate command in console.php

[](#reference-the-translate-command-in-consolephp)

```
'commandMap' => array(
    ...
    'i18n-columns'    => array(
        'class' => 'i18n-columns.commands.I18nColumnsCommand',
    ),
    ...
),

```

### Configure models to be multilingual

[](#configure-models-to-be-multilingual)

#### 1. Add the behavior to the models that you want multilingual

[](#1-add-the-behavior-to-the-models-that-you-want-multilingual)

```
public function behaviors()
{
    return array(
        'i18n-columns' => array(
             'class' => 'I18nColumnsBehavior',
             /* The multilingual attributes */
             'translationAttributes' => array(
                  'title',
                  'slug',
                  'image_id',
                  'etc',
             ),
            /* Specify multilingual belongsTo relations in the form 'RelatedModel' => array('relationName' => 'foreignKey') */
            'multilingualRelations' => array(
                'Image' => array('image' => 'image_id'),
            ),
        ),
    );
}

```

#### 2. Create migration from command line:

[](#2-create-migration-from-command-line)

`./yiic i18n-columns process`

Prior to this, you should already have configured a default language (`$config['language']`) and available languages (`$config['components']['langHandler']['languages']`) for your app.

Run with `--verbose` to see more detailed output.

#### 3. Apply the generated migration:

[](#3-apply-the-generated-migration)

`./yiic migrate`

This will rename the fields that are defined in translationAttributes to fieldname\_defaultlanguagecode and add columns for the remaining languages.

Sample migration file:

```
