Skip to content

Commit

Permalink
Correctly implementing plugin translation files auto loading
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 24, 2014
1 parent 6329a5e commit 77ff1ba
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/I18n/MessagesFileLoader.php
Expand Up @@ -70,11 +70,11 @@ public function translationsFolder() {
$basePath = APP . 'Locale' . DS;

if (Plugin::loaded($pluginName)) {
$basePath = Plugin::path($pluginName) . 'Locale' . DS;
$basePath = Plugin::path($pluginName) . 'src' . DS . 'Locale' . DS;
}

foreach ($folders as $folder) {
$path = $basePath . $folder . DS . 'LC_MESSAGES' . DS;
$path = $basePath . $folder . DS . 'LC_MESSAGES' . DS;
if (is_dir($path)) {
return $path;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\Test\TestCase\I18n;

use Aura\Intl\Package;
use Cake\Core\Plugin;
use Cake\I18n\I18n;
use Cake\TestSuite\TestCase;

Expand All @@ -34,6 +36,7 @@ public function tearDown() {
parent::tearDown();
I18n::clear();
I18n::defaultFormatter('basic');
Plugin::unload();
}

/**
Expand Down Expand Up @@ -89,4 +92,37 @@ public function testPluralSelectionBasicFormatter() {
$this->assertEquals('There is only one', $result);
}

/**
* Tests that custom translation packages can be created on the fly and used later on
*
* @return void
*/
public function testCreateCustomTranslationPackage() {
I18n::translator('custom', 'fr_FR', function() {
$package = new Package();
$package->setMessages([
'Cow' => 'Le moo'
]);
return $package;
});

$translator = I18n::translator('custom', 'fr_FR');
$this->assertEquals('Le moo', $translator->translate('Cow'));
}

/**
* Tests that messages can also be loaded from plugins by using the
* domain = plugin_name convention
*
* @return void
*/
public function testPluginMesagesLoad() {
Plugin::load('TestPlugin');
$translator = I18n::translator('test_plugin');
$this->assertEquals(
'Plural Rule 1 (from plugin)',
$translator->translate('Plural Rule 1')
);
}

}
@@ -0,0 +1,16 @@
msgid ""
msgstr ""
"Project-Id-Version: CakePHP Testsuite\n"
"POT-Creation-Date: 2008-05-15 02:51-0700\n"
"PO-Revision-Date: \n"
"Last-Translator: CakePHP I18N & I10N Team <i10n.cakephp@gmail.com>\n"
"Language-Team: CakePHP I18N & I10N Team <i10n.cakephp@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Poedit-Language: Two Forms of Plurals\n"
"X-Poedit-SourceCharset: utf-8\n"

msgid "Plural Rule 1"
msgstr "Plural Rule 1 (from plugin)"

This file was deleted.

0 comments on commit 77ff1ba

Please sign in to comment.