Skip to content

Commit

Permalink
Allow apps to overwrite translation domains from third party plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Dec 23, 2014
1 parent c540e2b commit 5c5fb59
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/Config/bootstrap.php
Expand Up @@ -70,6 +70,12 @@
*
*/

/**
* To prefer app translation over plugin translation, you can set
*
* Configure::write('I18n.preferApp', true);
*/

/**
* You can attach event listeners to the request lifecycle as Dispatcher Filter. By default CakePHP bundles two filters:
*
Expand Down
4 changes: 3 additions & 1 deletion lib/Cake/I18n/I18n.php
Expand Up @@ -387,7 +387,9 @@ protected function _bindTextDomain($domain) {
$pluginDomain = Inflector::underscore($plugin);
if ($pluginDomain === $domain) {
$searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
$searchPaths = array_reverse($searchPaths);
if (!Configure::read('I18n.preferApp')) {
$searchPaths = array_reverse($searchPaths);
}
break;
}
}
Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/Test/Case/I18n/I18nTest.php
Expand Up @@ -1758,6 +1758,30 @@ public function testPluginTranslation() {
$this->assertTrue(in_array('25 = 0 or > 1 (from plugin)', $plurals));
}

/**
* Test that Configure::read('I18n.preferApp') will prefer app.
*
* @return void
*/
public function testPluginTranslationPreferApp() {
// Reset internally stored entries
I18n::clear();
Cache::clear(false, '_cake_core_');

Configure::write('I18n.preferApp', true);

App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));

Configure::write('Config.language', 'po');
$singular = $this->_domainSingular();
$this->assertEquals('Plural Rule 1', $singular);

$plurals = $this->_domainPlural();
$this->assertTrue(in_array('0 = 0 or > 1', $plurals));
}

/**
* testPoMultipleLineTranslation method
*
Expand Down
21 changes: 21 additions & 0 deletions lib/Cake/Test/test_app/Locale/po/LC_MESSAGES/test_plugin.po
@@ -0,0 +1,21 @@
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"

msgid "%d = 1"
msgid_plural "%d = 0 or > 1"
msgstr[0] "%d = 1"
msgstr[1] "%d = 0 or > 1"

0 comments on commit 5c5fb59

Please sign in to comment.