Skip to content

Commit

Permalink
Adding support for specifying the locale using the entity property
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 10, 2014
1 parent f7553b0 commit 1d1bb62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -149,7 +149,7 @@ public function beforeFind(Event $event, $query) {
}

public function beforeSave(Event $event, $entity) {
$locale = $this->locale();
$locale = $entity->get('_locale') ?: $this->locale();

if (!$locale) {
return;
Expand Down Expand Up @@ -180,6 +180,8 @@ public function beforeSave(Event $event, $entity) {
}

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

foreach ($fields as $field) {
$entity->dirty($field, false);
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -505,6 +505,7 @@ public function testInsertNewTranslations() {
$this->assertEquals(1, $article->get('id'));
$article->set('title', 'Le titre');
$table->save($article);
$this->assertEquals('fra', $article->get('_locale'));

$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
Expand All @@ -520,4 +521,31 @@ public function testInsertNewTranslations() {
$this->assertEquals('Le contenu', $article->get('body'));
}

/**
* Tests that it is possible to use the _locale property to specify the language
* to use for saving an entity
*
* @return void
*/
public function testUpdateTranslationWithLocaleInEntity() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
$article->set('_locale', 'fra');
$article->set('title', 'Le titre');
$table->save($article);

$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
$this->assertEquals('First Article', $article->get('title'));
$this->assertEquals('First Article Body', $article->get('body'));

$table->locale('fra');
$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
$this->assertEquals('Le titre', $article->get('title'));
$this->assertEquals('First Article Body', $article->get('body'));
}

}

0 comments on commit 1d1bb62

Please sign in to comment.