Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 0e0e5dd07af632d11e11541a30c8dca33a20789c 1 parent bd23a07
Ingo Schommer authored December 04, 2011

Showing 1 changed file with 19 additions and 4 deletions. Show diff stats Hide diff stats

  1. 23  i18n/i18n.php
23  i18n/i18n.php
@@ -1460,16 +1460,19 @@ public static function get_time_format() {
1460 1460
 	 * @return string The translated string, according to the currently set locale {@link i18n::set_locale()}
1461 1461
 	 */
1462 1462
 	static function _t($entity, $string = "", $priority = 40, $context = "") {
  1463
+		// get current locale (either default or user preference)
  1464
+		$locale = i18n::get_locale();
  1465
+		$lang = i18n::get_lang_from_locale($locale);
  1466
+		
1463 1467
 		foreach(self::get_translators() as $priority => $translators) {
1464 1468
 			foreach($translators as $name => $translator) {
1465 1469
 				$adapter = $translator->getAdapter();
1466  
-
1467  
-				// get current locale (either default or user preference)
1468  
-				$locale = i18n::get_locale();
1469 1470
 				$adapter->setLocale($locale);
1470 1471
 
1471 1472
 				// if language table isn't loaded for this locale, get it for each of the modules
1472  
-				if(!$adapter->isAvailable($locale)) i18n::include_by_locale($locale);
  1473
+				if(!$adapter->isAvailable($locale) && !$adapter->isAvailable($lang)) {
  1474
+					i18n::include_by_locale($locale);
  1475
+				}
1473 1476
 
1474 1477
 				$translation = $adapter->translate($entity, $locale);
1475 1478
 
@@ -1927,6 +1930,18 @@ static function include_by_locale($locale) {
1927 1930
 						}
1928 1931
 					}
1929 1932
 				}
  1933
+				
  1934
+				// Add empty translations to ensure the locales are "registered" with isAvailable(),
  1935
+				// and the next invocation of include_by_locale() doesn't cause a new reparse.
  1936
+				foreach($selectedLocales as $selectedLocale) {
  1937
+					$adapter->addTranslation(
  1938
+						array(
  1939
+							'content' => array('_' => '_'), 
  1940
+							'locale' => $selectedLocale, 
  1941
+							'usetranslateadapter' => true
  1942
+						)
  1943
+					);
  1944
+				}
1930 1945
 			}
1931 1946
 		}
1932 1947
 	}

0 notes on commit 0e0e5dd

Please sign in to comment.
Something went wrong with that request. Please try again.