Permalink
Browse files

Implemented translations saving in the TranslateBehavior

  • Loading branch information...
1 parent 01d196f commit 8935530d527f8a701c985457ba678156cd69c708 @lorenzo lorenzo committed Feb 10, 2014
Showing with 72 additions and 0 deletions.
  1. +37 −0 src/Model/Behavior/TranslateBehavior.php
  2. +35 −0 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
@@ -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
@@ -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.