Skip to content

Commit

Permalink
Abstracting the old MessageLoader so that it can use any parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 20, 2014
1 parent 7204112 commit eeadc30
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/I18n/I18n.php
Expand Up @@ -64,7 +64,7 @@ public static function translator($package = 'default', $locale = null, callable
try {
return static::translators()->get($package);
} catch (LoadException $e) {
static::translator($package, $locale, new MessageLoader($package, $locale));
static::translator($package, $locale, new MessagesFileLoader($package, $locale));
return static::translators()->get($package);
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/I18n/MessageLoader.php → src/I18n/MessagesFileLoader.php
Expand Up @@ -15,25 +15,28 @@
namespace Cake\I18n;

use Aura\Intl\Package;
use Cake\I18n\Parser\PoFileParser;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Utility\Inflector;

/**
*
*
*
*/
class MessageLoader {
class MessagesFileLoader {

protected $_name;

protected $_locale;

protected $basePath;
protected $_extension;

protected $_basePath;

public function __construct($name, $locale) {
public function __construct($name, $locale, $extension = 'po') {
$this->_name = $name;
$this->_locale = $locale;
$this->_extension = $extension;

$pluginName = Inflector::camelize($name);
$this->_basePath = APP . 'Locale' . DS;
Expand All @@ -46,12 +49,20 @@ public function __construct($name, $locale) {
public function __invoke() {
$package = new Package;
$folder = $this->translationsFolder();
$ext = $this->_extension;

if (!$folder || !is_file($folder . $this->_name . '.po')) {
if (!$folder || !is_file($folder . $this->_name . ".$ext")) {
return $package;
}

$messages = (new PoFileParser)->parse($folder . $this->_name . '.po');
$name = ucfirst($ext);
$class = App::classname($name, 'I18n\Parser', 'FileParser');

if (!$class) {
throw new \RuntimeException(sprintf('Could not find class %s'), "{$name}FileParser");
}

$messages = (new $class)->parse($folder . $this->_name . ".$ext");
$package->setMessages($messages);
return $package;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -25,9 +25,16 @@
*/
class I18nTest extends TestCase {

/**
* Tests that a default translator is created and messages are parsed
* correclty
*
* @return void
*/
public function testDefaultTranslator() {
$translator = I18n::translator();
$this->assertInstanceOf('Aura\Intl\Translator', $translator);
$this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
}

}
2 changes: 1 addition & 1 deletion tests/TestCase/I18n/Parser/PoFileParserTest.php
Expand Up @@ -50,7 +50,7 @@ public function testParse() {
*/
public function testParseMultiLine() {
$parser = new PoFileParser;
$file = APP . 'Locale' . DS . 'po' . DS . 'LC_MESSAGES' . DS . 'default.po';
$file = APP . 'Locale' . DS . 'en' . DS . 'LC_MESSAGES' . DS . 'default.po';
$messages = $parser->parse($file);
$this->assertCount(12, $messages);
$this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]);
Expand Down

0 comments on commit eeadc30

Please sign in to comment.