Yii Framework Forum: I18Nactiverecord - A Multilingual Active Record - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

I18Nactiverecord - A Multilingual Active Record Rate Topic: -----

#1 User is offline   rontiki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-March 11

Posted 05 October 2012 - 11:03 PM

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.

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)
);




$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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users