Skip to content

Commit

Permalink
Implemented ability to set a custom formatter to translation packages
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 24, 2014
1 parent 59be3d1 commit 6329a5e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/I18n/I18n.php
Expand Up @@ -20,8 +20,8 @@
use Aura\Intl\PackageLocator;
use Aura\Intl\TranslatorFactory;
use Aura\Intl\TranslatorLocator;
use Cake\I18n\Formatter\SprintfFormatter;
use Cake\I18n\Formatter\IcuFormatter;
use Cake\I18n\Formatter\SprintfFormatter;

/**
* I18n handles translation of Text and time format strings.
Expand All @@ -32,26 +32,26 @@ class I18n {

protected static $_defaultLocale = 'en_US';

protected static $_defaultFormatter = 'basic';

public static function translators() {
if (static::$_collection !== null) {
return static::$_collection;
}

$translators = new TranslatorLocator(
return static::$_collection = new TranslatorLocator(
new PackageLocator,
new FormatterLocator([
'basic' => function() {
'sprintf' => function() {
return new SprintfFormatter;
},
'icu' => function() {
'basic' => function() {
return new IcuFormatter;
},
]),
new TranslatorFactory,
static::$_defaultLocale
);

return static::$_collection = $translators;
}

public static function translator($package = 'default', $locale = null, callable $loader = null) {
Expand Down Expand Up @@ -82,11 +82,33 @@ public static function translator($package = 'default', $locale = null, callable
return $translator;
}

public static function defaultFormatter($name = null) {
if ($name === null) {
return static::$_defaultFormatter;
}

static::$_defaultFormatter = $name;
}

public static function clear() {
static::$_collection = null;
}

protected static function _fallbackTranslator($package, $locale) {
$chain = new ChainMessagesLoader([
new MessagesFileLoader($package, $locale, 'mo'),
new MessagesFileLoader($package, $locale, 'po')
]);

if (static::$_defaultFormatter !== 'basic') {
$formatter = static::$_defaultFormatter;
$chain = function() use ($formatter, $chain) {
$package = $chain();
$package->setFormatter($formatter);
return $package;
};
}

static::translator($package, $locale, $chain);
return static::translators()->get($package);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -25,6 +25,17 @@
*/
class I18nTest extends TestCase {

/**
* Tear down method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
I18n::clear();
I18n::defaultFormatter('basic');
}

/**
* Tests that a default translator is created and messages are parsed
* correclty
Expand All @@ -49,10 +60,12 @@ public function testTranslatorLoadMoFile() {

/**
* Tests that plural rules are correctly used for the English language
* using the sprintf formatter
*
* @return void
*/
public function testPluralSelection() {
I18n::defaultFormatter('sprintf');
$translator = I18n::translator(); // en_US
$result = $translator->translate('%d = 0 or > 1', ['_count' => 1]);
$this->assertEquals('1 is 1 (po translated)', $result);
Expand All @@ -61,4 +74,19 @@ public function testPluralSelection() {
$this->assertEquals('2 is 2-4 (po translated)', $result);
}

/**
* Tests that plural rules are correctly used for the English language
* using the basic formatter
*
* @return void
*/
public function testPluralSelectionBasicFormatter() {
$translator = I18n::translator('special');
$result = $translator->translate('There are {_count} things', ['_count' => 2]);
$this->assertEquals('There are 2 things', $result);

$result = $translator->translate('There are {_count} things', ['_count' => 1]);
$this->assertEquals('There is only one', $result);
}

}
13 changes: 13 additions & 0 deletions tests/test_app/TestApp/Locale/en/LC_MESSAGES/special.po
@@ -0,0 +1,13 @@
msgid ""
msgstr ""
"Project-Id-Version: CakePHP Testsuite\n"
"PO-Revision-Date: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-SourceCharset: utf-8\n"

msgid "There is only one"
msgid_plural "There are {_count} things"
msgstr[0] "There is only one"
msgstr[1] "There are {_count} things"

0 comments on commit 6329a5e

Please sign in to comment.