Skip to content

Commit 1545f99

Browse files
committed
Added a chain messages loader to automatically load .mo files if available
1 parent 77d8c11 commit 1545f99

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/I18n/ChainMessagesLoader.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 3.0.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\I18n;
16+
17+
use Aura\Intl\Package;
18+
19+
/**
20+
*
21+
*
22+
*/
23+
class ChainMessagesLoader {
24+
25+
protected $_loaders = [];
26+
27+
public function __construct(array $loaders) {
28+
$this->_loaders = $loaders;
29+
}
30+
31+
public function __invoke() {
32+
foreach ($this->_loaders as $k => $loader) {
33+
if (!is_callable($loader)) {
34+
throw new \RuntimeException(
35+
sprintf('Loader "%s" in the chain is not a valid callable'),
36+
$k
37+
);
38+
}
39+
40+
$package = $loader();
41+
42+
if (!$package) {
43+
continue;
44+
}
45+
46+
if (!($package instanceof Package)) {
47+
throw new \RuntimeException(
48+
sprintf('Loader "%s" in the chain did not return a valid Package object'),
49+
$k
50+
);
51+
}
52+
53+
if (count($package->getMessages())) {
54+
return $package;
55+
}
56+
}
57+
58+
return new Package;
59+
}
60+
61+
}

src/I18n/I18n.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ public static function translator($package = 'default', $locale = null, callable
6464
try {
6565
return static::translators()->get($package);
6666
} catch (LoadException $e) {
67-
static::translator($package, $locale, new MessagesFileLoader($package, $locale));
68-
return static::translators()->get($package);
67+
return static::_fallbackTranslator($package, $locale);
6968
}
7069
}
7170

71+
protected static function _fallbackTranslator($package, $locale) {
72+
$chain = new ChainMessagesLoader([
73+
new MessagesFileLoader($package, $locale, 'mo'),
74+
new MessagesFileLoader($package, $locale, 'po')
75+
]);
76+
static::translator($package, $locale, $chain);
77+
return static::translators()->get($package);
78+
}
79+
7280
/**
7381
* Used by the translation functions in basics.php
7482
* Returns a translated string based on current language and translation files stored in locale folder

tests/TestCase/I18n/I18nTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ public function testDefaultTranslator() {
3737
$this->assertEquals('%d is 1 (po translated)', $translator->translate('%d = 1'));
3838
}
3939

40+
/**
41+
* Tests that the translator can automatically load messages from a .mo file
42+
*
43+
* @return void
44+
*/
45+
public function testTranslatorLoadMoFile() {
46+
$translator = I18n::translator('default', 'es_ES');
47+
$this->assertEquals('Plural Rule 6 (translated)', $translator->translate('Plural Rule 1'));
48+
}
4049
}

0 commit comments

Comments
 (0)