Skip to content
Permalink
Browse files

Fixes issue where translations other than default were not saved.

Fixes #4726
  • Loading branch information...
lorenzo committed Sep 26, 2014
1 parent d5a189d commit b6ab4daa135c2d200e1b09f02bc4438de56ab14c
@@ -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;
@@ -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);
@@ -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
*/
@@ -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.
You can’t perform that action at this time.