diff options
author | Gábor Hojtsy <gabor@hojtsy.hu> | 2007-05-29 14:37:49 +0000 |
---|---|---|
committer | Gábor Hojtsy <gabor@hojtsy.hu> | 2007-05-29 14:37:49 +0000 |
commit | 36e87e190c637338d6fd7d72132d5e74913a674d (patch) | |
tree | 06248a35fe00ef5d9251782c8439fcc41b367fa4 | |
parent | af34a17925adaec3167d716d601701d80366d3ad (diff) | |
download | brdo-36e87e190c637338d6fd7d72132d5e74913a674d.tar.gz brdo-36e87e190c637338d6fd7d72132d5e74913a674d.tar.bz2 |
#147640 by Jose A Reyero with further cleanup by myself: add language code to localization functions
This makes it possible to call these functions with a given language code when sending emails to multiple users with different languages in a request for example.
-rw-r--r-- | includes/bootstrap.inc | 8 | ||||
-rw-r--r-- | includes/common.inc | 55 | ||||
-rw-r--r-- | includes/locale.inc | 9 | ||||
-rw-r--r-- | modules/aggregator/aggregator.module | 2 | ||||
-rw-r--r-- | modules/locale/locale.module | 28 |
5 files changed, 62 insertions, 40 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index a394a7c75..d0f9e5109 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -1053,9 +1053,13 @@ function language_list($field = 'language', $reset = FALSE) { /** * Default language used on the site + * + * @param $property + * Optional property of the language object to return */ -function language_default() { - return variable_get('language_default', (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0)); +function language_default($property = NULL) { + $language = variable_get('language_default', (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0)); + return $property ? $language->$property : $language; } diff --git a/includes/common.inc b/includes/common.inc index abcc94489..f8b7150a4 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -692,7 +692,7 @@ function fix_gpc_magic() { * @return * The translated string. */ -function t($string, $args = 0, $langcode = NULL) { +function t($string, $args = array(), $langcode = NULL) { global $language; static $custom_strings; @@ -713,7 +713,7 @@ function t($string, $args = 0, $langcode = NULL) { elseif (function_exists('locale') && $langcode != 'en') { $string = locale($string, $langcode); } - if (!$args) { + if (empty($args)) { return $string; } else { @@ -835,8 +835,9 @@ function check_url($uri) { * * Arbitrary elements may be added using the $args associative array. */ -function format_rss_channel($title, $link, $description, $items, $language = 'en', $args = array()) { - // arbitrary elements may be added using the $args associative array +function format_rss_channel($title, $link, $description, $items, $langcode = NULL, $args = array()) { + global $language; + $langcode = $langcode ? $langcode : $language->language; $output = "<channel>\n"; $output .= ' <title>'. check_plain($title) ."</title>\n"; @@ -846,7 +847,7 @@ function format_rss_channel($title, $link, $description, $items, $language = 'en // We strip all HTML tags, but need to prevent double encoding from properly // escaped source data (such as & becoming &amp;). $output .= ' <description>'. check_plain(decode_entities(strip_tags($description))) ."</description>\n"; - $output .= ' <language>'. check_plain($language) ."</language>\n"; + $output .= ' <language>'. check_plain($langcode) ."</language>\n"; $output .= format_xml_elements($args); $output .= $items; $output .= "</channel>\n"; @@ -948,30 +949,33 @@ function format_xml_elements($array) { * content (check_plain + theme_placeholder) * Note that you do not need to include @count in this array. * This replacement is done automatically for the plural case. + * @param $langcode + * Optional language code to translate to a language other than + * what is used to display the page. * @return * A translated string. */ -function format_plural($count, $singular, $plural, $args = array()) { +function format_plural($count, $singular, $plural, $args = array(), $langcode = NULL) { if ($count == 1) { return t($singular, $args); } $args['@count'] = $count; // get the plural index through the gettext formula - $index = (function_exists('locale_get_plural')) ? locale_get_plural($count) : -1; + $index = (function_exists('locale_get_plural')) ? locale_get_plural($count, $langcode) : -1; if ($index < 0) { // backward compatibility - return t($plural, $args); + return t($plural, $args, $langcode); } else { switch ($index) { case "0": - return t($singular, $args); + return t($singular, $args, $langcode); case "1": - return t($plural, $args); + return t($plural, $args, $langcode); default: unset($args['@count']); $args['@count['. $index .']'] = $count; - return t(strtr($plural, array('@count' => '@count['. $index .']')), $args); + return t(strtr($plural, array('@count' => '@count['. $index .']')), $args, $langcode); } } } @@ -1002,21 +1006,24 @@ function parse_size($size) { * * @param $size * The size in bytes. + * @param $langcode + * Optional language code to translate to a language other than + * what is used to display the page. * @return * A translated string representation of the size. */ -function format_size($size) { +function format_size($size, $langcode = NULL) { if ($size < 1024) { - return format_plural($size, '1 byte', '@count bytes'); + return format_plural($size, '1 byte', '@count bytes', array(), $langcode); } else { $size = round($size / 1024, 2); - $suffix = t('KB'); + $suffix = t('KB', array(), $langcode); if ($size >= 1024) { $size = round($size / 1024, 2); - $suffix = t('MB'); + $suffix = t('MB', array(), $langcode); } - return t('@size @suffix', array('@size' => $size, '@suffix' => $suffix)); + return t('@size @suffix', array('@size' => $size, '@suffix' => $suffix), $langcode); } } @@ -1027,16 +1034,19 @@ function format_size($size) { * The length of the interval in seconds. * @param $granularity * How many different units to display in the string. + * @param $langcode + * Optional language code to translate to a language other than + * what is used to display the page. * @return * A translated string representation of the interval. */ -function format_interval($timestamp, $granularity = 2) { +function format_interval($timestamp, $granularity = 2, $langcode = NULL) { $units = array('1 year|@count years' => 31536000, '1 week|@count weeks' => 604800, '1 day|@count days' => 86400, '1 hour|@count hours' => 3600, '1 min|@count min' => 60, '1 sec|@count sec' => 1); $output = ''; foreach ($units as $key => $value) { $key = explode('|', $key); if ($timestamp >= $value) { - $output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]); + $output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1], array(), $langcode); $timestamp %= $value; $granularity--; } @@ -1045,7 +1055,7 @@ function format_interval($timestamp, $granularity = 2) { break; } } - return $output ? $output : t('0 sec'); + return $output ? $output : t('0 sec', array(), $langcode); } /** @@ -1066,10 +1076,13 @@ function format_interval($timestamp, $granularity = 2) { * format. * @param $timezone * Time zone offset in seconds; if omitted, the user's time zone is used. + * @param $langcode + * Optional language code to translate to a language other than + * what is used to display the page. * @return * A translated date string in the requested format. */ -function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) { +function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) { if (!isset($timezone)) { global $user; if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) { @@ -1102,7 +1115,7 @@ function format_date($timestamp, $type = 'medium', $format = '', $timezone = NUL for ($i = 0; $i < $max; $i++) { $c = $format[$i]; if (strpos('AaDFlM', $c) !== FALSE) { - $date .= t(gmdate($c, $timestamp)); + $date .= t(gmdate($c, $timestamp), array(), $langcode); } else if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) { $date .= gmdate($c, $timestamp); diff --git a/includes/locale.inc b/includes/locale.inc index 141279c4a..f2ff4e088 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -37,10 +37,9 @@ function locale_languages_overview_form() { '#options' => $options, '#default_value' => $enabled, ); - $default = language_default(); $form['site_default'] = array('#type' => 'radios', '#options' => $options, - '#default_value' => $default->language, + '#default_value' => language_default('language'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); $form['#theme'] = 'locale_languages_overview_form'; @@ -323,8 +322,7 @@ function locale_languages_edit_form_validate($form, &$form_state, $form_values) if (!empty($form_values['domain']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE domain = '%s' AND language != '%s'", $form_values['domain'], $form_values['langcode']))) { form_set_error('domain', t('The domain (%domain) is already tied to a language (%language).', array('%domain' => $form_values['domain'], '%language' => $duplicate->language))); } - $default = language_default(); - if (empty($form_values['prefix']) && $default->language != $form_values['langcode'] && empty($form_values['domain'])) { + if (empty($form_values['prefix']) && language_default('language') != $form_values['langcode'] && empty($form_values['domain'])) { form_set_error('prefix', t('Only the default language can have both the domain and prefix empty.')); } if (!empty($form_values['prefix']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE prefix = '%s' AND language != '%s'", $form_values['prefix'], $form_values['langcode']))) { @@ -368,8 +366,7 @@ function locale_languages_delete_form($langcode) { drupal_goto('admin/settings/language'); } - $default = language_default(); - if ($default->language == $langcode) { + if (language_default('language') == $langcode) { drupal_set_message(t('The default language cannot be deleted.')); drupal_goto('admin/settings/language'); } diff --git a/modules/aggregator/aggregator.module b/modules/aggregator/aggregator.module index 806c59a95..4467b893a 100644 --- a/modules/aggregator/aggregator.module +++ b/modules/aggregator/aggregator.module @@ -1250,7 +1250,7 @@ function aggregator_page_rss() { $output .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; $output .= "<rss version=\"2.0\">\n"; - $output .= format_rss_channel(variable_get('site_name', 'Drupal') . ' ' . t('aggregator'), url('aggregator' . $url, array('absolute' => TRUE)), variable_get('site_name', 'Drupal') . ' - ' . t('aggregated feeds') . $title, $items, 'en'); + $output .= format_rss_channel(variable_get('site_name', 'Drupal') . ' ' . t('aggregator'), url('aggregator' . $url, array('absolute' => TRUE)), variable_get('site_name', 'Drupal') . ' - ' . t('aggregated feeds') . $title, $items); $output .= "</rss>\n"; drupal_set_header('Content-Type: application/rss+xml; charset=utf-8'); diff --git a/modules/locale/locale.module b/modules/locale/locale.module index 3a4e835c1..7d0933e8e 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -190,8 +190,7 @@ function locale_user($type, $edit, &$user, $category = NULL) { $languages = language_list('enabled'); $languages = $languages['1']; if ($user->language == '') { - $default = language_default(); - $user->language = $default->language; + $user->language = language_default('language'); } $names = array(); foreach ($languages as $langcode => $language) { @@ -367,26 +366,35 @@ function locale_refresh_cache() { * Returns plural form index for a specific number. * * The index is computed from the formula of this language. + * + * @param $count + * Number to return plural for. + * @param $langcode + * Optional language code to translate to a language other than + * what is used to display the page. */ -function locale_get_plural($count) { +function locale_get_plural($count, $langcode = NULL) { global $language; static $locale_formula, $plurals = array(); - if (!isset($plurals[$count])) { + $langcode = $langcode ? $langcode : $language->language; + + if (!isset($plurals[$langcode][$count])) { if (!isset($locale_formula)) { - $locale_formula = $language->formula; + $language_list = language_list(); + $locale_formula[$langcode] = $language_list[$langcode]->formula; } - if ($locale_formula) { + if ($locale_formula[$langcode]) { $n = $count; - $plurals[$count] = @eval("return intval($locale_formula);"); - return $plurals[$count]; + $plurals[$langcode][$count] = @eval('return intval('. $locale_formula[$langcode] .');'); + return $plurals[$langcode][$count]; } else { - $plurals[$count] = -1; + $plurals[$langcode][$count] = -1; return -1; } } - return $plurals[$count]; + return $plurals[$langcode][$count]; } |