Home

Awesome

Translatable

Transparent attribute translation for ActiveRecords.

Features:

Here's a basic example:

<?php

// Load a record in application default language ...
$car = New Car;
$car->manufacturer_id = 123;

// Set a description, e.g. in english if this is the app language
$car->description = 'English description';

// Save both: the new car record and a related translation record
$car->save();

// Change language
$car->language = 'de';
$car->description = 'German description';

// We could again call save() here, but we only want to save the translation record
$car->saveTranslation();


// Load a record in a specific language
$car = Car::model()->language('de')->findByPk(1);

// Output: 'German description'
echo $car->description;

$car->language = 'en';

// Output: 'English description'
echo $car->description;

Installation

Simply extract the package to your protected/extensions directory and rename it to translatable.

How to use

1. Move translateable columns into a separate table

The behavior requires that you move all columns that you want to translate from the original table into a dedicated translation table. So if you have a table books and want to translate title and abstract, you need to modify yourd DB schema a little:

    +--------------+        +--------------+        +-------------------+
    |    books     |        |    books     |        | book_translations |
    +--------------+        +--------------+        +-------------------+
    |           id |        |           id |        |                id |
    | publisher_id |  --->  | publisher_id |   +    |           book_id |
    |    author_id |        |    author_id |        |          language |
    |        title |        |   created_at |        |             title |
    |     abstract |        |   updated_at |        |          abstract |
    |   created_at |        +--------------+        +-------------------+
    |   updated_at |
    +--------------+

2. Attach the behavior to your ActiveRecord model

With the above DB setup you can now attach the behavior to the Books ActiveRecord.

<?php
public function behaviors()
{
    return array(
        // IMPORTANT: Always use 'Translatable' as key
        'Translatable' => array(
            'class'                 => 'ext.translatable.Translatable',
            'translationAttributes' => array('title','abstract'),

            // Optional configuration with their defaults
            'translationRelation'   => 'translation',
            'languageColumn'        => 'language',
        ),
    );
}

3. Define a relation from your main record to your translation record

You also have to create a record for the translation table (e.g. BookTranslations) and define a HAS_MANY relationship for it in the Books model.

<?php
public function relations()
{
    return array(
        // IMPORTANT: Use the language column as `index`
        'translations' => array(self::HAS_MANY, 'BookTranslations', 'book_id', 'index'=>'language'),
    );
}

4. Create validation rules in your main record

You will also want to create some rules for these attributes in the Books record.

<?php
public function rules()
{
    return array(
        array('publisher_id,author_id,title,abstract', 'required'),
    );
}

Advanced examples

Use a fallback language

TODO

Use fallback columns

TODO

Tabular edit of several languages

TODO

API

Properties

Methods

NOTE: The fallbackLanguage can not be set in the behavior configuration. It's meant to be set on loaded models only.

About

The development of this extension was kindly sponsored by herzog kommunikation GmbH, Stuttgart, Germany.

Changelog

1.0.1

1.0.0