Browse Source

Now plugin translations can be overridden by files put in the app folder

Jose Lorenzo Rodriguez 11 years ago
parent
commit
ba85edc757
2 changed files with 38 additions and 14 deletions
  1. 23 14
      src/I18n/MessagesFileLoader.php
  2. 15 0
      tests/TestCase/I18n/I18nTest.php

+ 23 - 14
src/I18n/MessagesFileLoader.php

@@ -104,10 +104,19 @@ class MessagesFileLoader {
  */
  */
 	public function __invoke() {
 	public function __invoke() {
 		$package = new Package('default');
 		$package = new Package('default');
-		$folder = $this->translationsFolder();
+		$folders = $this->translationsFolders();
 		$ext = $this->_extension;
 		$ext = $this->_extension;
+		$file = false;
 
 
-		if (!$folder || !is_file($folder . $this->_name . ".$ext")) {
+		foreach ($folders as $folder) {
+			$path = $folder . $this->_name . ".$ext";
+			if (is_file($path)) {
+				$file = $path;
+				break;
+			}
+		}
+
+		if (!$file) {
 			return $package;
 			return $package;
 		}
 		}
 
 
@@ -118,19 +127,18 @@ class MessagesFileLoader {
 			throw new \RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
 			throw new \RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
 		}
 		}
 
 
-		$messages = (new $class)->parse($folder . $this->_name . ".$ext");
+		$messages = (new $class)->parse($file);
 		$package->setMessages($messages);
 		$package->setMessages($messages);
 		return $package;
 		return $package;
 	}
 	}
 
 
 /**
 /**
- * Returns the folder where the file should be looked for according to the locale
+ * Returns the folders where the file should be looked for according to the locale
  * and package name.
  * and package name.
  *
  *
- * @return string|boolean The folder where the file should be looked for or false
- * if it does not exists.
+ * @return array The list of folders where the translation file should be looked for
  */
  */
-	public function translationsFolder() {
+	public function translationsFolders() {
 		$locale = Locale::parseLocale($this->_locale) + ['region' => null];
 		$locale = Locale::parseLocale($this->_locale) + ['region' => null];
 
 
 		$folders = [
 		$folders = [
@@ -140,19 +148,20 @@ class MessagesFileLoader {
 
 
 		$pluginName = Inflector::camelize($this->_name);
 		$pluginName = Inflector::camelize($this->_name);
 		$basePath = APP . 'Locale' . DS;
 		$basePath = APP . 'Locale' . DS;
+		$searchPath = [];
 
 
-		if (Plugin::loaded($pluginName)) {
-			$basePath = Plugin::path($pluginName) . 'src' . DS . 'Locale' . DS;
+		foreach ($folders as $folder) {
+			$searchPath[] = $basePath . $folder . DS;
 		}
 		}
 
 
-		foreach ($folders as $folder) {
-			$path = $basePath . $folder . DS;
-			if (is_dir($path)) {
-				return $path;
+		if (Plugin::loaded($pluginName)) {
+			$basePath = Plugin::path($pluginName) . 'src' . DS . 'Locale' . DS;
+			foreach ($folders as $folder) {
+				$searchPath[] = $basePath . $folder . DS;
 			}
 			}
 		}
 		}
 
 
-		return false;
+		return $searchPath;
 	}
 	}
 
 
 }
 }

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

@@ -146,6 +146,21 @@ class I18nTest extends TestCase {
 	}
 	}
 
 
 /**
 /**
+ * Tests that messages messages from a plugin can be automatically
+ * overridden by messages in app
+ *
+ * @return void
+ */
+	public function testPluginOverride() {
+		Plugin::load('TestTheme');
+		$translator = I18n::translator('test_theme');
+		$this->assertEquals(
+			'translated',
+			$translator->translate('A Message')
+		);
+	}
+
+/**
  * Tests the defaultLocale method
  * Tests the defaultLocale method
  *
  *
  * @return void
  * @return void