PHPackages                             neam/yii-i18n-attribute-messages - 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-attribute-messages

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

neam/yii-i18n-attribute-messages
================================

Transparent attribute translation for ActiveRecords, leveraging Yii's built-in translation features for translated field contents.

0.1.0(12y ago)16781MITPHPPHP &gt;=5.0.0

Since Nov 25Pushed 11y ago4 watchersCompare

[ Source](https://github.com/neam/yii-i18n-attribute-messages)[ Packagist](https://packagist.org/packages/neam/yii-i18n-attribute-messages)[ Docs](https://github.com/neam/yii-i18n-attribute-messages)[ RSS](/packages/neam-yii-i18n-attribute-messages/feed)WikiDiscussions develop Synced 3w ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Yii Extension: I18nAttributeMessages
====================================

[](#yii-extension-i18nattributemessages)

Transparent attribute translation for ActiveRecords, leveraging Yii's built-in translation features to retrieve translated attribute contents.

All you'll need to do is to rename the fields from `$book->title` to `$book->_title` in your database. The included console command scans your database and configuration and creates a migration for all necessary renames.

The behavior then transparently turns `$book->title` into `Yii:t('attributes.Book.title', $book->_title)` and `$book->title_de` into `Yii:t('attributes.Book.title', $book->_title, array(), null, 'de')`, while providing transparent saving of translations simply by assigning and saving these attributes in the model (Note: CDbMessageSource only).

Features
--------

[](#features)

- Eases the translation of user-generated content in a project
- Eases the creation of UI for translators to perform translation work
- Works with any Yii-compatible message source when retrieving translations
- Saving of translations when using CDbMessageSource
- Console command automatically creates migrations for the necessary database changes
- The source message is left in the model for Gii compatibility (generated models will have the correct validation rules and field order for the translated attributes)
- Rigorous unit tests
- Use with any number of attributes/languages without worrying about database restrictions on row size and/or column counts being exceeded

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

[](#limitations)

Not ideal for translated attributes that are supposed to be native in the active records' database tables, such as translated foreign keys, or multilingual look-up/search columns. Use  for those attributes instead.

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

[](#requirements)

- Yii 1.1 or above
- Use of Yii console

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-attribute-messages"
    },
    ...
],
"require":{
    "neam/yii-i18n-attribute-messages":"dev-master",
    ...
},

```

Then install through composer:

```
php composer.phar update neam/yii-i18n-attribute-messages

```

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

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

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

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

```

### Import the behavior in main.php

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

```
'import' => array(
    ...
    'i18n-attribute-messages.behaviors.I18nAttributeMessagesBehavior',
    ...
),

```

### Reference the console command in console.php

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

```
'commandMap' => array(
    ...
    'i18n-attribute-messages'    => array(
        'class' => 'i18n-attribute-messages.commands.I18nAttributeMessagesCommand',
    ),
    ...
),

```

### 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-attribute-messages' => array(
             'class' => 'I18nAttributeMessagesBehavior',

             /* The multilingual attributes */
             'translationAttributes' => array(
                  'title',
                  'slug',
                  'book_id',
                  //'etc',
             ),

            /* An array of allowed language/locale ids that are to be used as suffixes, such as title_en, title_de etc */
            //'languageSuffixes' => array_keys(Yii::app()->params["languages"]),

            /* Configure if you want to use another translation component for this behavior. Default is 'messages' */
            //'messageSourceComponent' => 'attributeMessages',

        ),
    );
}

```

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

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

```
./yiic i18n-attribute-messages process

```

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, which will be the placed that the source content is stored (the content that is to be translated).

Sample migration file:

```

```

Note: This field is generated automatically by Gii.

#### Creating an input to set/update the swedish translation of the field "title"

[](#creating-an-input-to-setupdate-the-swedish-translation-of-the-field-title)

```

```

Hint: You might want to display the source language content next to the translation field, like so:

```

    :

```

Also, don't forget to adjust the validation rules (safe, required, etc) for the virtual translation fields.

#### Creating an input to set/update the current app language's translation of the field "title"

[](#creating-an-input-to-setupdate-the-current-app-languages-translation-of-the-field-title)

```

