Browse Source

Fixing i18n when two Tables have the same name

Inflector::underscore is the only (Cake) way to ensure uniqueness in the TableRegistry.
Patrick Conroy 11 years ago
parent
commit
ffb0dae395

+ 11 - 1
src/ORM/Behavior/TranslateBehavior.php

@@ -23,6 +23,7 @@ use Cake\ORM\Entity;
 use Cake\ORM\Query;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
+use Cake\Utility\Inflector;
 
 /**
  * This behavior provides a way to translate dynamic data by keeping translations
@@ -99,7 +100,16 @@ class TranslateBehavior extends Behavior
      */
     public function initialize(array $config)
     {
-        $this->_translationTable = TableRegistry::get($this->_config['translationTable']);
+        $translationAlias = Inflector::slug($this->_config['translationTable'], '_');
+
+        if (!TableRegistry::exists($translationAlias)) {
+            $this->_translationTable = TableRegistry::get($translationAlias, [
+                'className' => $this->_config['translationTable']
+            ]);
+        } else {
+            $this->_translationTable = TableRegistry::get($translationAlias);
+        }
+
         $this->setupFieldAssociations(
             $this->_config['fields'],
             $this->_config['translationTable'],

+ 1 - 1
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -93,7 +93,7 @@ class TranslateBehaviorTest extends TestCase
         $items = $table->associations();
         $i18n = $items->getByProperty('_i18n');
 
-        $this->assertEquals('\TestApp\Model\Table\I18nTable', $i18n->name());
+        $this->assertEquals('TestApp_Model_Table_I18nTable', $i18n->name());
         $this->assertEquals('custom_i18n_table', $i18n->target()->table());
         $this->assertEquals('test_custom_i18n_datasource', $i18n->target()->connection()->configName());
     }