locale
- Versions
- 4.6 – 5
locale($string)- 6
locale($string = NULL, $langcode = NULL, $reset = FALSE)- 7
locale($string = NULL, $context = NULL, $langcode = NULL, $reset = FALSE)
Provides interface translation services
This function is called from t() to translate a string if needed.
Code
modules/locale.module, line 122
<?php
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]);
}
// We don't have this translation cached, so get it from the DB
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;
}
}
// Either we have no such source string, or no 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) VALUES (%d, '%s')", $obj->lid, $locale);
}
}
// We have no such source string
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) VALUES (%d, '%s')", $lid->lid, $locale);
}
}
// Clear locale cache in DB
cache_clear_all("locale:$locale");
}
}
return $string;
}
?>Login or register to post comments 