I know this is old, but since it's unanswered and I got into some trouble figuring this out, here are my two cents:
scope, Cakephp 1.3.
first, you need an i18n record for every field -> record -> model in your app, of course only the models with the Translate behavior and only for the specified fields, so yes, you'll end up with duplicated content both in the actual model fields and the i18n table.
Also, you need existing translations for any language you specify for the find methods to return anything. This one got me confused, because I also added the translate behavior to an already developed project, with tons of data. Basically what I did was create a sync method in order to created every translation for every field for every model with the translate behavior. Should I add a new language in the future, I'll have to run it again.
here is the code for that:
$locales = array( 'spa', 'eng', 'por' );
$models = array( 'Event', 'News', 'Promotion', 'Shop', 'ShopCategory' );
foreach ( $models as $model )
{
$this->loadModel( $model );
$records = $this->{$model}->find( 'all', array(
'fields' => array( "*" ),
'contain' => FALSE
) );
$fields = $this->{$model}->actsAs['Translate'];
foreach ( $records as $record )
{
foreach ( $fields as $field )
{
foreach ( $locales as $locale )
{
$content = mysql_real_escape_string( $record[$model][$field] );
$this->{$model}->query( "INSERT INTO `i18n` (`locale`, `model`, `foreign_key`, `field`, `content`) VALUES('{$locale}', '{$model}', '{$record[$model]['id']}', '{$field}', '{$content}');" );
}
}
}
}
Tip: create an unique index in the i18n table with locale, model, foreign_key and field keys.
So, now that I have my original data replicated in all the languages, I need to be able to create new records with a give language. The problem is, if I create a new record in English, I should be able to update it in Spanish, but since there's no i18n record for Spanish, I cannot see the record in my CMS and I cannot edit it.
The way I solved this is to create i18n records for every language when creating new records, but only edit with the current language when editing records.
I used this modification for the Translate behavior:
http://bin.cakephp.org/view/1939852942
But most examples with this modification said that you have to create form inputs in the form:
echo $this->Form->input('name.eng');
echo $this->Form->input('name.spa');
echo $this->Form->input('description.eng');
echo $this->Form->input('description.spa');
I find this annoying, so I basically made a helper function in app_controller to convert translatable fields to this data structure:
function _expandLocalizations( $data )
{
foreach ( $data as $modelName => $modelData )
{
if ( !isset( $modelData[$this->{$modelName}->primaryKey] ) )
{
foreach ( $this->{$modelName}->actsAs['Translate'] as $field )
{
$translations = array();
foreach ( $this->languages as $language )
{
$translations[$language] = $this->data[$modelName][$field];
}
$data[$modelName][$field] = $translations;
}
}
}
return $data;
}
and in every add method:
if ( !empty( $this->data ) )
{
$this->News->set( $this->data );
if ( $this->News->validates() )
{
$this->News->create();
$data = $this->_expandLocalizations( $this->data );
$saved = $this->News->save( $data );
if ( $saved )
{
This way I can use traditional forms, and I don't need to change the view if I add new languages.
I need to modify the tradicional baked add method because I need the submitted data for validation, but the modified data for saving.
Finally, I followed this steps for changing the language within the site/admin: http://nuts-and-bolts-of-cakephp.com/2008/11/28/cakephp-url-based-language-switching-for-i18n-and-l10n-internationalization-and-localization/
So basically that's it.
1. You create translations for every data you already have in the DB, each copied to every language.
2. Create every translation for new records on create, all with the same value of course.
3. Edit every record in the selected translation.