From 0e0e5dd07af632d11e11541a30c8dca33a20789c Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 4 Dec 2011 18:36:45 +0100 Subject: [PATCH] BUGFIX Ensuring that every locale (and its lang part) are registered as languages in i18n::include_by_locale() to avoid expensive re-parsing on subsequent i18n::_t() calls to the same locale --- i18n/i18n.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/i18n/i18n.php b/i18n/i18n.php index 82775b7c5..59e2242e6 100644 --- a/i18n/i18n.php +++ b/i18n/i18n.php @@ -1460,16 +1460,19 @@ class i18n extends Object implements TemplateGlobalProvider { * @return string The translated string, according to the currently set locale {@link i18n::set_locale()} */ static function _t($entity, $string = "", $priority = 40, $context = "") { + // get current locale (either default or user preference) + $locale = i18n::get_locale(); + $lang = i18n::get_lang_from_locale($locale); + foreach(self::get_translators() as $priority => $translators) { foreach($translators as $name => $translator) { $adapter = $translator->getAdapter(); - - // get current locale (either default or user preference) - $locale = i18n::get_locale(); $adapter->setLocale($locale); // if language table isn't loaded for this locale, get it for each of the modules - if(!$adapter->isAvailable($locale)) i18n::include_by_locale($locale); + if(!$adapter->isAvailable($locale) && !$adapter->isAvailable($lang)) { + i18n::include_by_locale($locale); + } $translation = $adapter->translate($entity, $locale); @@ -1927,6 +1930,18 @@ class i18n extends Object implements TemplateGlobalProvider { } } } + + // Add empty translations to ensure the locales are "registered" with isAvailable(), + // and the next invocation of include_by_locale() doesn't cause a new reparse. + foreach($selectedLocales as $selectedLocale) { + $adapter->addTranslation( + array( + 'content' => array('_' => '_'), + 'locale' => $selectedLocale, + 'usetranslateadapter' => true + ) + ); + } } } }