summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/common.inc21
-rw-r--r--modules/locale/locale.module32
2 files changed, 30 insertions, 23 deletions
diff --git a/includes/common.inc b/includes/common.inc
index ec2f06432..feb28de30 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -604,7 +604,7 @@ function fix_gpc_magic() {
}
/**
- * Translate strings to the current locale.
+ * Translate strings to the page language or a given language.
*
* All human-readable text that will be displayed somewhere within a page should be
* run through the t() function.
@@ -695,27 +695,32 @@ function fix_gpc_magic() {
* - @variable: escape plain text to HTML (check_plain)
* - %variable: escape text and theme as a placeholder for user-submitted
* content (check_plain + theme_placeholder)
+ * @param $langcode
+ * Optional language code to translate to a language other than
+ * what is used to display the page.
* @return
* The translated string.
*/
-function t($string, $args = 0) {
+function t($string, $args = 0, $langcode = NULL) {
global $language;
static $custom_strings;
+ $langcode = isset($langcode) ? $langcode : $language->language;
+
// First, check for an array of customized strings. If present, use the array
// *instead of* database lookups. This is a high performance way to provide a
// handful of string replacements. See settings.php for examples.
// Cache the $custom_strings variable to improve performance.
- if (!isset($custom_strings)) {
- $custom_strings = variable_get('locale_custom_strings_'. $language->language, array());
+ if (!isset($custom_strings[$langcode])) {
+ $custom_strings[$langcode] = variable_get('locale_custom_strings_'. $langcode, array());
}
// Custom strings work for English too, even if locale module is disabled.
- if (isset($custom_strings[$string])) {
- $string = $custom_strings[$string];
+ if (isset($custom_strings[$langcode][$string])) {
+ $string = $custom_strings[$langcode][$string];
}
// Translate with locale module if enabled.
- elseif (function_exists('locale') && $language->language != 'en') {
- $string = locale($string);
+ elseif (function_exists('locale') && $langcode != 'en') {
+ $string = locale($string, $langcode);
}
if (!$args) {
return $string;
diff --git a/modules/locale/locale.module b/modules/locale/locale.module
index 8a8f03f3b..2bd65463e 100644
--- a/modules/locale/locale.module
+++ b/modules/locale/locale.module
@@ -284,35 +284,37 @@ function locale_theme() {
*
* This function is called from t() to translate a string if needed.
*/
-function locale($string) {
+function locale($string, $langcode = NULL) {
global $language;
static $locale_t;
+ $langcode = isset($langcode) ? $langcode : $language->language;
+
// Store database cached translations in a static var.
- if (!isset($locale_t)) {
- $locale_t = array();
- if (!($cache = cache_get('locale:'. $language->language, 'cache'))) {
+ if (!isset($locale_t[$langcode])) {
+ $locale_t[$langcode] = array();
+ if (!($cache = cache_get('locale:'. $langcode, 'cache'))) {
locale_refresh_cache();
- $cache = cache_get('locale:'. $language->language, 'cache');
+ $cache = cache_get('locale:'. $langcode, 'cache');
}
if ($cache) {
- $locale_t = $cache->data;
+ $locale_t[$langcode] = $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]);
+ if (isset($locale_t[$langcode][$string])) {
+ $string = ($locale_t[$langcode][$string] === TRUE ? $string : $locale_t[$langcode][$string]);
}
// We do not 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.language = '%s' AND s.textgroup = 'default'", $string, $language->language);
+ $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.language = '%s' AND s.textgroup = 'default'", $string, $langcode);
// Translation found
if ($trans = db_fetch_object($result)) {
if (!empty($trans->translation)) {
- $locale_t[$string] = $trans->translation;
+ $locale_t[$langcode][$string] = $trans->translation;
$string = $trans->translation;
}
}
@@ -322,20 +324,20 @@ function locale($string) {
$result = db_query("SELECT lid, source FROM {locales_source} WHERE source = '%s' AND textgroup = 'default'", $string);
// We have no such translation
if ($obj = db_fetch_object($result)) {
- if ($language) {
- db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '')", $obj->lid, $language->language);
+ if ($langcode) {
+ db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '')", $obj->lid, $langcode);
}
}
// We have no such source string
else {
db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', 'default')", request_uri(), $string);
- if ($language) {
+ if ($langcode) {
$lid = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = 'default'", $string));
- db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '')", $lid->lid, $language->language);
+ db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '')", $lid->lid, $langcode);
}
}
// Clear locale cache in DB
- cache_clear_all('locale:'. $language->language, 'cache');
+ cache_clear_all('locale:'. $langcode, 'cache');
}
}