Browse Source

Logic for loading the .po files from APP and plugins

Jose Lorenzo Rodriguez 11 years ago
parent
commit
ff8e5d23d1
2 changed files with 91 additions and 36 deletions
  1. 14 36
      src/I18n/I18n.php
  2. 77 0
      src/I18n/MessageLoader.php

+ 14 - 36
src/I18n/I18n.php

@@ -14,11 +14,13 @@
  */
 namespace Cake\I18n;
 
-use Aura\Intl\PackageLocator;
+use Aura\Intl\Exception as LoadException;
 use Aura\Intl\FormatterLocator;
+use Aura\Intl\Package;
+use Aura\Intl\PackageLocator;
 use Aura\Intl\TranslatorFactory;
 use Aura\Intl\TranslatorLocator;
-use Aura\Intl\Package;
+
 /**
  * I18n handles translation of Text and time format strings.
  *
@@ -44,31 +46,27 @@ class I18n {
 			static::$_defaultLocale
 		);
 
-		static::attachDefaults($translators);
 		return static::$_collection = $translators;
 	}
 
 	public static function translator($package = 'default', $locale = null, callable $loader = null) {
 		if ($loader !== null) {
-			$packages = $translators->getPackages();
+			$packages = static::translators()->getPackages();
 			$locale = $locale ?: static::$_defaultLocale;
 			$packages->set($package, $locale, $loader);
 			return;
 		}
 
-		return static::translators()->get($package);
-	}
+		if ($locale) {
+			static::translators()->setLocale($locale);
+		}
 
-	public static function attachDefaults(TranslatorLocator $translators) {
-		$packages = $translators->getPackages();
-		$packages->set('default', static::$_defaultLocale, function() {
-			$package = new Package;
-			$package->setMessages([
-				'FOO' => 'The text for "foo."',
-				'BAR' => 'The text for "bar."'
-			]);
-			return $package;
-		});
+		try {
+			return static::translators()->get($package);
+		} catch (LoadException $e) {
+			static::translator($package, $locale, new MessageLoader($package, $locale));
+			return static::translators()->get($package);
+		}
 	}
 
 /**
@@ -89,24 +87,4 @@ class I18n {
 		
 	}
 
-/**
- * Clears the domains internal data array. Useful for testing i18n.
- *
- * @return void
- */
-	public static function clear() {
-		$self = I18n::getInstance();
-		$self->_domains = array();
-	}
-
-/**
- * Get the loaded domains cache.
- *
- * @return array
- */
-	public static function domains() {
-		$self = I18n::getInstance();
-		return $self->_domains;
-	}
-
 }

+ 77 - 0
src/I18n/MessageLoader.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\I18n;
+
+use Aura\Intl\Package;
+use Cake\I18n\Loader\PoFileLoader;
+use Cake\Core\Plugin;
+use Cake\Utility\Inflector;
+
+/**
+ * 
+ *
+ */
+class MessageLoader {
+
+	protected $_name;
+
+	protected $_locale;
+
+	protected $basePath;
+
+	public function __construct($name, $locale) {
+		$this->_name = $name;
+		$this->_locale = $locale;
+
+		$pluginName = Inflector::camelize($name);
+		$this->_basePath = APP . 'Locale' . DS;
+
+		if (Plugin::loaded($pluginName)) {
+			$this->_basePath = Plugin::path($pluginName) . 'Locale' . DS;
+		}
+	}
+
+	public function __invoke() {
+		$package = new Package;
+		$folder = $this->translationsFolder();
+
+		if (!$folder || !is_file($folder . $this->_name . '.po')) {
+			return $package;
+		}
+
+		$messages = (new PoFileLoader)->parse($folder . $this->_name . '.po');
+		$package->setMessages($messages);
+		return $package;
+	}
+
+	public function translationsFolder() {
+		$locale = locale_parse($this->_locale) + ['region' => null];
+
+		$folders = [
+			implode('_', [$locale['language'], $locale['region']]),
+			$locale['language']
+		];
+
+		foreach ($folders as $folder) {
+			$path = $this->_basePath  . $folder . DS . 'LC_MESSAGES' . DS;
+			if (is_dir($path)) {
+				return $path;
+			}
+		}
+
+		return false;
+	}
+
+}