Skip to content

Commit

Permalink
Fixes issue where translations other than default were not saved.
Browse files Browse the repository at this point in the history
Fixes #4726
  • Loading branch information
lorenzo committed Sep 26, 2014
1 parent d5a189d commit b6ab4da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -173,6 +173,7 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
$options['associated'] = $newOptions + $options['associated'];

$this->_bundleTranslatedFields($entity);
$bundled = $entity->get('_i18n') ?: [];

if ($locale === $this->config('defaultLocale')) {
return;
Expand Down Expand Up @@ -204,7 +205,7 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
]);
}

$entity->set('_i18n', array_values($modified + $new));
$entity->set('_i18n', array_merge($bundled, array_values($modified + $new)));
$entity->set('_locale', $locale, ['setter' => false]);
$entity->dirty('_locale', false);

Expand Down
36 changes: 36 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -18,10 +18,19 @@
use Cake\Event\Event;
use Cake\I18n\I18n;
use Cake\Model\Behavior\TranslateBehavior;
use Cake\Model\Behavior\Translate\TranslateTrait;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
* Stub entity class
*/
class Article extends Entity {

use TranslateTrait;
}

/**
* Translate behavior test case
*/
Expand Down Expand Up @@ -758,4 +767,31 @@ public function testUseCountInFindTranslations() {
$this->assertNotEmpty($article->get('_translations'));
}

/**
* Tests that multiple translations saved when having a default locale
* are correclty saved
*
* @return void
*/
public function testSavingWithNonDefaultLocale() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$table->entityClass(__NAMESPACE__ . '\Article');
I18n::locale('fra');
$translations = [
'fra' => ['title' => 'Un article'],
'spa' => ['title' => 'Un artículo']
];

$article = $table->get(1);
foreach ($translations as $lang => $data) {
$article->translation($lang)->set($data, ['guard' => false]);
}

$table->save($article);
$article = $table->find('translations')->where(['Articles.id' => 1])->first();
$this->assertEquals('Un article', $article->translation('fra')->title);
$this->assertEquals('Un artículo', $article->translation('spa')->title);
}

}

0 comments on commit b6ab4da

Please sign in to comment.