Same name and namespace in other branches
  1. 4.6.x modules/locale.module \locale()
  2. 5.x modules/locale/locale.module \locale()
  3. 6.x modules/locale/locale.module \locale()
  4. 7.x modules/locale/locale.module \locale()

Provides interface translation services.

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

1 call to locale()
t in includes/common.inc
Translate strings to the current locale.
5 string references to 'locale'
locale_admin_manage_delete_form_submit in modules/locale.module
Process language deletion submissions.
_locale_add_language in includes/locale.inc
Helper function to add a language
_locale_admin_import_submit in includes/locale.inc
Process the locale import form submission.
_locale_export_po in includes/locale.inc
Exports a Portable Object (Template) file for a language
_locale_import_po in includes/locale.inc
Parses Gettext Portable Object file information and inserts into database

File

modules/locale.module, line 171
Enables administrators to manage the site interface languages.

Code

function locale($string) {
  global $locale;
  static $locale_t;

  // Store database cached translations in a static var.
  if (!isset($locale_t)) {
    $cache = cache_get("locale:{$locale}");
    if ($cache == 0) {
      locale_refresh_cache();
      $cache = cache_get("locale:{$locale}");
    }
    $locale_t = unserialize($cache->data);
  }

  // We have the translation cached (if it is TRUE, then there is no
  // translation, so there is no point in checking the database)
  if (isset($locale_t[$string])) {
    $string = $locale_t[$string] === TRUE ? $string : $locale_t[$string];
  }
  else {
    $result = db_query("SELECT s.lid, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.source = '%s' AND t.locale = '%s'", $string, $locale);

    // Translation found
    if ($trans = db_fetch_object($result)) {
      if (!empty($trans->translation)) {
        $locale_t[$string] = $trans->translation;
        $string = $trans->translation;
      }
    }
    else {
      $result = db_query("SELECT lid, source FROM {locales_source} WHERE source = '%s'", $string);

      // We have no such translation
      if ($obj = db_fetch_object($result)) {
        if ($locale) {
          db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '')", $obj->lid, $locale);
        }
      }
      else {
        db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", request_uri(), $string);
        if ($locale) {
          $lid = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $string));
          db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '')", $lid->lid, $locale);
        }
      }

      // Clear locale cache in DB
      cache_clear_all("locale:{$locale}");
    }
  }
  return $string;
}