You are here

function locale in Localization client 5

Provides interface translation services.

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

1 call to locale()
_l10n_client_page_strings in ./l10n_client.module
Get the strings to translate for this page. These will be:
3 string references to 'locale'
l10n_client_menu in ./l10n_client.module
Implementation of hook_menu().
locale_admin_manage_delete_form_submit in locale/locale.module
Process language deletion submissions.
_l10n_client_page_strings in ./l10n_client.module
Get the strings to translate for this page. These will be:

File

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

Code

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

  // Return all cached strings if no string was specified
  if (!isset($string)) {
    return $locale_t;
  }

  // Store database cached translations in a static var.
  if (!isset($locale_t)) {

    // 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) {
      $cache = cache_get("locale:{$locale}", 'cache');
      if (!$cache) {
        locale_refresh_cache();
        $cache = cache_get("locale:{$locale}", 'cache');
      }
      $locale_t = unserialize($cache->data);
    }
    else {
      $locale_t = array();
    }
  }

  // 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])) {
    $locale_t[$string] = TRUE;
    $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, store all data in cache
    if ($trans = db_fetch_object($result)) {
      $locale_t[$string] = $trans;
    }
    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);
        }
        $locale_t[$string] = $obj;
      }
      else {
        db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", request_uri(), $string);
        if ($locale) {
          $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $string));
          db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '')", $lid, $locale);
        }
        $locale_t[$string] = (object) array(
          'lid' => $lid,
        );
      }

      // Clear locale cache in DB
      cache_clear_all("locale:{$locale}", 'cache');
    }
  }

  // The static cache may contain strings or full objects with some more data
  if (is_object($locale_t[$string])) {
    return empty($locale_t[$string]->translation) ? $string : $locale_t[$string]->translation;
  }
  else {
    return $locale_t[$string] === TRUE ? $string : $locale_t[$string];
  }
}