summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/bootstrap.inc105
-rw-r--r--includes/common.inc39
-rw-r--r--includes/language.inc136
-rw-r--r--includes/locale.inc536
-rw-r--r--includes/path.inc51
5 files changed, 636 insertions, 231 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 13e584452..26fa25ec2 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -85,15 +85,20 @@ define('DRUPAL_BOOTSTRAP_SESSION', 4);
define('DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE', 5);
/**
- * Seventh bootstrap phase: set $_GET['q'] to Drupal path of request.
+ * Seventh bootstrap phase: find out language of the page.
*/
-define('DRUPAL_BOOTSTRAP_PATH', 6);
+define('DRUPAL_BOOTSTRAP_LANGUAGE', 6);
+
+/**
+ * Eighth bootstrap phase: set $_GET['q'] to Drupal path of request.
+ */
+define('DRUPAL_BOOTSTRAP_PATH', 7);
/**
* Final bootstrap phase: Drupal is fully loaded; validate and fix
* input data.
*/
-define('DRUPAL_BOOTSTRAP_FULL', 7);
+define('DRUPAL_BOOTSTRAP_FULL', 8);
/**
* Role ID for anonymous users; should match what's in the "role" table.
@@ -106,6 +111,30 @@ define('DRUPAL_ANONYMOUS_RID', 1);
define('DRUPAL_AUTHENTICATED_RID', 2);
/**
+ * No language negotiation. The default language is used.
+ */
+define('LANGUAGE_NEGOTIATION_NONE', 0);
+
+/**
+ * Path based negotiation with fallback to default language
+ * if no defined path prefix identified.
+ */
+define('LANGUAGE_NEGOTIATION_PATH_DEFAULT', 1);
+
+/**
+ * Path based negotiation with fallback to user preferences
+ * and browser language detection if no defined path prefix
+ * identified.
+ */
+define('LANGUAGE_NEGOTIATION_PATH', 2);
+
+/**
+ * Domain based negotiation with fallback to default language
+ * if no language identified by domain.
+ */
+define('LANGUAGE_NEGOTIATION_DOMAIN', 3);
+
+/**
* Start the timer with the specified name. If you start and stop
* the same timer multiple times, the measured intervals will be
* accumulated.
@@ -409,6 +438,7 @@ function variable_del($name) {
unset($conf[$name]);
}
+
/**
* Retrieve the current page from the cache.
*
@@ -762,11 +792,12 @@ function drupal_anonymous_user($session = '') {
* DRUPAL_BOOTSTRAP_SESSION: initialize session handling.
* DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start
* the variable system and try to serve a page from the cache.
+ * DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page.
* DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request.
* DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data.
*/
function drupal_bootstrap($phase) {
- static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL);
+ static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL);
while (!is_null($current_phase = array_shift($phases))) {
_drupal_bootstrap($current_phase);
@@ -824,6 +855,10 @@ function _drupal_bootstrap($phase) {
drupal_page_header();
break;
+ case DRUPAL_BOOTSTRAP_LANGUAGE:
+ drupal_init_language();
+ break;
+
case DRUPAL_BOOTSTRAP_PATH:
require_once './includes/path.inc';
// Initialize $_GET['q'] prior to loading modules and invoking hook_init().
@@ -898,3 +933,65 @@ function get_t() {
}
return $t;
}
+
+/**
+ * Choose a language for the current page, based on site and user preferences.
+ */
+function drupal_init_language() {
+ global $language, $user;
+
+ // Ensure the language is correctly returned, even without multilanguage support.
+ // Useful for eg. XML/HTML 'lang' attributes.
+ if (variable_get('language_count', 1) == 1) {
+ $language = language_default();
+ }
+ else {
+ include_once './includes/language.inc';
+ $language = language_initialize();
+ }
+}
+
+/**
+ * Get a list of languages set up indexed by the specified key
+ *
+ * @param $field The field to index the list with.
+ * @param $reset Boolean to request a reset of the list.
+ */
+function language_list($field = 'language', $reset = FALSE) {
+ static $languages = NULL;
+
+ // Reset language list
+ if ($reset) {
+ $languages = NULL;
+ }
+
+ // Init language list
+ if (!isset($languages)) {
+ $result = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC');
+ while ($row = db_fetch_object($result)) {
+ $languages['language'][$row->language] = $row;
+ }
+ }
+
+ // Return the array indexed by the right field
+ if (!isset($languages[$field])) {
+ $languages[$field] = array();
+ foreach($languages['language'] as $lang) {
+ // Some values should be collected into an array
+ if (in_array($field, array('enabled', 'weight'))) {
+ $languages[$field][$lang->$field][$lang->language] = $lang;
+ }
+ else {
+ $languages[$field][$lang->$field] = $lang;
+ }
+ }
+ }
+ return $languages[$field];
+}
+
+/**
+ * Default language used on the site
+ */
+function language_default() {
+ return variable_get('language_default', (object) array('language' => 'en', 'name' => 'English', 'direction' => 0, 'native' => 'English'));
+}
diff --git a/includes/common.inc b/includes/common.inc
index 9515c7623..a51891caf 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -600,33 +600,6 @@ function fix_gpc_magic() {
}
/**
- * Initialize the localization system.
- */
-function locale_initialize() {
- global $user;
-
- if (function_exists('i18n_get_lang')) {
- return i18n_get_lang();
- }
-
- if (function_exists('locale')) {
- $languages = locale_supported_languages();
- $languages = $languages['name'];
- }
- else {
- // Ensure the locale/language is correctly returned, even without locale.module.
- // Useful for e.g. XML/HTML 'lang' attributes.
- $languages = array('en' => 'English');
- }
- if ($user->uid && isset($languages[$user->language])) {
- return $user->language;
- }
- else {
- return key($languages);
- }
-}
-
-/**
* Translate strings to the current locale.
*
* All human-readable text that will be displayed somewhere within a page should be
@@ -722,8 +695,8 @@ function locale_initialize() {
* The translated string.
*/
function t($string, $args = 0) {
- global $locale;
- if (function_exists('locale') && $locale != 'en') {
+ global $language;
+ if (function_exists('locale') && $language->language != 'en') {
$string = locale($string);
}
if (!$args) {
@@ -1177,6 +1150,11 @@ function url($path = NULL, $options = array()) {
'absolute' => FALSE,
'alias' => FALSE,
);
+
+ // May need language dependant rewriting if language.inc is present
+ if (function_exists('language_url_rewrite')) {
+ language_url_rewrite($path, $options);
+ }
if ($options['fragment']) {
$options['fragment'] = '#'. $options['fragment'];
}
@@ -1884,7 +1862,6 @@ function xmlrpc($url) {
function _drupal_bootstrap_full() {
static $called;
- global $locale;
if ($called) {
return;
@@ -1908,8 +1885,6 @@ function _drupal_bootstrap_full() {
fix_gpc_magic();
// Load all enabled modules
module_load_all();
- // Initialize the localization system. Depends on i18n.module being loaded already.
- $locale = locale_initialize();
// Let all modules take action before menu system handles the reqest
module_invoke_all('init');
diff --git a/includes/language.inc b/includes/language.inc
new file mode 100644
index 000000000..d145fd7f7
--- /dev/null
+++ b/includes/language.inc
@@ -0,0 +1,136 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Multiple language handling functionality.
+ */
+
+/**
+ * Choose a language for the page, based on language negotiation settings.
+ */
+function language_initialize() {
+ global $user;
+
+ $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
+ switch($mode) {
+ case LANGUAGE_NEGOTIATION_NONE:
+ return language_default();
+
+ case LANGUAGE_NEGOTIATION_DOMAIN:
+ $languages = language_list();
+ foreach($languages as $language) {
+ $parts = parse_url($language->domain);
+ if ($_SERVER['SERVER_NAME'] == $parts['host']) {
+ return $language;
+ }
+ }
+ return language_default();
+
+ case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
+ case LANGUAGE_NEGOTIATION_PATH:
+ $languages = language_list('prefix');
+ $args = explode('/', $_GET['q']);
+ $language = array_shift($args);
+ if (isset($languages[$language])) {
+ $_GET['q'] = implode('/', $args);
+ return $languages[$language];
+ }
+ elseif ($mode == LANGUAGE_NEGOTIATION_PATH_DEFAULT) {
+ return language_default();
+ }
+ break;
+ }
+
+ // User language.
+ $languages = language_list();
+ if ($user->uid && isset($languages[$user->language])) {
+ return $languages[$user->language];
+ }
+
+ // Browser accept-language parsing.
+ if ($language = language_from_browser()) {
+ return $language;
+ }
+
+ // Fall back on the default if everything else fails.
+ return language_default();
+}
+
+/**
+ * Indetify language from the Accept-language HTTP header we got.
+ */
+function language_from_browser() {
+ // Specified by the user via the browser's Accept Language setting
+ // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5"
+ $browser_langs = array();
+
+ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+ $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+ for ($i = 0; $i < count($browser_accept); $i++) {
+ // The language part is either a code or a code with a quality.
+ // We cannot do anything with a * code, so it is skipped.
+ // If the quality is missing, it is assumed to be 1 according to the RFC.
+ if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($browser_accept[$i]), $found)) {
+ $browser_langs[$found[1]] = (isset($found[3]) ? (float) $found[3] : 1.0);
+ }
+ }
+ }
+
+ // Order the codes by quality
+ arsort($browser_langs);
+
+ // Try to find the first preferred language we have
+ $languages = language_list('enabled');
+ foreach ($browser_langs as $langcode => $q) {
+ if (isset($languages['1'][$langcode])) {
+ return $languages['1'][$langcode];
+ }
+ }
+}
+
+/**
+ * Rewrite URL's with language based prefix. Parameters are the same
+ * as those of the url() function.
+ */
+function language_url_rewrite(&$path, &$options) {
+ global $language;
+
+ // Only modify relative (insite) URLs.
+ if (!$options['absolute']) {
+
+ // Language can be passed as an option, or we go for current language.
+ $path_language = isset($options['language']) ? $options['language'] : $language;
+ switch(variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
+
+ case LANGUAGE_NEGOTIATION_NONE:
+ break;
+
+ case LANGUAGE_NEGOTIATION_DOMAIN:
+ if ($rewritelang->domain) {
+ $options['absolute'] = TRUE;
+ $path = $path_language->domain .'/'. $path;
+ }
+ break;
+
+ case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
+ $default = language_default();
+ if ($path_language->language == $default->language) {
+ break;
+ }
+ // Intentionally no break here.
+
+ case LANGUAGE_NEGOTIATION_PATH:
+ if (isset($path_language->prefix) && $path_language->prefix) {
+ // Get alias if not already aliased.
+ if (!$options['alias']) {
+ $path = drupal_get_path_alias($path, $path_language->language);
+ $options['alias'] = TRUE;
+ }
+ $path = empty($path) ? $path_language->prefix : $path_language->prefix .'/'. $path;
+ }
+ break;
+
+ }
+ }
+}
diff --git a/includes/locale.inc b/includes/locale.inc
index 9e830d4f0..d526a33de 100644
--- a/includes/locale.inc
+++ b/includes/locale.inc
@@ -7,92 +7,101 @@
*/
// ---------------------------------------------------------------------------------
-// Language addition functionality (administration only)
+// Language management functionality (administration only)
/**
* Helper function to add a language
+ *
+ * @param $code
+ * Language code.
+ * @param $name
+ * English name of the language
+ * @param $native
+ * Native name of the language
+ * @param $direction
+ * 0 for left to right, 1 for right to left direction
+ * @param $domain
+ * Optional custom domain name with protocol, without
+ * trailing slash (eg. http://de.example.com).
+ * @param $prefix
+ * Optional path prefix for the language. Defaults to the
+ * language code if omitted.
+ * @param $verbose
+ * Switch to omit the verbose message to the user when used
+ * only as a utility function.
*/
-function _locale_add_language($code, $name, $onlylanguage = TRUE) {
- db_query("INSERT INTO {locales_meta} (locale, name) VALUES ('%s','%s')", $code, $name);
+function _locale_add_language($code, $name, $native, $direction = 0, $domain = '', $prefix = '', $verbose = TRUE) {
+ if (empty($prefix)) {
+ $prefix = $code;
+ }
+ db_query("INSERT INTO {languages} (language, name, native, direction, domain, prefix) VALUES ('%s', '%s', '%s', %d, '%s', '%s')", $code, $name, $native, $direction, $domain, $prefix);
$result = db_query("SELECT lid FROM {locales_source}");
while ($string = db_fetch_object($result)) {
- db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d,'%s', '')", $string->lid, $code);
+ db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d,'%s', '')", $string->lid, $code);
}
// If only the language was added, and not a PO file import triggered
// the language addition, we need to inform the user on how to start
- // a translation
- if ($onlylanguage) {
- drupal_set_message(t('The language %locale has been created and can now be used to import a translation. More information is available in the <a href="@locale-help">help screen</a>.', array('%locale' => t($name), '@locale-help' => url('admin/help/locale'))));
+ // working with the language.
+ if ($verbose) {
+ drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($name), '@locale-help' => url('admin/help/locale'))));
}
else {
- drupal_set_message(t('The language %locale has been created.', array('%locale' => t($name))));
+ drupal_set_message(t('The language %language has been created.', array('%language' => t($name))));
}
- watchdog('locale', t('The %language language (%locale) has been created.', array('%language' => $name, '%locale' => $code)));
+ watchdog('locale', t('The %language language (%code) has been created.', array('%language' => t($name), '%code' => $code)));
}
/**
* User interface for the language management screen.
*/
function _locale_admin_manage_screen() {
- $languages = locale_supported_languages(TRUE, TRUE);
+ $languages = language_list('language', TRUE);
$options = array();
- $form['name'] = array('#tree' => TRUE);
- foreach ($languages['name'] as $key => $lang) {
- $options[$key] = '';
- $status = db_fetch_object(db_query("SELECT isdefault, enabled FROM {locales_meta} WHERE locale = '%s'", $key));
- if ($status->enabled) {
- $enabled[] = $key;
- }
- if ($status->isdefault) {
- $isdefault = $key;
- }
- if ($key == 'en') {
- $form['name']['en'] = array('#value' => check_plain($lang));
- }
- else {
- $original = db_fetch_object(db_query("SELECT COUNT(*) AS strings FROM {locales_source}"));
- $translation = db_fetch_object(db_query("SELECT COUNT(*) AS translation FROM {locales_target} WHERE locale = '%s' AND translation != ''", $key));
-
- $ratio = ($original->strings > 0 && $translation->translation > 0) ? round(($translation->translation/$original->strings)*100., 2) : 0;
+ $form['weight'] = array('#tree' => TRUE);
+ foreach ($languages as $langcode => $language) {
- $form['name'][$key] = array('#type' => 'textfield',
- '#default_value' => $lang,
- '#size' => 15,
- '#maxlength' => 64,
- );
- $form['translation'][$key] = array('#value' => "$translation->translation/$original->strings ($ratio%)");
+ $options[$langcode] = '';
+ if ($language->enabled) {
+ $enabled[] = $langcode;
}
+ $form['weight'][$langcode] = array(
+ '#type' => 'weight',
+ '#default_value' => $language->weight
+ );
+ $form['name'][$langcode] = array('#value' => check_plain($language->name));
+ $form['native'][$langcode] = array('#value' => check_plain($language->native));
+ $form['direction'][$langcode] = array('#value' => ($language->direction ? 'Right to left' : 'Left to right'));
}
$form['enabled'] = array('#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $enabled,
);
+ $default = language_default();
$form['site_default'] = array('#type' => 'radios',
'#options' => $options,
- '#default_value' => $isdefault,
+ '#default_value' => $default->language,
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
- $form['#submit']['locale_admin_manage_screen_submit'] = array();
- $form['#validate']['locale_admin_manage_screen_validate'] = array();
+ $form['#submit']['_locale_admin_manage_screen_submit'] = array();
$form['#theme'] = 'locale_admin_manage_screen';
return $form;
}
/**
- * Theme the locale admin manager form.
+ * Theme the admin langauge manager form.
*/
function theme_locale_admin_manage_screen($form) {
foreach ($form['name'] as $key => $element) {
// Do not take form control structures.
if (is_array($element) && element_child($key)) {
- $rows[] = array(check_plain($key), drupal_render($form['name'][$key]), drupal_render($form['enabled'][$key]), drupal_render($form['site_default'][$key]), ($key != 'en' ? drupal_render($form['translation'][$key]) : t('n/a')), ($key != 'en' ? l(t('delete'), 'admin/build/locale/language/delete/'. $key) : ''));
+ $rows[] = array(array('data' => drupal_render($form['enabled'][$key]), 'align' => 'center'), check_plain($key), '<strong>'. drupal_render($form['name'][$key]) .'</strong>', drupal_render($form['native'][$key]), drupal_render($form['direction'][$key]), drupal_render($form['site_default'][$key]), drupal_render($form['weight'][$key]), l(t('edit'), 'admin/build/locale/language/edit/'. $key). ($key != 'en' ? ' ' .l(t('delete'), 'admin/build/locale/language/delete/'. $key) : ''));
}
}
- $header = array(array('data' => t('Code')), array('data' => t('English name')), array('data' => t('Enabled')), array('data' => t('Default')), array('data' => t('Translated')), array('data' => t('Operations')));
+ $header = array(array('data' => t('Enabled')), array('data' => t('Code')), array('data' => t('English name')), array('data' => t('Native name')), array('data' => t('Direction')), array('data' => t('Default')), array('data' => t('Weight')), array('data' => t('Operations')));
$output = theme('table', $header, $rows);
$output .= drupal_render($form);
@@ -104,69 +113,165 @@ function theme_locale_admin_manage_screen($form) {
*/
function _locale_admin_manage_screen_submit($form_id, $form_values) {
// Save changes to existing languages.
- $languages = locale_supported_languages(FALSE, TRUE);
- foreach ($languages['name'] as $key => $value) {
- if ($form_values['site_default'] == $key) {
- $form_values['enabled'][$key] = 1; // autoenable the default language
+ $languages = language_list();
+ $enabled_count = 0;
+ foreach ($languages as $langcode => $language) {
+ if ($form_values['site_default'] == $langcode) {
+ $form_values['enabled'][$langcode] = 1; // autoenable the default language
}
- $enabled = $form_values['enabled'][$key] ? 1 : 0;
- if ($key == 'en') {
- // Disallow name change for English locale.
- db_query("UPDATE {locales_meta} SET isdefault = %d, enabled = %d WHERE locale = 'en'", ($form_values['site_default'] == $key), $enabled);
+ if ($form_values['enabled'][$langcode]) {
+ $enabled_count++;
+ $language->enabled = 1;
}
else {
- db_query("UPDATE {locales_meta} SET name = '%s', isdefault = %d, enabled = %d WHERE locale = '%s'", $form_values['name'][$key], ($form_values['site_default'] == $key), $enabled, $key);
+ $language->enabled = 0;
}
+ $language->weight = $form_values['weight'][$langcode];
+ db_query("UPDATE {languages} SET enabled = %d, weight = %d WHERE language = '%s'", $language->enabled, $language->weight, $langcode);
+ $languages[$langcode] = $language;
}
drupal_set_message(t('Configuration saved.'));
+ variable_set('language_default', $languages[$form_values['site_default']]);
+ variable_set('language_count', $enabled_count);
- // Changing the locale settings impacts the interface:
+ // Changing the language settings impacts the interface.
cache_clear_all('*', 'cache_page', TRUE);
return 'admin/build/locale/language/overview';
}
+/**
+ * Predefined language setup form.
+ */
function locale_add_language_form() {
- $isocodes = _locale_prepare_iso_list();
+ $predefined = _locale_prepare_predefined_list();
$form = array();
$form['language list'] = array('#type' => 'fieldset',
- '#title' => t('Language list'),
+ '#title' => t('Predefined language'),
'#collapsible' => TRUE,
);
$form['language list']['langcode'] = array('#type' => 'select',
'#title' => t('Language name'),
- '#default_value' => key($isocodes),
- '#options' => $isocodes,
- '#description' => t('Select your language here, or add it below, if you are unable to find it.'),
+ '#default_value' => key($predefined),
+ '#options' => $predefined,
+ '#description' => t('Select the desired language here, or add it below, if you are unable to find it in the list.'),
);
$form['language list']['submit'] = array('#type' => 'submit', '#value' => t('Add language'));
return $form;
}
+/**
+ * Custom language addition form.
+ */
function locale_custom_language_form() {
$form = array();
$form['custom language'] = array('#type' => 'fieldset',
'#title' => t('Custom language'),
'#collapsible' => TRUE,
);
- $form['custom language']['langcode'] = array('#type' => 'textfield',
- '#title' => t('Language code'),
- '#size' => 12,
- '#maxlength' => 60,
- '#required' => TRUE,
- '#description' => t('Commonly this is an <a href="@iso-codes">ISO 639 language code</a> with an optional country code for regional variants. Examples include "en", "en-US" and "zh-cn".', array('@iso-codes' => 'http://www.w3.org/WAI/ER/IG/ert/iso639.htm')),
+ _locale_language_form($form['custom language']);
+ $form['custom language']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Add custom language')
);
- $form['custom language']['langname'] = array('#type' => 'textfield',
+ // Use the validation and submit functions of the add language form.
+ $form['#submit']['locale_add_language_form_submit'] = array();
+ $form['#validate']['locale_add_language_form_validate'] = array();
+ $form['#theme'] = 'locale_add_language_form';
+ return $form;
+}
+
+/**
+ * Editing screen for a particular language.
+ *
+ * @param $langcode
+ * Languauge code of the language to edit.
+ */
+function _locale_admin_manage_edit_screen($langcode) {
+ if ($language = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $langcode))) {
+ $form = array();
+ _locale_language_form($form, $language);
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Save language')
+ );
+ $form['#submit']['locale_edit_language_form_submit'] = array();
+ $form['#validate']['locale_edit_language_form_validate'] = array();
+ $form['#theme'] = 'locale_edit_language_form';
+ return $form;
+ }
+ else {
+ drupal_not_found();
+ }
+}
+
+/**
+ * Common pieces of the language addition and editing form.
+ *
+ * @param $form
+ * A parent form item (or empty array) to add items below.
+ * @param $language
+ * Language object to edit.
+ */
+function _locale_language_form(&$form, $language = NULL) {
+ if (!is_object($language)) {
+ $language = new stdClass();
+ }
+ if (isset($language->language)) {
+ $form['langcode_view'] = array(
+ '#type' => 'item',
+ '#title' => t('Language code'),
+ '#value' => $language->language
+ );
+ $form['langcode'] = array(
+ '#type' => 'value',
+ '#value' => $language->language
+ );
+ }
+ else {
+ $form['langcode'] = array('#type' => 'textfield',
+ '#title' => t('Language code'),
+ '#size' => 12,
+ '#maxlength' => 60,
+ '#required' => TRUE,
+ '#default_value' => @$language->language,
+ '#disabled' => (isset($language->language)),
+ '#description' => t('This should be an <a href="@rfc4646">RFC 4646</a> compliant language identifier. Basic tags use a country code with an optional script or regional variant name, like "en", "en-US" and "zh-Hant".', array('@rfc4646' => 'http://www.ietf.org/rfc/rfc4646.txt')),
+ );
+ }
+ $form['langname'] = array('#type' => 'textfield',
'#title' => t('Language name in English'),
'#maxlength' => 64,
+ '#default_value' => @$language->name,
'#required' => TRUE,
'#description' => t('Name of the language. Will be available for translation in all languages.'),
);
- $form['custom language']['submit'] = array('#type' => 'submit', '#value' => t('Add custom language'));
- // Use the validation and submit functions of the add language form.
- $form['#submit']['locale_add_language_form_submit'] = array();
- $form['#validate']['locale_add_language_form_validate'] = array();
- $form['#theme'] = 'locale_add_language_form';
+ $form['langnative'] = array('#type' => 'textfield',
+ '#title' => t('Native language name'),
+ '#maxlength' => 64,
+ '#default_value' => @$language->native,
+ '#required' => TRUE,
+ '#description' => t('Name of the language in the language being added.'),
+ );
+ $form['prefix'] = array('#type' => 'textfield',
+ '#title' => t('Path prefix'),
+ '#maxlength' => 64,
+ '#default_value' => @$language->prefix,
+ '#description' => t('Optional path prefix, for example "deutsch" for the German version. This value is not used in the "None" and "Domain" negotiation schemes. You can leave this empty if you use "Path only" negotiation and this is the default language. Changing this will break existing URLs.')
+ );
+ $form['domain'] = array('#type' => 'textfield',
+ '#title' => t('Language domain'),
+ '#maxlength' => 64,
+ '#default_value' => @$language->domain,
+ '#description' => t('Optional custom domain with protocol (eg. "http://example.de" or "http://de.example.com" for the German version). This value is only used in the "Domain" negotiation mode. If left empty and in "Domain" mode, this language will not be accessible.'),
+ );
+ $form['direction'] = array('#type' => 'radios',
+ '#title' => t('Direction'),
+ '#required' => TRUE,
+ '#description' => t('Direction of the text being written in this language.'),
+ '#default_value' => @$language->direction,
+ '#options' => array(0 => t('Left to right'), 1 => t('Right to left'))
+ );
return $form;
}
@@ -183,16 +288,21 @@ function _locale_admin_manage_add_screen() {
* Validate the language addition form.
*/
function locale_add_language_form_validate($form_id, $form_values) {
- if ($duplicate = db_num_rows(db_query("SELECT locale FROM {locales_meta} WHERE locale = '%s'", $form_values['langcode'])) != 0) {
- form_set_error(t('The language %language (%code) already exists.', array('%language' => $form_values['langname'], '%code' => $form_values['langcode'])));
+ if ($duplicate = db_num_rows(db_query("SELECT language FROM {languages} WHERE language = '%s'", $form_values['langcode'])) != 0) {
+ form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_values['langname'], '%code' => $form_values['langcode'])));
}
if (!isset($form_values['langname'])) {
- $isocodes = _locale_get_iso639_list();
- if (!isset($isocodes[$form_values['langcode']])) {
+ // Predefined language selection.
+ $predefined = _locale_get_predefined_list();
+ if (!isset($predefined[$form_values['langcode']])) {
form_set_error('langcode', t('Invalid language code.'));
}
}
+ else {
+ // Reuse the editing form validation routine if we add a custom language
+ locale_edit_language_form_validate($form_id, $form_values);
+ }
}
/**
@@ -201,31 +311,108 @@ function locale_add_language_form_validate($form_id, $form_values) {
function locale_add_language_form_submit($form_id, $form_values) {
if (isset($form_values['langname'])) {
// Custom language form.
- _locale_add_language($form_values['langcode'], $form_values['langname']);
+ _locale_add_language($form_values['langcode'], $form_values['langname'], $form_values['langnative'], $form_values['direction'], $form_values['domain'], $form_values['prefix']);
}
else {
- $isocodes = _locale_get_iso639_list();
- _locale_add_language($form_values['langcode'], $isocodes[$form_values['langcode']][0]);
+ // Predefined language selection.
+ $predefined = _locale_get_predefined_list();
+ $lang = &$predefined[$form_values['langcode']];
+ _locale_add_language($form_values['langcode'], $lang[0], isset($lang[1]) ? $lang[1] : $lang[0], isset($lang[2]) ? $lang[2] : 0, '', $form_values['langcode']);
+ }
+
+ return 'admin/build/locale';
+}
+
+/**
+ * Validate the language editing form. Reused for custom language addition too.
+ */
+function locale_edit_language_form_validate($form_id, $form_values) {
+ if (!empty($form_values['domain']) && !empty($form_values['prefix'])) {
+ form_set_error('prefix', t('Domain and path prefix values should not be set at the same time.'));
+ }
+ 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'])) {
+ 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']))) {
+ form_set_error('prefix', t('The prefix (%prefix) is already tied to a language (%language).', array('%prefix' => $form_values['prefix'], '%language' => $duplicate->language)));
+ }
+}
+
+/**
+ * Process the language editing form submission.
+ */
+function locale_edit_language_form_submit($form_id, $form_values) {
+ db_query("UPDATE {languages} SET name = '%s', native = '%s', domain = '%s', prefix = '%s', direction = %d WHERE language = '%s'", $form_values['langname'], $form_values['langnative'], $form_values['domain'], $form_values['prefix'], $form_values['direction'], $form_values['langcode']);
+ $default = language_default();
+ if ($default->language == $form_values['langcode']) {
+ $default->name = $form_values['langname'];
+ $default->native = $form_values['langnative'];
+ $default->domain = $form_values['domain'];
+ $default->prefix = $form_values['prefix'];
+ $default->direction = $form_values['direction'];
+ variable_set('language_default', $default);
}
+ return 'admin/build/locale';
+}
+
+/**
+ * Setting for language negotiation options
+ */
+function locale_configure_language_form() {
+ $form['language_negotiation'] = array(
+ '#title' => t('Language negotiation'),
+ '#type' => 'radios',
+ '#options' => array(
+ LANGUAGE_NEGOTIATION_NONE => t('None. Language will be independent of visitor preferences and language prefixes or domains.'),
+ LANGUAGE_NEGOTIATION_PATH_DEFAULT => t('Path prefix only. If a suitable path prefix is not identified, the default language is used.'),
+ LANGUAGE_NEGOTIATION_PATH => t('Path prefix with language fallback. If a suitable path prefix is not identified, language is based on user preferences and browser language settings.'),
+ LANGUAGE_NEGOTIATION_DOMAIN => t('Domain name only. If a suitable domain name is not identified, the default language is used.')),
+ '#default_value' => variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE),
+ '#description' => t('How should languages get detected? Changing this also changes how paths are constructed, so setting a different value breaks all URLs. Do not change on a live site without thinking twice!')
+ );
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Save settings')
+ );
+ return $form;
+}
+/**
+ * Submit function for language negotiation settings.
+ */
+function locale_configure_language_form_submit($form_id, $form_values) {
+ variable_set('language_negotiation', $form_values['language_negotiation']);
+ drupal_set_message(t('Language negotiation configuration saved.'));
return 'admin/build/locale';
}
+// ---------------------------------------------------------------------------------
+// Translation import/export screens (administration only)
+
/**
* User interface for the translation import screen.
*/
function _locale_admin_import() {
- $languages = locale_supported_languages(FALSE, TRUE);
- $languages = array_map('t', $languages['name']);
+ $languages = language_list();
+ $names = array();
+ foreach ($languages as $langcode => $language) {
+ $names[$langcode] = t($language->name);
+ }
+ // English means the factory default strings,
+ // we should not import into it.
unset($languages['en']);
if (!count($languages)) {
- $languages = _locale_prepare_iso_list();
+ $languages = _locale_prepare_predefined_list();
}
else {
$languages = array(
t('Already added languages') => $languages,
- t('Languages not yet added') => _locale_prepare_iso_list()
+ t('Languages not yet added') => _locale_prepare_predefined_list()
);
}
@@ -240,7 +427,7 @@ function _locale_admin_import() {
);
$form['import']['langcode'] = array('#type' => 'select',
'#title' => t('Import into'),
- '#options' => $languages,
+ '#options' => $names,
'#description' => t('Choose the language you want to add strings into. If you choose a language which is not yet set up, then it will be added.'),
);
$form['import']['mode'] = array('#type' => 'radios',
@@ -259,10 +446,11 @@ function _locale_admin_import() {
*/
function _locale_admin_import_submit($form_id, $form_values) {
// Add language, if not yet supported
- $languages = locale_supported_languages(TRUE, TRUE);
- if (!isset($languages['name'][$form_values['langcode']])) {
- $isocodes = _locale_get_iso639_list();
- _locale_add_language($form_values['langcode'], $isocodes[$form_values['langcode']][0], FALSE);
+ $languages = language_list('language', TRUE);
+ if (!isset($languages[$form_values['langcode']])) {
+ $predefined = _locale_get_predefined_list();
+ $lang = &$predefined[$form_values['langcode']];
+ _locale_add_language($form_values['langcode'], $lang[0], isset($lang[1]) ? $lang[1] : '', isset($lang[2]) ? $lang[2] : 0, '', '', FALSE);
}
// Now import strings into the language
@@ -283,7 +471,7 @@ function _locale_export_po_form($languages) {
);
$form['export']['langcode'] = array('#type' => 'select',
'#title' => t('Language name'),
- '#options' => $languages,
+ '#options' => array_keys($languages),
'#description' => t('Select the language you would like to export in gettext Portable Object (.po) format.'),
);
$form['export']['submit'] = array('#type' => 'submit', '#value' => t('Export'));
@@ -308,8 +496,11 @@ function _locale_export_pot_form() {
* User interface for the translation export screen
*/
function _locale_admin_export_screen() {
- $languages = locale_supported_languages(FALSE, TRUE);
- $languages = array_map('t', $languages['name']);
+ $languages = language_list();
+ $names = array();
+ foreach ($languages as $langcode => $language) {
+ $names[$langcode] = t($language->name);
+ }
unset($languages['en']);
$output = '';
@@ -330,14 +521,20 @@ function _locale_export_po_form_submit($form_id, $form_values) {
_locale_export_po($form_values['langcode']);
}
+// ---------------------------------------------------------------------------------
+// String search and editing (administration only)
+
/**
* User interface for the string search screen
*/
function _locale_string_seek_form() {
// Get *all* languages set up
- $languages = locale_supported_languages(FALSE, TRUE);
- asort($languages['name']); unset($languages['name']['en']);
- $languages['name'] = array_map('check_plain', $languages['name']);
+ $languages = language_list();
+ unset($languages['en']);
+ $names = array();
+ foreach ($languages as $language) {
+ $names[$language->language] = check_plain(t($language->name));
+ }
// Present edit form preserving previous user settings
$query = _locale_string_seek_query();
@@ -347,19 +544,19 @@ function _locale_string_seek_form() {
);
$form['search']['string'] = array('#type' => 'textfield',
'#title' => t('Strings to search for'),
- '#default_value' => $query->string,
+ '#default_value' => @$query['string'],
'#size' => 30,
'#maxlength' => 30,
'#description' => t('Leave blank to show all strings. The search is case sensitive.'),
);
$form['search']['language'] = array('#type' => 'radios',
'#title' => t('Language'),
- '#default_value' => ($query->language ? $query->language : 'all'),
- '#options' => array_merge(array('all' => t('All languages'), 'en' => t('English (provided by Drupal)')), $languages['name']),
+ '#default_value' => (!empty($query['language']) ? $query['language'] : 'all'),
+ '#options' => array_merge(array('all' => t('All languages'), 'en' => t('English (provided by Drupal)')), $names),
);
$form['search']['searchin'] = array('#type' => 'radios',
'#title' => t('Search in'),
- '#default_value' => ($query->searchin ? $query->searchin : 'all'),
+ '#default_value' => (!empty($query['searchin']) ? $query['searchin'] : 'all'),
'#options' => array('all' => t('All strings in that language'), 'translated' => t('Only translated strings'), 'untranslated' => t('Only untranslated strings')),
);
$form['search']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
@@ -372,10 +569,10 @@ function _locale_string_seek_form() {
* User interface for string editing.
*/
function _locale_string_edit($lid) {
- $languages = locale_supported_languages(FALSE, TRUE);
- unset($languages['name']['en']);
+ $languages = language_list();
+ unset($languages['en']);
- $result = db_query('SELECT DISTINCT s.source, t.translation, t.locale FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.lid = %d', $lid);
+ $result = db_query('SELECT DISTINCT s.source, t.translation, t.language FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.lid = %d', $lid);
$form = array();
$form['translations'] = array('#tree' => TRUE);
while ($translation = db_fetch_object($result)) {
@@ -384,13 +581,13 @@ function _locale_string_edit($lid) {
// Approximate the number of rows in a textfield with a maximum of 10.
$rows = min(ceil(str_word_count($orig) / 12), 10);
- $form['translations'][$translation->locale] = array(
+ $form['translations'][$translation->language] = array(
'#type' => 'textarea',
- '#title' => $languages['name'][$translation->locale],
+ '#title' => $languages[$translation->language]->name,
'#default_value' => $translation->translation,
'#rows' => $rows,
);
- unset($languages['name'][$translation->locale]);
+ unset($languages[$translation->language]);
}
// Handle erroneous lid.
@@ -406,10 +603,10 @@ function _locale_string_edit($lid) {
'#weight' => -1,
);
- foreach ($languages['name'] as $key => $lang) {
- $form['translations'][$key] = array(
+ foreach ($languages as $langcode => $language) {
+ $form['translations'][$langcode] = array(
'#type' => 'textarea',
- '#title' => $lang,
+ '#title' => t($language->name),
'#rows' => $rows,
);
}
@@ -427,12 +624,12 @@ function _locale_string_edit($lid) {
function _locale_string_edit_submit($form_id, $form_values) {
$lid = $form_values['lid'];
foreach ($form_values['translations'] as $key => $value) {
- $trans = db_fetch_object(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $key));
+ $trans = db_fetch_object(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND languauge = '%s'", $lid, $key));
if (isset($trans->translation)) {
- db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND locale = '%s'", $value, $lid, $key);
+ db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND language = '%s'", $value, $lid, $key);
}
else {
- db_query("INSERT INTO {locales_target} (lid, translation, locale) VALUES (%d, '%s', '%s')", $lid, $value, $key);
+ db_query("INSERT INTO {locales_target} (lid, translation, language) VALUES (%d, '%s', '%s')", $lid, $value, $key);
}
}
drupal_set_message(t('The string has been saved.'));
@@ -457,6 +654,9 @@ function _locale_string_delete($lid) {
drupal_goto('admin/build/locale/string/search');
}
+// ---------------------------------------------------------------------------------
+// Utility functions
+
/**
* Parses Gettext Portable Object file information and inserts into database
*
@@ -474,7 +674,7 @@ function _locale_import_po($file, $lang, $mode) {
}
// Check if we have the language already in the database
- if (!db_fetch_object(db_query("SELECT locale FROM {locales_meta} WHERE locale = '%s'", $lang))) {
+ if (!db_fetch_object(db_query("SELECT language FROM {languages} WHERE language = '%s'", $lang))) {
drupal_set_message(t('The language selected for import is not supported.'), 'error');
return FALSE;
}
@@ -708,10 +908,10 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
// Get the plural formula
if ($hdr["Plural-Forms"] && $p = _locale_import_parse_plural_forms($hdr["Plural-Forms"], $file->filename)) {
list($nplurals, $plural) = $p;
- db_query("UPDATE {locales_meta} SET plurals = %d, formula = '%s' WHERE locale = '%s'", $nplurals, $plural, $lang);
+ db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", $nplurals, $plural, $lang);
}
else {
- db_query("UPDATE {locales_meta} SET plurals = %d, formula = '%s' WHERE locale = '%s'", 0, '', $lang);
+ db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", 0, '', $lang);
}
$headerdone = TRUE;
}
@@ -738,13 +938,13 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
$lid = $loc->lid;
// update location field
db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $comments, $lid);
- $trans2 = db_fetch_object(db_query("SELECT lid, translation, plid, plural FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
+ $trans2 = db_fetch_object(db_query("SELECT lid, translation, plid, plural FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $lang));
if (!$trans2->lid) { // no translation in current language
- db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
+ db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
$additions++;
} // translation exists
else if ($mode == 'overwrite' || $trans2->translation == '') {
- db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE locale = '%s' AND lid = %d", $trans, $plid, $key, $lang, $lid);
+ db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE language = '%s' AND lid = %d", $trans, $plid, $key, $lang, $lid);
if ($trans2->translation == '') {
$additions++;
}
@@ -757,7 +957,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $comments, $english[$key]);
$loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key]));
$lid = $loc->lid;
- db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
+ db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
if ($trans != '') {
$additions++;
}
@@ -775,13 +975,13 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
$lid = $loc->lid;
// update location field
db_query("UPDATE {locales_source} SET location = '%s' WHERE source = '%s'", $comments, $english);
- $trans = db_fetch_object(db_query("SELECT lid, translation FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
+ $trans = db_fetch_object(db_query("SELECT lid, translation FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $lang));
if (!$trans->lid) { // no translation in current language
- db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);
+ db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);
$additions++;
} // translation exists
else if ($mode == 'overwrite') { //overwrite in any case
- db_query("UPDATE {locales_target} SET translation = '%s' WHERE locale = '%s' AND lid = %d", $translation, $lang, $lid);
+ db_query("UPDATE {locales_target} SET translation = '%s' WHERE language = '%s' AND lid = %d", $translation, $lang, $lid);
if ($trans->translation == '') {
$additions++;
}
@@ -790,7 +990,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
}
} // overwrite if empty string
else if ($trans->translation == '') {
- db_query("UPDATE {locales_target} SET translation = '%s' WHERE locale = '%s' AND lid = %d", $translation, $lang, $lid);
+ db_query("UPDATE {locales_target} SET translation = '%s' WHERE language = '%s' AND lid = %d", $translation, $lang, $lid);
$additions++;
}
}
@@ -798,7 +998,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL
db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $comments, $english);
$loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english));
$lid = $loc->lid;
- db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);
+ db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);
if ($translation != '') {
$additions++;
}
@@ -1104,8 +1304,8 @@ function _locale_export_po($language) {
// Get language specific strings, or all strings
if ($language) {
- $meta = db_fetch_object(db_query("SELECT * FROM {locales_meta} WHERE locale = '%s'", $language));
- $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.locale = '%s' ORDER BY t.plid, t.plural", $language);
+ $meta = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $language));
+ $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.language = '%s' ORDER BY t.plid, t.plural", $language);
}
else {
$result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid ORDER BY t.plid, t.plural");
@@ -1290,40 +1490,27 @@ function _locale_string_language_list($translation) {
// Add CSS
drupal_add_css(drupal_get_path('module', 'locale') .'/locale.css', 'module', 'all', FALSE);
- $languages = locale_supported_languages(FALSE, TRUE);
- unset($languages['name']['en']);
+ $languages = language_list();
+ unset($languages['en']);
$output = '';
- foreach ($languages['name'] as $key => $value) {
- if (isset($translation[$key])) {
- $output .= ($translation[$key] != '') ? $key .' ' : "<em class=\"locale-untranslated\">$key</em> ";
- }
+ foreach ($languages as $langcode => $language) {
+ $output .= (!empty($translation[$langcode])) ? $langcode .' ' : "<em class=\"locale-untranslated\">$langcode</em> ";
}
return $output;
}
/**
- * Build object out of search criteria specified in request variables
+ * Build array out of search criteria specified in request variables
*/
function _locale_string_seek_query() {
- static $query;
+ static $query = NULL;
if (!isset($query)) {
+ $query = array();
$fields = array('string', 'language', 'searchin');
- $query = new stdClass();
- if (is_array($_REQUEST['edit'])) {
- foreach ($_REQUEST['edit'] as $key => $value) {
- if (!empty($value) && in_array($key, $fields)) {
- $query->$key = $value;
- }
- }
- }
- else {
- foreach ($_REQUEST as $key => $value) {
- if (!empty($value) && in_array($key, $fields)) {
- $query->$key = strpos(',', $value) ? explode(',', $value) : $value;
- }
- }
+ foreach ($fields as $field) {
+ $query[$field] = !empty($_REQUEST[$field]) ? $_REQUEST[$field] : '';
}
}
return $query;
@@ -1333,37 +1520,39 @@ function _locale_string_seek_query() {
* Perform a string search and display results in a table
*/
function _locale_string_seek() {
+ $output = '';
+
// We have at least one criterion to match
if ($query = _locale_string_seek_query()) {
- $join = "SELECT s.source, s.location, s.lid, t.translation, t.locale FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid ";
+ $join = "SELECT s.source, s.location, s.lid, t.translation, t.language FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid ";
$arguments = array();
// Compute LIKE section
- switch ($query->searchin) {
+ switch ($query['searchin']) {
case 'translated':
$where = "WHERE (t.translation LIKE '%%%s%%' AND t.translation != '')";
$orderby = "ORDER BY t.translation";
- $arguments[] = $query->string;
+ $arguments[] = $query['string'];
break;
case 'untranslated':
$where = "WHERE (s.source LIKE '%%%s%%' AND t.translation = '')";
$orderby = "ORDER BY s.source";
- $arguments[] = $query->string;
+ $arguments[] = $query['string'];
break;
case 'all' :
default:
$where = "WHERE (s.source LIKE '%%%s%%' OR t.translation LIKE '%%%s%%')";
$orderby = '';
- $arguments[] = $query->string;
- $arguments[] = $query->string;
+ $arguments[] = $query['string'];
+ $arguments[] = $query['string'];
break;
}
- switch ($query->language) {
+ switch ($query['language']) {
// Force search in source strings
case "en":
$sql = $join ." WHERE s.source LIKE '%%%s%%' ORDER BY s.source";
- $arguments = array($query->string); // $where is not used, discard its arguments
+ $arguments = array($query['string']); // $where is not used, discard its arguments
break;
// Search in all languages
case "all":
@@ -1371,8 +1560,8 @@ function _locale_string_seek() {
break;
// Some different language
default:
- $sql = "$join $where AND t.locale = '%s' $orderby";
- $arguments[] = $query->language;
+ $sql = "$join $where AND t.language = '%s' $orderby";
+ $arguments[] = $query['language'];
}
$result = pager_query($sql, 50, 0, NULL, $arguments);
@@ -1380,25 +1569,19 @@ function _locale_string_seek() {
$header = array(t('String'), t('Locales'), array('data' => t('Operations'), 'colspan' => '2'));
$arr = array();
while ($locale = db_fetch_object($result)) {
- $arr[$locale->lid]['locales'][$locale->locale] = $locale->translation;
+ $arr[$locale->lid]['locales'][$locale->language] = $locale->translation;
$arr[$locale->lid]['location'] = $locale->location;
$arr[$locale->lid]['source'] = $locale->source;
}
+ $rows = array();
foreach ($arr as $lid => $value) {
$rows[] = array(array('data' => check_plain(truncate_utf8($value['source'], 150, FALSE, TRUE)) .'<br /><small>'. $value['location'] .'</small>'), array('data' => _locale_string_language_list($value['locales']), 'align' => 'center'), array('data' => l(t('edit'), "admin/build/locale/string/edit/$lid"), 'class' => 'nowrap'), array('data' => l(t('delete'), "admin/build/locale/string/delete/$lid"), 'class' => 'nowrap'));
}
- $request = array();
- if (count($query)) {
- foreach ($query as $key => $value) {
- $request[$key] = (is_array($value)) ? implode(',', $value) : $value;
- }
- }
-
if (count($rows)) {
$output .= theme('table', $header, $rows);
}
- if ($pager = theme('pager', NULL, 50, 0, $request)) {
+ if ($pager = theme('pager', NULL, 50)) {
$output .= $pager;
}
}
@@ -1412,24 +1595,25 @@ function _locale_string_seek() {
/**
* Prepares the language code list for a select form item with only the unsupported ones
*/
-function _locale_prepare_iso_list() {
- $languages = locale_supported_languages(FALSE, TRUE);
- $isocodes = _locale_get_iso639_list();
- foreach ($isocodes as $key => $value) {
- if (isset($languages['name'][$key])) {
- unset($isocodes[$key]);
+function _locale_prepare_predefined_list() {
+ $languages = language_list();
+ $predefined = _locale_get_predefined_list();
+ foreach ($predefined as $key => $value) {
+ if (isset($languages[$key])) {
+ unset($predefined[$key]);
continue;
}
+ // Include native name in output, if possible
if (count($value) == 2) {
$tname = t($value[0]);
- $isocodes[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])";
+ $predefined[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])";
}
else {
- $isocodes[$key] = t($value[0]);
+ $predefined[$key] = t($value[0]);
}
}
- asort($isocodes);
- return $isocodes;
+ asort($predefined);
+ return $predefined;
}
/**
@@ -1437,7 +1621,7 @@ function _locale_prepare_iso_list() {
*
* Based on ISO 639 and http://people.w3.org/rishida/names/languages.html
*/
-function _locale_get_iso639_list() {
+function _locale_get_predefined_list() {
return array(
"aa" => array("Afar"),
"ab" => array("Abkhazian", "аҧсуа бызшәа"),
@@ -1445,7 +1629,7 @@ function _locale_get_iso639_list() {
"af" => array("Afrikaans"),
"ak" => array("Akan"),
"am" => array("Amharic", "አማርኛ"),
- "ar" => array("Arabic", "العربية"),
+ "ar" => array("Arabic", /* Left-to-right marker "‭" */ "العربية", 1),
"as" => array("Assamese"),
"av" => array("Avar"),
"ay" => array("Aymara"),
@@ -1480,7 +1664,7 @@ function _locale_get_iso639_list() {
"es" => array("Spanish", "Español"),
"et" => array("Estonian", "Eesti"),
"eu" => array("Basque", "Euskera"),
- "fa" => array("Persian", "فارسی"),
+ "fa" => array("Persian", /* Left-to-right marker "‭" */ "فارسی", 1),
"ff" => array("Fulah", "Fulfulde"),
"fi" => array("Finnish", "Suomi"),
"fj" => array("Fiji"),
@@ -1494,7 +1678,7 @@ function _locale_get_iso639_list() {
"gu" => array("Gujarati"),
"gv" => array("Manx"),
"ha" => array("Hausa"),
- "he" => array("Hebrew", "עברית"),
+ "he" => array("Hebrew", /* Left-to-right marker "‭" */ "עברית", 1),
"hi" => array("Hindi", "हिन्दी"),
"ho" => array("Hiri Motu"),
"hr" => array("Croatian", "Hrvatski"),
@@ -1561,7 +1745,7 @@ function _locale_get_iso639_list() {
"pa" => array("Punjabi"),
"pi" => array("Pali"),
"pl" => array("Polish", "Polski"),
- "ps" => array("Pashto", "پښتو"),
+ "ps" => array("Pashto", /* Left-to-right marker "‭" */ "پښتو", 1),
"pt" => array("Portuguese, Portugal", "Português"),
"pt-br" => array("Portuguese, Brazil", "Português"),
"qu" => array("Quechua"),
@@ -1605,7 +1789,7 @@ function _locale_get_iso639_list() {
"ty" => array("Tahitian"),
"ug" => array("Uighur"),
"uk" => array("Ukrainian", "Українська"),
- "ur" => array("Urdu", "اردو"),
+ "ur" => array("Urdu", /* Left-to-right marker "‭" */ "اردو", 1),
"uz" => array("Uzbek", "o'zbek"),
"ve" => array("Venda"),
"vi" => array("Vietnamese", "Tiếng Việt"),
diff --git a/includes/path.inc b/includes/path.inc
index 4801458db..9efee799a 100644
--- a/includes/path.inc
+++ b/includes/path.inc
@@ -34,15 +34,21 @@ function drupal_init_path() {
* - source: return the Drupal system URL for a path alias (if one exists).
* @param $path
* The path to investigate for corresponding aliases or system URLs.
+ * @param $path_language
+ * Optional language code to search the path with. Defaults to the page language.
+ * If there's no path defined for that language it will search paths without
+ * language.
*
* @return
* Either a Drupal system path, an aliased path, or FALSE if no path was
* found.
*/
-function drupal_lookup_path($action, $path = '') {
- // $map keys are Drupal paths and the values are the corresponding aliases
- static $map = array(), $no_src = array();
- static $count;
+function drupal_lookup_path($action, $path = '', $path_language = '') {
+ global $language;
+ // $map is an array with language keys, holding arrays of Drupal paths to alias relations
+ static $map = array(), $no_src = array(), $count;
+
+ $path_language = $path_language ? $path_language : $language->language;
// Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
if (!isset($count)) {
@@ -55,26 +61,29 @@ function drupal_lookup_path($action, $path = '') {
}
elseif ($count > 0 && $path != '') {
if ($action == 'alias') {
- if (isset($map[$path])) {
- return $map[$path];
+ if (isset($map[$path_language][$path])) {
+ return $map[$path_language][$path];
}
- $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path));
- $map[$path] = $alias;
+ // Get the most fitting result falling back with alias without language
+ $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language));
+ $map[$path_language][$path] = $alias;
return $alias;
}
// Check $no_src for this $path in case we've already determined that there
// isn't a path that has this alias
- elseif ($action == 'source' && !isset($no_src[$path])) {
+ elseif ($action == 'source' && !isset($no_src[$path_language][$path])) {
// Look for the value $path within the cached $map
- if (!$src = array_search($path, $map)) {
- if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) {
- $map[$src] = $path;
+ $src = '';
+ if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
+ // Get the most fitting result falling back with alias without language
+ if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language))) {
+ $map[$path_language][$src] = $path;
}
else {
// We can't record anything into $map because we do not have a valid
// index and there is no need because we have not learned anything
// about any Drupal path. Thus cache to $no_src.
- $no_src[$path] = TRUE;
+ $no_src[$path_language][$path] = TRUE;
}
}
return $src;
@@ -89,18 +98,20 @@ function drupal_lookup_path($action, $path = '') {
*
* @param $path
* An internal Drupal path.
+ * @param $path_language
+ * An optional language code to look up the path in.
*
* @return
* An aliased path if one was found, or the original path if no alias was
* found.
*/
-function drupal_get_path_alias($path) {
+function drupal_get_path_alias($path, $path_language = '') {
$result = $path;
- if ($alias = drupal_lookup_path('alias', $path)) {
+ if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
$result = $alias;
}
if (function_exists('custom_url_rewrite')) {
- $result = custom_url_rewrite('alias', $result, $path);
+ $result = custom_url_rewrite('alias', $result, $path, $path_language);
}
return $result;
}
@@ -110,18 +121,20 @@ function drupal_get_path_alias($path) {
*
* @param $path
* A Drupal path alias.
+ * @param $path_language
+ * An optional language code to look up the path in.
*
* @return
* The internal path represented by the alias, or the original alias if no
* internal path was found.
*/
-function drupal_get_normal_path($path) {
+function drupal_get_normal_path($path, $path_language = '') {
$result = $path;
- if ($src = drupal_lookup_path('source', $path)) {
+ if ($src = drupal_lookup_path('source', $path, $path_language)) {
$result = $src;
}
if (function_exists('custom_url_rewrite')) {
- $result = custom_url_rewrite('source', $result, $path);
+ $result = custom_url_rewrite('source', $result, $path, $path_language);
}
return $result;
}