diff --git a/modules/locale/locale.module b/modules/locale/locale.module index f3dac6c..8a58a06 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -646,46 +646,58 @@ function locale($string = NULL, $context = NULL, $langcode = NULL) { } $langcode = isset($langcode) ? $langcode : $language->language; - - // Strings are cached by langcode and user roles, using instances of the - // LocaleLookup class to handle string lookup and caching. + + // Strings are cached by langcode and user roles, using instances of the + // LocaleLookup class to handle string lookup and caching. if (!isset($locale_t[$langcode]) && isset($language)) { - $locale_t[$langcode] = new LocaleLookup($langcode); - } - - return ($locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string]); + $locale_t[$langcode] = new LocaleLookup($langcode,$context); + } + return ($locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string]); } - + /** * Extends CacheArray to allow for dynamic building of the locale cache. */ class LocaleLookup extends DrupalCacheArray { - - /** - * A language code. - * @var string - */ - protected $langcode; - /** - * Constructs a LocaleCache object. - */ - public function __construct($langcode) { - $this->langcode = $langcode; - - // Add the current user's role IDs to the cache key, this ensures that, for - // example, strings for admin menu items and settings forms are not cached - // for anonymous users. - $rids = implode(':', array_keys($GLOBALS['user']->roles)); - parent::__construct("locale:$langcode:$rids", 'cache'); - } - - /** - * Overrides DrupalCacheArray::resolveCacheMiss(). - */ - protected function resolveCacheMiss($offset) { - $translation = db_fetch_object(db_query("SELECT s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.source = '%s' AND s.textgroup = 'default'", $this->langcode, $offset)); - if ($translation) { + /** + * A language code. + * @var string + */ + protected $langcode; + + /** + * The msgctxt context. + * @var string + */ + protected $context; + + /** + * Constructs a LocaleCache object. + */ + public function __construct($langcode, $context) { + $this->langcode = $langcode; + $this->context = (string) $context; + + // Add the current user's role IDs to the cache key, this ensures that, for + // example, strings for admin menu items and settings forms are not cached + // for anonymous users. + $rids = implode(':', array_keys($GLOBALS['user']->roles)); + parent::__construct("locale:$langcode:$rids", 'cache'); + } + + /** + * Overrides DrupalCacheArray::resolveCacheMiss(). + */ + protected function resolveCacheMiss($offset) { + + $translation = db_query("SELECT s.lid, t.translation, s.version FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.source = :source AND s.context = :context", array( + + ':language' => $this->langcode, + ':source' => $offset, + ':context' => $this->context, + ))->fetchObject(); + if ($translation) { if ($translation->version != VERSION) { // This is the first use of this string under current Drupal version. // Update the {locales_source} table to indicate the string is current. @@ -694,7 +706,7 @@ class LocaleLookup extends DrupalCacheArray { ->condition('lid', $translation->lid) ->execute(); } - $value = !empty($translation->translation) ? $translation->translation : TRUE; + $value = !empty($translation->translation) ? $translation->translation : TRUE; } else { // We don't have the source string, update the {locales_source} table to @@ -705,15 +717,14 @@ class LocaleLookup extends DrupalCacheArray { 'version' => VERSION, )) ->key(array( - 'source' => $string, - 'context' => (string) $context, - 'textgroup' => 'default', + 'source' => $offset, + 'context' => $this->context, )) ->execute(); $value = TRUE; } - - $this->storage[$offset] = $value; + + $this->storage[$offset] = $value; // Disabling the usage of string caching allows a module to watch for // the exact list of strings used on a page. From a performance // perspective that is a really bad idea, so we have no user