removed destructor, caching is done on file loading (fixes and improves #1085)

Signed-off-by: mark_story <mark@mark-story.com>
This commit is contained in:
0x20h 2010-09-21 20:45:57 +02:00 committed by mark_story
parent ba8280423d
commit 2eac24c31a
2 changed files with 13 additions and 46 deletions

View file

@ -80,14 +80,6 @@ class I18n extends Object {
*/
var $__noLocale = false;
/**
* Determine what should be cached
*
* @var boolean
* @access private
*/
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
@ -153,15 +145,16 @@ class I18n extends Object {
if (is_null($domain)) {
$domain = 'default';
}
$_this->domain = $domain . '_' . $_this->l10n->locale;
if (!isset($_this->__domains[$domain][$_this->__lang])) {
$_this->domain = $domain . '_' . $_this->l10n->lang;
if (empty($_this->__domains[$domain][$_this->__lang])) {
$_this->__domains[$domain][$_this->__lang] = Cache::read($_this->domain, '_cake_core_');
}
if (empty($_this->__domains[$domain][$_this->__lang][$_this->category])) {
$_this->__bindTextDomain($domain);
$_this->__cache[] = array('key' => $_this->domain, 'lang' => $_this->__lang, 'domain' => $domain);
Cache::write($_this->domain, $_this->__domains[$domain][$_this->__lang], '_cake_core_');
}
if ($_this->category == 'LC_TIME') {
@ -560,19 +553,4 @@ class I18n extends Object {
}
return $format;
}
/**
* Object destructor
*
* Write cache file if changes have been made to the $__map or $__paths
* @access private
*/
function __destruct() {
if (!empty($this->__cache)) {
foreach($this->__cache as $entry) {
if (empty($this->__domains[$entry['domain']][$entry['lang']])) continue;
Cache::write($entry['key'], array_filter($this->__domains[$entry['domain']][$entry['lang']]), '_cake_core_');
}
}
}
}

View file

@ -58,26 +58,21 @@ class I18nTest extends CakeTestCase {
function testTranslationCaching() {
Configure::write('Config.language', 'cache_test_po');
$i18n =& i18n::getInstance();
// reset cache & i18n
$i18n->__destruct();
// reset internally stored entries
$i18n->__cache = array();
$i18n->__domains = array();
Cache::clear(false, '_cake_core_');
$lang = $i18n->l10n->locale;
$lang = Configure::read('Config.language');#$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['dom1']['cache_test_po']['LC_MESSAGES']['dom1.foo'], 'Dom 1 Foo');
// destruct -> writes to cache
$i18n->__destruct();
// reset internally stored entries
$i18n->__domains = array();
$i18n->__cache = array();
@ -92,22 +87,16 @@ class I18nTest extends CakeTestCase {
// translate a item of dom2 (adds dom2 to cache)
$this->assertEqual(i18n::translate('dom2.foo', false, 'dom2'), 'Dom 2 Foo');
// modify cache entry manually to verify that dom1 entry is now read from cache
// verify dom2 was cached through manual read from cache
$cachedDom2 = Cache::read('dom2_' . $lang, '_cake_core_');
$this->assertEqual($cachedDom2['LC_MESSAGES']['dom2.foo'], 'Dom 2 Foo');
$this->assertEqual($cachedDom2['LC_MESSAGES']['dom2.bar'], 'Dom 2 Bar');
// modify cache entry manually to verify that dom1 entries now will be read from cache
$cachedDom1['LC_MESSAGES']['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['LC_MESSAGES']['dom2.foo'], 'Dom 2 Foo');
$this->assertEqual($cachedDom2['LC_MESSAGES']['dom2.bar'], 'Dom 2 Bar');
}