Skip to content
Permalink
Browse files

Applying patch from '0x20h' to fix issue where domain translation fil…

…es would be mixed together causing duplicate data in the various cache files. There should now be one cache per domain + language. Fixes #1085

Conflicts:

	cake/libs/i18n.php
	cake/tests/cases/libs/i18n.test.php
  • Loading branch information...
markstory committed Sep 14, 2010
1 parent 1b48cd6 commit 47e6aa2deb01dcfe4b03bc38b083474beac46c33
Showing with 77 additions and 11 deletions.
  1. +27 −10 cake/libs/i18n.php
  2. +50 −1 cake/tests/cases/libs/i18n.test.php
@@ -80,12 +80,12 @@ class I18n extends Object {
*/
var $__noLocale = false;
/**
* Determine if $__domains cache should be wrote
* Determine what should be cached
*
* @var boolean
* @access private
*/
var $__cache = false;
var $__cache = array();
/**
* Set to true when I18N::__bindTextDomain() is called for the first time.
* If a translation file is found it is set to false again
@@ -124,7 +124,7 @@ function &getInstance() {
*/
function translate($singular, $plural = null, $domain = null, $category = 6, $count = null) {
$_this =& I18n::getInstance();
if (strpos($singular, "\r\n") !== false) {
$singular = str_replace("\r\n", "\n", $singular);
}
@@ -151,13 +151,19 @@ function translate($singular, $plural = null, $domain = null, $category = 6, $co
}
$_this->domain = $domain . '_' . $_this->l10n->locale;
if (empty($_this->__domains)) {
$_this->__domains = Cache::read($_this->domain, '_cake_core_');
if (!isset($_this->__domains[$_this->category][$_this->__lang][$domain])) {
$_this->__domains[$_this->category][$_this->__lang][$domain] = Cache::read($_this->domain, '_cake_core_');
}
if (!isset($_this->__domains[$_this->category][$_this->__lang][$domain])) {
if (empty($_this->__domains[$_this->category][$_this->__lang][$domain])) {
$_this->__bindTextDomain($domain);
$_this->__cache = true;
$_this->__cache[] = array(
'key' => $_this->domain,
'category' => $_this->category,
'lang' => $_this->__lang,
'domain' => $domain,
'locale' => $_this->l10n->locale
);
}
if (!isset($count)) {
@@ -445,9 +451,20 @@ function __loadPo($file, $domain) {
* @access private
*/
function __destruct() {
if ($this->__cache) {
Cache::write($this->domain, array_filter($this->__domains), '_cake_core_');
if (!empty($this->__cache)) {
foreach($this->__cache as $entry) {
if (empty($this->__domains[$entry['category']][$entry['lang']][$entry['domain']])) {
continue;
}
Cache::write(
$entry['key'],
array_filter($this->__domains[$entry['category']][$entry['lang']][$entry['domain']]),
'_cake_core_'
);
}
}
$this->__cache = array();
$this->__domains = array();
}
}
?>
?>
@@ -58,7 +58,56 @@ function tearDown() {
Configure::write('localePaths', $this->_localePaths);
Configure::write('pluginPaths', $this->_pluginPaths);
Configure::write('__objects', $this->_objects);
}
function testTranslationCaching() {
Configure::write('Config.language', 'cache_test_po');
$i18n =& i18n::getInstance();
// reset cache & i18n
$i18n->__destruct();
Cache::clear(false, '_cake_core_');
$lang = $i18n->l10n->locale;
Cache::config('_cake_core_', Cache::config('default'));
// make some calls to translate using different domains
$this->assertEqual(i18n::translate('dom1.foo', false, 'dom1'), 'Dom 1 Foo');
$this->assertEqual(i18n::translate('dom1.bar', false, 'dom1'), 'Dom 1 Bar');
$this->assertEqual($i18n->__cache[0]['key'], 'dom1_' . $lang);
$this->assertEqual($i18n->__cache[0]['domain'], 'dom1');
$this->assertEqual($i18n->__domains['LC_MESSAGES']['cache_test_po']['dom1']['dom1.foo'], 'Dom 1 Foo');
// destruct -> writes to cache
$i18n->__destruct();
// now only dom1 should be in cache
$cachedDom1 = Cache::read('dom1_' . $lang, '_cake_core_');
$this->assertEqual($cachedDom1['dom1.foo'], 'Dom 1 Foo');
$this->assertEqual($cachedDom1['dom1.bar'], 'Dom 1 Bar');
// dom2 not in cache
$this->assertFalse(Cache::read('dom2_' . $lang, '_cake_core_'));
// translate a item of dom2 (adds dom2 to cache)
$this->assertEqual(i18n::translate('dom2.foo', false, 'dom2'), 'Dom 2 Foo');
// modify cache entry to verify that dom1 entry is now read from cache
$cachedDom1['dom1.foo'] = 'FOO';
Cache::write('dom1_' . $lang, $cachedDom1, '_cake_core_');
$this->assertEqual(i18n::translate('dom1.foo', false, 'dom1'), 'FOO');
// verify that only dom2 will be cached now
$this->assertEqual($i18n->__cache[0]['key'], 'dom2_' . $lang);
$this->assertEqual(count($i18n->__cache), 1);
// write to cache
$i18n->__destruct();
// verify caching through manual read from cache
$cachedDom2 = Cache::read('dom2_' . $lang, '_cake_core_');
$this->assertEqual($cachedDom2['dom2.foo'], 'Dom 2 Foo');
$this->assertEqual($cachedDom2['dom2.bar'], 'Dom 2 Bar');
}
/**
* testDefaultStrings method
@@ -2653,4 +2702,4 @@ function __pluralFromCore() {
return $plurals;
}
}
?>
?>

0 comments on commit 47e6aa2

Please sign in to comment.
You can’t perform that action at this time.