function locale

Provides interface translation services.

This function is called from t() to translate a string if needed.

Parameters

$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.

2 calls to locale()
LocaleExportTest::testExportTranslation in drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleExportTest.php
Test exportation of translations.
t in drupal/core/includes/bootstrap.inc
Translates a string to the current language or to a given language.
36 string references to 'locale'
AssetCollectionTest::testGetLastModifiedWithValues in drupal/core/vendor/kriswallsmith/assetic/tests/Assetic/Test/Asset/AssetCollectionTest.php
AssetWriterTest::getCombinationTests in drupal/core/vendor/kriswallsmith/assetic/tests/Assetic/Test/AssetWriterTest.php
AssetWriterTest::testAssetWithInputVars in drupal/core/vendor/kriswallsmith/assetic/tests/Assetic/Test/AssetWriterTest.php
AssetWriterTest::testWriteAssetWithVars in drupal/core/vendor/kriswallsmith/assetic/tests/Assetic/Test/AssetWriterTest.php
EntityAccessTest::testEntityTranslationAccess in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php
Ensures entity access for entity translations is properly working.

... See full list

File

drupal/core/modules/locale/locale.module, line 307
Enables the translation of the user interface to languages other than English.

Code

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];
}