You are here

function locale in Drupal 6

Same name and namespace in other branches
  1. 4 modules/locale.module \locale()
  2. 5 modules/locale/locale.module \locale()
  3. 7 modules/locale/locale.module \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.

$langcode: Language code to use for the lookup.

$reset: Set to TRUE to reset the in-memory cache.

1 call to locale()
t in includes/common.inc
Translate strings to the page language or a given language.
20 string references to 'locale'
drupal_verify_profile in includes/install.inc
Verify a profile for installation.
install_select_locale_form in ./install.php
Form API array definition for language selection.
language_list in includes/bootstrap.inc
Get a list of languages set up indexed by the specified key
locale_add_language in includes/locale.inc
API function to add a language.
locale_help in modules/locale/locale.module
Implementation of hook_help().

... See full list

File

modules/locale/locale.module, line 329
Add language handling functionality and enables the translation of the user interface to languages other than English.

Code

function locale($string = NULL, $langcode = NULL, $reset = FALSE) {
  global $language;
  static $locale_t;
  if ($reset) {

    // Reset in-memory cache.
    $locale_t = NULL;
  }
  if (!isset($string)) {

    // Return all cached strings if no string was specified
    return $locale_t;
  }
  $langcode = isset($langcode) ? $langcode : $language->language;

  // Store database cached translations in a static var.
  if (!isset($locale_t[$langcode])) {
    $locale_t[$langcode] = array();

    // 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
    // interface for this. Be careful when turning this option off!
    if (variable_get('locale_cache_strings', 1) == 1) {
      if ($cache = cache_get('locale:' . $langcode, 'cache')) {
        $locale_t[$langcode] = $cache->data;
      }
      elseif (lock_acquire('locale_cache_' . $langcode)) {

        // Refresh database stored cache of translations for given language.
        // We only store short strings used in current version, to improve
        // performance and consume less memory.
        $result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = 'default' AND s.version = '%s' AND LENGTH(s.source) < 75", $langcode, VERSION);
        while ($data = db_fetch_object($result)) {
          $locale_t[$langcode][$data->source] = empty($data->translation) ? TRUE : $data->translation;
        }
        cache_set('locale:' . $langcode, $locale_t[$langcode]);
        lock_release('locale_cache_' . $langcode);
      }
    }
  }

  // If we have the translation cached, skip checking the database
  if (!isset($locale_t[$langcode][$string])) {

    // We do not have this translation cached, so get it from the DB.
    $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'", $langcode, $string));
    if ($translation) {

      // We have the source string at least.
      // Cache translation string or TRUE if no translation exists.
      $locale_t[$langcode][$string] = empty($translation->translation) ? TRUE : $translation->translation;
      if ($translation->version != VERSION) {

        // This is the first use of this string under current Drupal version. Save version
        // and clear cache, to include the string into caching next time. Saved version is
        // also a string-history information for later pruning of the tables.
        db_query("UPDATE {locales_source} SET version = '%s' WHERE lid = %d", VERSION, $translation->lid);
        cache_clear_all('locale:', 'cache', TRUE);
      }
    }
    else {

      // We don't have the source string, cache this as untranslated.
      db_query("INSERT INTO {locales_source} (location, source, textgroup, version) VALUES ('%s', '%s', 'default', '%s')", request_uri(), $string, VERSION);
      $locale_t[$langcode][$string] = TRUE;

      // Clear locale cache so this string can be added in a later request.
      cache_clear_all('locale:', 'cache', TRUE);
    }
  }
  return $locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string];
}