Provides interface translation services.
This function is called from t() to translate a string if needed.
$string: A string to look up translation for. If omitted, all the cached strings will be returned in all languages already used on the page.
$context: The context of this string.
$langcode: Language code to use for the lookup.
function locale($string = NULL, $context = NULL, $langcode = NULL) {
$language_interface = language(LANGUAGE_TYPE_INTERFACE);
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['locale'] =& drupal_static(__FUNCTION__, array(
'cache' => array(),
'exists' => NULL,
));
}
$locale_t =& $drupal_static_fast['locale']['cache'];
$locale_exists =& $drupal_static_fast['locale']['exists'];
// Check whether Locale module is actually installed and operational.
// The mere existence of locale() does not imply that Locale module is
// actually enabled and its database tables are installed. Since PHP code
// cannot be unloaded, this is typically the case in the environment that
// is executing a test.
if (!isset($locale_exists)) {
$locale_exists = function_exists('module_exists') && module_exists('locale');
}
if (!$locale_exists) {
return $string;
}
if (!isset($string)) {
// Return all cached strings if no string was specified
return $locale_t;
}
$langcode = isset($langcode) ? $langcode : $language_interface->langcode;
// Strings are cached by langcode, context and roles, using instances of the
// LocaleLookup class to handle string lookup and caching.
if (!isset($locale_t[$langcode][$context]) && isset($language_interface)) {
$locale_t[$langcode][$context] = new LocaleLookup($langcode, $context, locale_storage());
}
return $locale_t[$langcode][$context][$string] === TRUE ? $string : $locale_t[$langcode][$context][$string];
}