Browse Source

Fix message path loading order

Jens Mischer 2 years ago
parent
commit
0243c5d219

+ 7 - 7
src/I18n/MessagesFileLoader.php

@@ -174,13 +174,6 @@ class MessagesFileLoader
 
         $searchPaths = [];
 
-        if ($this->_plugin && Plugin::isLoaded($this->_plugin)) {
-            $basePath = App::path('locales', $this->_plugin)[0];
-            foreach ($folders as $folder) {
-                $searchPaths[] = $basePath . $folder . DIRECTORY_SEPARATOR;
-            }
-        }
-
         $localePaths = App::path('locales');
         if (empty($localePaths) && defined('APP')) {
             $localePaths[] = ROOT . 'resources' . DIRECTORY_SEPARATOR . 'locales' . DIRECTORY_SEPARATOR;
@@ -191,6 +184,13 @@ class MessagesFileLoader
             }
         }
 
+        if ($this->_plugin && Plugin::isLoaded($this->_plugin)) {
+            $basePath = App::path('locales', $this->_plugin)[0];
+            foreach ($folders as $folder) {
+                $searchPaths[] = $basePath . $folder . DIRECTORY_SEPARATOR;
+            }
+        }
+
         return $searchPaths;
     }
 }

+ 14 - 0
tests/TestCase/I18n/I18nTest.php

@@ -191,12 +191,26 @@ class I18nTest extends TestCase
     {
         $this->loadPlugins([
             'TestTheme' => [],
+            'TestPluginTwo' => []
         ]);
+
         $translator = I18n::getTranslator('test_theme');
         $this->assertSame(
             'translated',
             $translator->translate('A Message')
         );
+
+        $translator = I18n::getTranslator('test_plugin_two');
+        $this->assertSame(
+            'Test Message (from app)',
+            $translator->translate('Test Message')
+            );
+
+        $translator = I18n::getTranslator('test_plugin_two.custom');
+        $this->assertSame(
+            'Test Custom (from test plugin two)',
+            $translator->translate('Test Custom')
+            );
     }
 
     /**

+ 41 - 0
tests/TestCase/I18n/MessagesFileLoaderTest.php

@@ -78,4 +78,45 @@ class MessagesFileLoaderTest extends TestCase
         $package = $loader();
         $this->assertFalse($package);
     }
+    
+    /**
+     * Testing MessagesFileLoader::translationsFilder array sequence
+     */
+    public function testTranslationFoldersSequence(): void
+    {
+        $this->loadPlugins([
+            'TestPluginTwo' => []
+        ]);
+        $loader = new MessagesFileLoader('test_plugin_two', 'en');
+
+        $expected = [
+            ROOT . DS . 'tests' . DS . 'test_app' . DS . 'resources' . DS . 'locales' . DS . 'en_' . DS,
+            ROOT . DS . 'tests' . DS . 'test_app' . DS . 'resources' . DS . 'locales' . DS . 'en' . DS,
+            ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS . 'resources' . DS . 'locales' . DS . 'en_' . DS,
+            ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS . 'resources' . DS . 'locales' . DS . 'en' . DS,
+        ];
+        $result = $loader->translationsFolders();
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
+     * Testing plugin override from app
+     */
+    public function testAppOverridesPlugin(): void
+    {
+        $this->loadPlugins([
+            'TestPlugin' => [],
+            'TestPluginTwo' => []
+        ]);
+
+        $loader = new MessagesFileLoader('test_plugin', 'en');
+        $package  = $loader();
+        $messages = $package->getMessages();
+        $this->assertSame('Plural Rule 1 (from plugin)', $messages['Plural Rule 1']['_context']['']);
+        
+        $loader = new MessagesFileLoader('test_plugin_two', 'en');
+        $package  = $loader();
+        $messages = $package->getMessages();
+        $this->assertSame('Test Message (from app)', $messages['Test Message']['_context']['']);
+    }
 }

+ 2 - 0
tests/test_app/Plugin/TestPluginTwo/resources/locales/en/custom.po

@@ -0,0 +1,2 @@
+msgid "Test Custom"
+msgstr "Test Custom (from test plugin two)"

+ 2 - 0
tests/test_app/Plugin/TestPluginTwo/resources/locales/en/test_plugin_two.po

@@ -0,0 +1,2 @@
+msgid "Test Message"
+msgstr "Test Message (from test plugin two)"

+ 2 - 0
tests/test_app/resources/locales/en/test_plugin_two.po

@@ -0,0 +1,2 @@
+msgid "Test Message"
+msgstr "Test Message (from app)"