```

### More examples

[](#more-examples-1)

Simply look at any other examples of form building in Yii. Since the translated attributes are ordinary model attributes, you may use core or third-party extensions that save to and read from model attributes for constructing your translation UI.

Changelog
---------

[](#changelog)

### 0.1.0

[](#010)

- Eases the translation of user-generated content in a project
- Eases the creation of UI for translators to perform translation work
- Works with any Yii-compatible message source when retrieving translations
- Saving of translations when using CDbMessageSource
- Console command automatically creates migrations for the necessary database changes
- The source message is left in the model for Gii compatibility (generated models will have the correct validation rules and field order for the translated attributes)
- Rigorous unit tests

### 0.0.0

[](#000)

- Forked  v0.3.1

Testing the extension
---------------------

[](#testing-the-extension)

### One-time preparations

[](#one-time-preparations)

Switch to the extension's root directory

```
cd vendor/neam/yii-i18n-attribute-messages

```

Create a database called yiam\_test in your local mysql server installation. Create a user called yiam\_test with yiam\_test as the password and make sure that this user has access to the local database.

After this, you can run the following routine to test the extension:

### Test the command

[](#test-the-command)

#### 1. Set-up the test database

[](#1-set-up-the-test-database)

Load tests/db/unmodified.sql into the database.

#### 2. Run the console command

[](#2-run-the-console-command)

```
tests/app/protected/yiic i18n-attribute-messages process

```

#### 3. Apply the migration

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

```
tests/app/protected/yiic migrate

```

### Test the behavior

[](#test-the-behavior)

Run the unit tests

```
php codecept.phar run unit

```

You should get output similar to:

```
Codeception PHP Testing Framework v1.6.2
Powered by PHPUnit 3.7.19 by Sebastian Bergmann.

Suite unit started
Trying to ensure empty db (BasicTest::ensureEmptyDb) - Ok
Trying to ensure known source language (BasicTest::ensureKnownSourceLanguage) - Ok
Trying to see behavior (BasicTest::seeBehavior) - Ok
Trying to interpret language suffix (BasicTest::interpretLanguageSuffix) - Ok
Trying to get (BasicTest::get) - Ok
Trying to set without suffix (BasicTest::setWithoutSuffix) - Ok
Trying to set with suffix (BasicTest::setWithSuffix) - Ok
Trying to save single with source message (BasicTest::saveSingleWithSourceMessage) - Ok
Trying to save single without source message (BasicTest::saveSingleWithoutSourceMessage) - Ok
Trying to fetch single without suffix (BasicTest::fetchSingleWithoutSuffix) - Ok
Trying to reuse previous translation (BasicTest::reusePreviousTranslation) - Ok
Trying to update existing (BasicTest::updateExisting) - Ok
Trying to further fallback behavior tests (BasicTest::furtherFallbackBehaviorTests) - Ok
Trying to test test suite (EmptyTest::testTestSuite) - Ok

Time: 0 seconds, Memory: 14.25Mb

OK (14 tests, 124 assertions)

```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

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

Unknown

Total

1

Last Release

4601d ago

### Community

Maintainers

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

---

Top Contributors

[![motin](https://avatars.githubusercontent.com/u/793037?v=4)](https://github.com/motin "motin (49 commits)")[![cniska](https://avatars.githubusercontent.com/u/1044868?v=4)](https://github.com/cniska "cniska (3 commits)")

---

Tags

i18ntranslationyii

### Embed Badge

![Health badge](/badges/neam-yii-i18n-attribute-messages/health.svg)

```
[![Health](https://phpackages.com/badges/neam-yii-i18n-attribute-messages/health.svg)](https://phpackages.com/packages/neam-yii-i18n-attribute-messages)
```

###  Alternatives

[jenssegers/date

A date library to help you work with dates in different languages

1.8k11.5M83](/packages/jenssegers-date)[gettext/gettext

PHP gettext manager

70232.6M117](/packages/gettext-gettext)[fightbulc/moment

Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js

9683.4M13](/packages/fightbulc-moment)[jms/translation-bundle

Puts the Symfony Translation Component on steroids

44311.1M75](/packages/jms-translation-bundle)[lexik/translation-bundle

This bundle allows to import translation files content into the database and provide a GUI to edit translations.

4482.8M19](/packages/lexik-translation-bundle)[sonata-project/translation-bundle

SonataTranslationBundle

771.7M11](/packages/sonata-project-translation-bundle)

PHPackages © 2026

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