Skip to content

Commit

Permalink
Applying patch from '0x20h' to fix issue where domain translation fil…
Browse files Browse the repository at this point in the history
…es would be mixed together causing duplicate data in the various cache files. There should now be one cache per domain + language. Fixes #1085
  • Loading branch information
markstory committed Sep 14, 2010
1 parent 85ccd1f commit d7bb769
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
35 changes: 26 additions & 9 deletions cake/libs/i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,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.
Expand Down Expand Up @@ -128,7 +128,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);
}
Expand All @@ -155,13 +155,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 ($_this->category == 'LC_TIME') {
Expand Down Expand Up @@ -568,8 +574,19 @@ function __translateTime($format, $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();
}
}
52 changes: 52 additions & 0 deletions cake/tests/cases/libs/i18n.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,58 @@ function tearDown() {
App::objects('plugin', null, false);
}


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
*
Expand Down

0 comments on commit d7bb769

Please sign in to comment.