BUGFIX i18n::include_by_locale() doesn't include a fallback language, _t() instead will include the language if it's not available.

This fixes en_GB translations not being picked up. i18n::get_translators() will always have a fallback of "en" language without the
region, and "en_US" as the locale.
This commit is contained in:
Sean Harvey 2012-05-23 11:43:44 +12:00
parent 29e04a1049
commit 77dbceee35

View File

@ -1494,13 +1494,20 @@ class i18n extends Object implements TemplateGlobalProvider {
foreach($translators as $name => $translator) { foreach($translators as $name => $translator) {
$adapter = $translator->getAdapter(); $adapter = $translator->getAdapter();
// If language table isn't loaded for this locale, get it for each of the modules. // at this point, we need to ensure the language and locale are loaded
// The method will automatically load fallback languages (the lang for a locale). // as include_by_locale() doesn't load a fallback.
if(!$adapter->isAvailable($locale) && !$adapter->isAvailable($lang)) {
// TODO Remove reliance on global state, by refactoring into an i18nTranslatorManager // TODO Remove reliance on global state, by refactoring into an i18nTranslatorManager
// which is instanciated by core with a $clean instance variable. // which is instanciated by core with a $clean instance variable.
if(!$adapter->isAvailable($lang)) {
i18n::include_by_locale($lang, (isset($_GET['flush'])));
}
if(!$adapter->isAvailable($locale)) {
i18n::include_by_locale($locale, (isset($_GET['flush']))); i18n::include_by_locale($locale, (isset($_GET['flush'])));
} }
$translation = $adapter->translate($entity, $locale); $translation = $adapter->translate($entity, $locale);
// Return translation only if we found a match thats not the entity itself (Zend fallback) // Return translation only if we found a match thats not the entity itself (Zend fallback)
@ -1568,7 +1575,8 @@ class i18n extends Object implements TemplateGlobalProvider {
'disableNotices' => true, 'disableNotices' => true,
)) ))
); );
i18n::include_by_locale('en', isset($_GET['flush']));
i18n::include_by_locale('en_US', isset($_GET['flush'])); i18n::include_by_locale('en_US', isset($_GET['flush']));
} }
@ -1919,24 +1927,16 @@ class i18n extends Object implements TemplateGlobalProvider {
/** /**
* Includes all available language files for a certain defined locale. * Includes all available language files for a certain defined locale.
* If the locale is a fully qualified locale (e.g. "en_US" rather than "en"), *
* will load the base locale file as well (if available).
*
* @param string $locale All resources from any module in locale $locale will be loaded * @param string $locale All resources from any module in locale $locale will be loaded
* @param Boolean $clean Clean old caches? * @param Boolean $clean Clean old caches?
*/ */
static function include_by_locale($locale, $clean = false) { static function include_by_locale($locale, $clean = false) {
$lang = i18n::get_lang_from_locale($locale);
if($clean) { if($clean) {
$cache = Zend_Translate::getCache(); $cache = Zend_Translate::getCache();
if($cache) $cache->clean(Zend_Cache::CLEANING_MODE_ALL); if($cache) $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
} }
// Automatically include fallback language (if applicable)
// TODO Also include custom Zend_Translate routing languages
$selectedLocales = array_unique(array($lang, $locale));
// Sort modules by inclusion priority, then alphabetically // Sort modules by inclusion priority, then alphabetically
// TODO Should be handled by priority flags within modules // TODO Should be handled by priority flags within modules
$prios = array('sapphire' => 10, 'framework' => 10, 'admin' => 11, 'cms' => 12, 'mysite' => 90); $prios = array('sapphire' => 10, 'framework' => 10, 'admin' => 11, 'cms' => 12, 'mysite' => 90);
@ -1959,15 +1959,13 @@ class i18n extends Object implements TemplateGlobalProvider {
// Load translations from modules // Load translations from modules
foreach($modules as $module) { foreach($modules as $module) {
foreach($selectedLocales as $selectedLocale) { $filename = $adapter->getFilenameForLocale($locale);
$filename = $adapter->getFilenameForLocale($selectedLocale); $filepath = "{$module}/lang/" . $filename;
$filepath = "{$module}/lang/" . $filename;
if($filename && !file_exists($filepath)) continue; if($filename && !file_exists($filepath)) continue;
$adapter->addTranslation( $adapter->addTranslation(
array('content' => $filepath, 'locale' => $selectedLocale) array('content' => $filepath, 'locale' => $locale)
); );
}
} }
// Load translations from themes // Load translations from themes
@ -1979,29 +1977,25 @@ class i18n extends Object implements TemplateGlobalProvider {
strpos($theme, SSViewer::current_theme()) === 0 strpos($theme, SSViewer::current_theme()) === 0
&& file_exists("{$themesBase}/{$theme}/lang/") && file_exists("{$themesBase}/{$theme}/lang/")
) { ) {
foreach($selectedLocales as $selectedLocale) { $filename = $adapter->getFilenameForLocale($locale);
$filename = $adapter->getFilenameForLocale($selectedLocale); $filepath = "{$themesBase}/{$theme}/lang/" . $filename;
$filepath = "{$themesBase}/{$theme}/lang/" . $filename; if($filename && !file_exists($filepath)) continue;
if($filename && !file_exists($filepath)) continue; $adapter->addTranslation(
$adapter->addTranslation( array('content' => $filepath, 'locale' => $locale)
array('content' => $filepath, 'locale' => $selectedLocale) );
);
}
} }
} }
} }
// Add empty translations to ensure the locales are "registered" with isAvailable(), // 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. // and the next invocation of include_by_locale() doesn't cause a new reparse.
foreach($selectedLocales as $selectedLocale) { $adapter->addTranslation(
$adapter->addTranslation( array(
array( 'content' => array('_' => '_'),
'content' => array('_' => '_'), 'locale' => $locale,
'locale' => $selectedLocale, 'usetranslateadapter' => true
'usetranslateadapter' => true )
) );
);
}
} }
} }
} }