I18Nactiverecord - A Multilingual Active Record

The i18nActiveRecord is an Active Record for representing internationalized relational data in Yii. It extends the ActiveRecord class by providing an integrated translation capability. You can use it as you would use any CActiveRecord. I wouldn’t want to write a whole lot here but you can read the full details on my blog.

Below is a snippet of what it can do.

[sql]

create table item(

id integer not null primary key auto_increment,

price double(10,2) not null

);

create table item_i18n(

i18n_id integer not null,

locale varchar(4) not null,

name varchar(128) not null,

description text,

primary key(i18n_id,locale)

);

[/sql]





$item = new Item();

$item->price = 2.99;


// add an English translation

$item->setLocale('en_US');

$item->name ='Microwave oven';


// add a French translation

$item->setLocale('fr_FR');

$item->name ='Four micro-ondes';


// save the item and its translations

$item->save();


// retrieve text and non-text translations directly from the main object

echo $item->price; // 12.99

$item->setLocale('en_US');

echo $item->name; // Microwave oven

$item->setLocale('fr_FR');

echo $item->name; // Four micro-ondes




$item->eachTranslation(function($itemi18n){

    //both english and french translation will get new name

    $itemi18n->name= "New name";

});


$item = Item::model()->findByPk(1);

// remove the French translation

$item->removeTranslation('fr_FR');


$items = Item::model()->findAll(); // one query to retrieve all items

$locale = 'en_US';

foreach ($items as $item) {

  echo $item->price;

  $item->setLocale($locale);

  echo $item->name; // one query to retrieve the English translation

}




$items = Item::model()

  ->withI18n('en_US')

  ->find(); // one query to retrieve both all items and their translations

foreach ($items as $item) {

  echo $item->price;

  echo $item->name; // no additional query

}

You can get detailed requirements also from there.