Description
It's necessary to create Setup (or some behavior) class that would give api to easy implement modules with fields that must be available on different languages. This class can create some additinal tables automatically for example i have table "news" and i want that fields "title" and "description" will be available for a few languages (the api of business models and collection must be the same as for non-translatable table). Table "news" has few fields. There are id, title, description, is_active, created_at.
In the install file of module i create a ddl table object and set some parameter (or behavior object) that responsible for the creation of the additional table with template name %base_tablename%_translation (e.g. news_translation). This table contains all translatable fields (e.g. title, description) id and store_id. Id field is the same that is in base table (e.g. news). Also you need to add primary index for (id, storeid).
For example,
$table = $installer->getConnection()
->newTable($installer->getTable('news'))
->addColumn('block_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
'identity' => true,
'nullable' => false,
'primary' => true,
), 'News ID')
->addColumn('title', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
'nullable' => false,
), 'News Title')
->addColumn('description', Varien_Db_Ddl_Table::TYPE_TEXT, '2M', array(
), 'News Description')
->addColumn('creation_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
), 'Block Creation Time')
->addColumn('update_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
), 'Block Modification Time')
->addColumn('is_active', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
'nullable' => false,
'default' => '1',
), 'Is Block Active')
->setComment('CMS Block Table');
// Add Table Behavior
$table->addBehavior(new TranslatableBehavior(array('title', 'description'));
$installer->getConnection()->createTable($table);
This code must generate the following SQL:
CREATE TABLE `news` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`creation_time` datetime DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '1'
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='CMS Blocks'
CREATE TABLE `news_translation` (
`id` smallint(6) NOT NULL,
`title` varchar(255) not null,
`description` text,
`store_id` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`,`store_id`),
KEY `FK_CMS_BLOCK_STORE_STORE` (`store_id`),
CONSTRAINT `FK_CMS_BLOCK_STORE_BLOCK` FOREIGN KEY (`id`) REFERENCES `news` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_CMS_BLOCK_STORE_STORE` FOREIGN KEY (`store_id`) REFERENCES `core_store` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='CMS Blocks to Stores';
To work with models withous changing api:
// collection
$collection = new TranslatableCollection();
$collection->setStoreId(Mage::app()->getStore()->getId())->load();
// item
$item = new TranslatableModel();
$item->setStoreId(Mage::app()->getStore()->getId())->load($itemId);
echo $item->getTitle();
echo $item->getDescription();
It would look like a Doctrine I18n Behavior - http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/behaviors.html#i18n .