ソースを参照

Add ability to use custom po files inside plugins

Alejandro Ibarra 3 年 前
コミット
e0177f93fa

+ 21 - 8
src/I18n/MessagesFileLoader.php

@@ -38,6 +38,13 @@ class MessagesFileLoader
     protected $_name;
 
     /**
+     * The package (domain) plugin
+     *
+     * @var string
+     */
+    protected $_plugin;
+
+    /**
      * The locale to load for the given package.
      *
      * @var string
@@ -93,6 +100,13 @@ class MessagesFileLoader
     public function __construct(string $name, string $locale, string $extension = 'po')
     {
         $this->_name = $name;
+        // If space is not added after slash, the character after it remains lowercased
+        $pluginName = Inflector::camelize(str_replace('/', '/ ', $this->_name));
+        if (strpos($this->_name, '.')) {
+            [$this->_plugin, $this->_name] = pluginSplit($pluginName);
+        } elseif (Plugin::isLoaded($pluginName)) {
+            $this->_plugin = $pluginName;
+        }
         $this->_locale = $locale;
         $this->_extension = $extension;
     }
@@ -159,6 +173,13 @@ 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;
@@ -169,14 +190,6 @@ class MessagesFileLoader
             }
         }
 
-        // If space is not added after slash, the character after it remains lowercased
-        $pluginName = Inflector::camelize(str_replace('/', '/ ', $this->_name));
-        if (Plugin::isLoaded($pluginName)) {
-            $basePath = App::path('locales', $pluginName)[0];
-            foreach ($folders as $folder) {
-                $searchPaths[] = $basePath . $folder . DIRECTORY_SEPARATOR;
-            }
-        }
 
         return $searchPaths;
     }

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

@@ -167,6 +167,12 @@ class I18nTest extends TestCase
             'String 1 (from plugin three)',
             $translator->translate('String 1')
         );
+
+        $translator = I18n::getTranslator('company/test_plugin_three.custom');
+        $this->assertSame(
+            'String 2 (from plugin three)',
+            $translator->translate('String 2')
+        );
     }
 
     /**

+ 16 - 0
tests/test_app/Plugin/Company/TestPluginThree/resources/locales/en/custom.po

@@ -0,0 +1,16 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: CakePHP Testsuite\n"
+"POT-Creation-Date: 2008-05-15 02:51-0700\n"
+"PO-Revision-Date: \n"
+"Last-Translator: CakePHP I18N & I10N Team <i10n.cakephp@gmail.com>\n"
+"Language-Team: CakePHP I18N & I10N Team <i10n.cakephp@gmail.com>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=n != 1;\n"
+"X-Poedit-Language: Two Forms of Plurals\n"
+"X-Poedit-SourceCharset: utf-8\n"
+
+msgid "String 2"
+msgstr "String 2 (from plugin three)"