Skip to content

Commit

Permalink
Logic for loading the .po files from APP and plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 13, 2014
1 parent 14ba631 commit ff8e5d2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 36 deletions.
50 changes: 14 additions & 36 deletions src/I18n/I18n.php
Expand Up @@ -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.
*
Expand All @@ -44,31 +46,27 @@ public static function translators() {
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);
}
}

/**
Expand All @@ -89,24 +87,4 @@ public static function translate($singular, $plural = null, $domain = null, $cou

}

/**
* 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 changes: 77 additions & 0 deletions 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;
}

}

0 comments on commit ff8e5d2

Please sign in to comment.