Skip to content

Commit

Permalink
Implemented translations saving in the TranslateBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 10, 2014
1 parent 01d196f commit 8935530
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -148,6 +148,43 @@ public function beforeFind(Event $event, $query) {
}, $query::PREPEND);
}

public function beforeSave(Event $event, $entity) {
$locale = $this->locale();

if (!$locale) {
return;
}

$values = $entity->extract($this->config()['fields'], true);
$fields = array_keys($values);
$key = $entity->get(current((array)$this->_table->primaryKey()));

$preexistent = TableRegistry::get('I18n')->find()
->select(['id', 'field'])
->where(['field IN' => $fields, 'locale' => $locale, 'foreign_key' => $key])
->indexBy('field');

$modified = [];
foreach ($preexistent as $field => $translation) {
$translation->set('content', $values[$field]);
$modified[$field] = $translation;
}

$new = array_diff_key($values, $modified);
foreach ($new as $field => $content) {
$new[$field] = new Entity(compact('locale', 'field', 'content'), [
'useSetters' => false,
'markNew' => true
]);
}

$entity->set('_i18n', array_values($modified + $new));

foreach ($fields as $field) {
$entity->dirty($field, false);
}
}

/**
* Sets all future finds for the bound table to also fetch translated fields for
* the passed locale. If no value is passed, it returns the currently configured
Expand Down
35 changes: 35 additions & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -456,4 +456,39 @@ public function testFindSingleLocaleBelongsto() {
$this->assertEquals($expected, $results);
}

/**
* Tests that updating an existing record translations work
*
* @return void
*/
public function testUpdateSingleLocale() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$table->locale('eng');
$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
$article->set('title', 'New translated article');
$table->save($article);

$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
$this->assertEquals('New translated article', $article->get('title'));
$this->assertEquals('Content #1', $article->get('body'));

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

$table->locale('eng');
$article->set('title', 'Wow, such translated article');
$article->set('body', 'A translated body');
$table->save($article);

$article = $table->find()->first();
$this->assertEquals(1, $article->get('id'));
$this->assertEquals('Wow, such translated article', $article->get('title'));
$this->assertEquals('A translated body', $article->get('body'));
}

}

0 comments on commit 8935530

Please sign in to comment.