浏览代码

Merge pull request #10427 from Robertroid/master

TranslateBehavior's method TranslationField now returns standard table alias for default locale
Mark Story 9 年之前
父节点
当前提交
d43f03c0ed
共有 2 个文件被更改,包括 42 次插入3 次删除
  1. 3 0
      src/ORM/Behavior/TranslateBehavior.php
  2. 39 3
      tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

+ 3 - 0
src/ORM/Behavior/TranslateBehavior.php

@@ -436,6 +436,9 @@ class TranslateBehavior extends Behavior implements PropertyMarshalInterface
     public function translationField($field)
     {
         $table = $this->_table;
+        if ($this->locale() === $this->getConfig('defaultLocale')) {
+            return $table->aliasField($field);
+        }
         $associationName = $table->getAlias() . '_' . $field . '_translation';
 
         if ($table->associations()->has($associationName)) {

+ 39 - 3
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -328,11 +328,47 @@ class TranslateBehaviorTest extends TestCase
     public function testTranslationFieldForTranslatedFields()
     {
         $table = TableRegistry::get('Articles');
-        $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body'],
+            'defaultLocale' => 'en_US'
+        ]);
+
+        $expectedSameLocale = 'Articles.title';
+        $expectedOtherLocale = 'Articles_title_translation.content';
 
-        $expected = 'Articles_title_translation.content';
         $field = $table->translationField('title');
-        $this->assertSame($expected, $field);
+        $this->assertSame($expectedSameLocale, $field);
+
+        I18n::locale('es_ES');
+        $field = $table->translationField('title');
+        $this->assertSame($expectedOtherLocale, $field);
+
+        I18n::locale('en');
+        $field = $table->translationField('title');
+        $this->assertSame($expectedOtherLocale, $field);
+
+        $table->removeBehavior('Translate');
+
+        $table->addBehavior('Translate', [
+            'fields' => ['title', 'body'],
+            'defaultLocale' => 'de_DE'
+        ]);
+
+        I18n::locale('de_DE');
+        $field = $table->translationField('title');
+        $this->assertSame($expectedSameLocale, $field);
+
+        I18n::locale('en_US');
+        $field = $table->translationField('title');
+        $this->assertSame($expectedOtherLocale, $field);
+
+        $table->locale('de_DE');
+        $field = $table->translationField('title');
+        $this->assertSame($expectedSameLocale, $field);
+
+        $table->locale('es');
+        $field = $table->translationField('title');
+        $this->assertSame($expectedOtherLocale, $field);
     }
 
     /**