From fff45d1f4bad87b8124ef383de5eae6d4f9a982a Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Thu, 3 May 2007 09:51:08 +0000 Subject: [PATCH] - Patch #139970 by Gabor: locale cleanup. --- includes/locale.inc | 750 ++++++++++++++++++++++------------ modules/locale/locale.info | 2 +- modules/locale/locale.install | 29 +- modules/locale/locale.module | 349 ++++++---------- 4 files changed, 653 insertions(+), 477 deletions(-) diff --git a/includes/locale.inc b/includes/locale.inc index fd9cbdcaab9..94a855baba1 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -3,60 +3,18 @@ /** * @file - * Admin-related functions for locale.module. + * Administration functions for locale.module. */ -// --------------------------------------------------------------------------------- -// 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. + * @defgroup locale-language-overview Language overview functionality + * @{ */ -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, 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 - // 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 help screen.', array('%language' => t($name), '@locale-help' => url('admin/help/locale')))); - } - else { - drupal_set_message(t('The language %language has been created.', array('%language' => t($name)))); - } - - watchdog('locale', 'The %language language (%code) has been created.', array('%language' => $name, '%code' => $code)); -} /** - * User interface for the language management screen. + * User interface for the language overview screen. */ -function _locale_admin_manage_screen() { +function locale_languages_overview_form() { $languages = language_list('language', TRUE); $options = array(); @@ -73,7 +31,7 @@ function _locale_admin_manage_screen() { ); $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['direction'][$langcode] = array('#value' => ($language->direction == LANGUAGE_RTL ? 'Right to left' : 'Left to right')); } $form['enabled'] = array('#type' => 'checkboxes', '#options' => $options, @@ -85,21 +43,20 @@ function _locale_admin_manage_screen() { '#default_value' => $default->language, ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); - $form['#submit']['_locale_admin_manage_screen_submit'] = array(); - $form['#theme'] = 'locale_admin_manage_screen'; + $form['#theme'] = 'locale_languages_overview_form'; return $form; } /** - * Theme the admin langauge manager form. + * Theme the langauge overview form. */ -function theme_locale_admin_manage_screen($form) { +function theme_locale_languages_overview_form($form) { $default = language_default(); foreach ($form['name'] as $key => $element) { // Do not take form control structures. if (is_array($element) && element_child($key)) { - $rows[] = array(array('data' => drupal_render($form['enabled'][$key]), 'align' => 'center'), check_plain($key), ''. drupal_render($form['name'][$key]) .'', 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' && $key != $default->language) ? ' '. l(t('delete') ,'admin/build/locale/language/delete/'. $key) : '')); + $rows[] = array(array('data' => drupal_render($form['enabled'][$key]), 'align' => 'center'), check_plain($key), ''. drupal_render($form['name'][$key]) .'', 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/settings/language/edit/'. $key). (($key != 'en' && $key != $default->language) ? ' '. l(t('delete') ,'admin/settings/language/delete/'. $key) : '')); } } $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'))); @@ -110,15 +67,15 @@ function theme_locale_admin_manage_screen($form) { } /** - * Process locale admin manager form submissions. + * Process language overview form submissions, updating existing languages. */ -function _locale_admin_manage_screen_submit($form_id, $form_values) { - // Save changes to existing languages. +function locale_languages_overview_form_submit($form_id, $form_values) { $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 + // Automatically enable the default language. + $form_values['enabled'][$langcode] = 1; } if ($form_values['enabled'][$langcode]) { $enabled_count++; @@ -138,13 +95,30 @@ function _locale_admin_manage_screen_submit($form_id, $form_values) { // Changing the language settings impacts the interface. cache_clear_all('*', 'cache_page', TRUE); - return 'admin/build/locale/language/overview'; + return 'admin/settings/language'; +} +/** + * @} End of "locale-language-overview" + */ + +/** + * @defgroup locale-language-add-edit Language addition and editing functionality + * @{ + */ + +/** + * User interface for the language addition screen. + */ +function locale_languages_add_screen() { + $output = drupal_get_form('locale_languages_predefined_form'); + $output .= drupal_get_form('locale_languages_custom_form'); + return $output; } /** * Predefined language setup form. */ -function locale_add_language_form() { +function locale_languages_predefined_form() { $predefined = _locale_prepare_predefined_list(); $form = array(); $form['language list'] = array('#type' => 'fieldset', @@ -164,21 +138,20 @@ function locale_add_language_form() { /** * Custom language addition form. */ -function locale_custom_language_form() { +function locale_languages_custom_form() { $form = array(); $form['custom language'] = array('#type' => 'fieldset', '#title' => t('Custom language'), '#collapsible' => TRUE, ); - _locale_language_form($form['custom language']); + _locale_languages_common_controls($form['custom language']); $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'; + // Reuse the validation and submit functions of the predefined language setup form. + $form['#submit']['locale_languages_predefined_form_submit'] = array(); + $form['#validate']['locale_languages_predefined_form_validate'] = array(); return $form; } @@ -188,17 +161,16 @@ function locale_custom_language_form() { * @param $langcode * Language code of the language to edit. */ -function _locale_admin_manage_edit_screen($langcode) { +function locale_languages_edit_form($langcode) { if ($language = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $langcode))) { $form = array(); - _locale_language_form($form, $language); + _locale_languages_common_controls($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'; + $form['#submit']['locale_languages_edit_form_submit'] = array(); + $form['#validate']['locale_languages_edit_form_validate'] = array(); return $form; } else { @@ -207,14 +179,14 @@ function _locale_admin_manage_edit_screen($langcode) { } /** - * Common pieces of the language addition and editing form. + * Common elements 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) { +function _locale_languages_common_controls(&$form, $language = NULL) { if (!is_object($language)) { $language = new stdClass(); } @@ -271,63 +243,57 @@ function _locale_language_form(&$form, $language = NULL) { '#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')) + '#options' => array(LANGUAGE_LTR => t('Left to right'), LANGUAGE_RTL => t('Right to left')) ); return $form; } -/** - * User interface for the language addition screen. - */ -function _locale_admin_manage_add_screen() { - $output = drupal_get_form('locale_add_language_form'); - $output .= drupal_get_form('locale_custom_language_form'); - return $output; -} - /** * Validate the language addition form. */ -function locale_add_language_form_validate($form_id, $form_values) { - 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['name'], '%code' => $form_values['langcode']))); +function locale_languages_predefined_form_validate($form_id, $form_values) { + $langcode = $form_values['langcode']; + + if ($duplicate = db_num_rows(db_query("SELECT language FROM {languages} WHERE language = '%s'", $langcode)) != 0) { + form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_values['name'], '%code' => $langcode))); } if (!isset($form_values['name'])) { // Predefined language selection. $predefined = _locale_get_predefined_list(); - if (!isset($predefined[$form_values['langcode']])) { + if (!isset($predefined[$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); + // Reuse the editing form validation routine if we add a custom language. + locale_languages_edit_form_validate($form_id, $form_values); } } /** * Process the language addition form submission. */ -function locale_add_language_form_submit($form_id, $form_values) { +function locale_languages_predefined_form_submit($form_id, $form_values) { + $langcode = $form_values['langcode']; if (isset($form_values['name'])) { // Custom language form. - _locale_add_language($form_values['langcode'], $form_values['name'], $form_values['native'], $form_values['direction'], $form_values['domain'], $form_values['prefix']); + locale_add_language($langcode, $form_values['name'], $form_values['native'], $form_values['direction'], $form_values['domain'], $form_values['prefix']); } else { // 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']); + $lang = &$predefined[$langcode]; + locale_add_language($langcode, $lang[0], isset($lang[1]) ? $lang[1] : $lang[0], isset($lang[2]) ? $lang[2] : 0, '', $langcode); } - return 'admin/build/locale'; + return 'admin/settings/language'; } /** * Validate the language editing form. Reused for custom language addition too. */ -function locale_edit_language_form_validate($form_id, $form_values) { +function locale_languages_edit_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.')); } @@ -346,7 +312,7 @@ function locale_edit_language_form_validate($form_id, $form_values) { /** * Process the language editing form submission. */ -function locale_edit_language_form_submit($form_id, $form_values) { +function locale_languages_edit_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['name'], $form_values['native'], $form_values['domain'], $form_values['prefix'], $form_values['direction'], $form_values['langcode']); $default = language_default(); if ($default->language == $form_values['langcode']) { @@ -356,13 +322,78 @@ function locale_edit_language_form_submit($form_id, $form_values) { } variable_set('language_default', $default); } - return 'admin/build/locale'; + return 'admin/settings/language'; } +/** + * @} End of "locale-language-add-edit" + */ + +/** + * @defgroup locale-language-delete Language deletion functionality + * @{ + */ + +/** + * User interface for the language deletion confirmation screen. + */ +function locale_languages_delete_form($langcode) { + + // Do not allow deletion of English locale. + if ($langcode == 'en') { + drupal_set_message(t('The English language cannot be deleted.')); + drupal_goto('admin/settings/language'); + } + + $default = language_default(); + if ($default->language == $langcode) { + drupal_set_message(t('The default language cannot be deleted.')); + drupal_goto('admin/settings/language'); + } + + // For other languages, warn user that data loss is ahead. + $languages = language_list(); + + if (!isset($languages[$langcode])) { + drupal_not_found(); + } + else { + $form['langcode'] = array('#type' => 'value', '#value' => $langcode); + return confirm_form($form, t('Are you sure you want to delete the language %name?', array('%name' => t($languages[$langcode]->name))), 'admin/settings/language', t('Deleting a language will remove all interface translations associated with it, and posts in this language will be set to be language neutral. This action cannot be undone.'), t('Delete'), t('Cancel')); + } +} + +/** + * Process language deletion submissions. + */ +function locale_languages_delete_form_submit($form_id, $form_values) { + $languages = language_list(); + if (isset($languages[$form_values['langcode']])) { + db_query("DELETE FROM {languages} WHERE language = '%s'", $form_values['langcode']); + db_query("DELETE FROM {locales_target} WHERE language = '%s'", $form_values['langcode']); + db_query("UPDATE {node} SET language = '' WHERE language = '%s'", $form_values['langcode']); + $variables = array('%locale' => $languages[$form_values['langcode']]->name); + drupal_set_message(t('The language %locale has been removed.', $variables)); + watchdog('locale', 'The language %locale has been removed.', $variables); + } + + // Changing the language settings impacts the interface: + cache_clear_all('*', 'cache_page', TRUE); + + return 'admin/settings/language'; +} +/** + * @} End of "locale-language-add-edit" + */ + +/** + * @defgroup locale-languages-negotiation Language negotiation options screen + * @{ + */ /** * Setting for language negotiation options */ -function locale_configure_language_form() { +function locale_languages_configure_form() { $form['language_negotiation'] = array( '#title' => t('Language negotiation'), '#type' => 'radios', @@ -372,7 +403,7 @@ function locale_configure_language_form() { 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!') + '#description' => t('The used language detection mode. Changing this also changes how paths are constructed, so setting a different value breaks all incoming links. Do not change on a live site without thinking twice!') ); $form['submit'] = array( '#type' => 'submit', @@ -384,21 +415,128 @@ function locale_configure_language_form() { /** * Submit function for language negotiation settings. */ -function locale_configure_language_form_submit($form_id, $form_values) { +function locale_languages_configure_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'; + return 'admin/settings/language'; +} +/** + * @} End of "locale-languages-negotiation" + */ + +/** + * @defgroup locale-translate-overview Translation overview screen. + * @{ + */ + +/** + * Overview screen for translations. + */ +function locale_translate_overview_screen() { + $languages = language_list('language', TRUE); + $groups = module_invoke_all('locale', 'groups'); + + // Build headers with all groups in order. + $headers = array_merge(array(t('Language')), array_values($groups)); + + // Collect summaries of all source strings in all groups. + $sums = db_query("SELECT COUNT(*) AS strings, textgroup FROM {locales_source} GROUP BY textgroup"); + $groupsums = array(); + while ($group = db_fetch_object($sums)) { + $groupsums[$group->textgroup] = $group->strings; + } + + // Setup overview table with default values, ensuring common order for values. + $rows = array(); + foreach ($languages as $langcode => $language) { + $rows[$langcode] = array('name' => ($langcode == 'en' ? t('English (built-in)') : t($language->name))); + foreach ($groups as $group => $name) { + $rows[$langcode][$group] = ($langcode == 'en' ? t('n/a') : '0/'. (isset($groupsums[$group]) ? $groupsums[$group] : 0) . ' (0%)'); + } + } + + // Languages with at least one record in the locale table. + $translations = db_query("SELECT COUNT(*) AS translation, t.language, s.textgroup FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE translation != '' GROUP BY textgroup, language"); + while ($data = db_fetch_object($translations)) { + $ratio = (!empty($groupsums[$data->textgroup]) && $data->translation > 0) ? round(($data->translation/$groupsums[$data->textgroup])*100., 2) : 0; + $rows[$data->language][$data->textgroup] = $data->translation .'/'. $groupsums[$data->textgroup] ." ($ratio%)"; + } + + return theme('table', $headers, $rows); +} +/** + * @} End of "locale-translate-overview" + */ + +/** + * @defgroup locale-translate-seek Translation search screen. + * @{ + */ + +/** + * String search screen. + */ +function locale_translate_seek_screen() { + $output = _locale_translate_seek(); + $output .= drupal_get_form('locale_translate_seek_form'); + return $output; } -// --------------------------------------------------------------------------------- -// Translation import/export screens (administration only) +/** + * User interface for the string search screen. + */ +function locale_translate_seek_form() { + // Get all languages, except English + $languages = locale_language_list('name', TRUE); + unset($languages['en']); + + // Present edit form preserving previous user settings + $query = _locale_translate_seek_query(); + $form = array(); + $form['search'] = array('#type' => 'fieldset', + '#title' => t('Search'), + ); + $form['search']['string'] = array('#type' => 'textfield', + '#title' => t('String contains'), + '#default_value' => @$query['string'], + '#description' => t('Leave blank to show all strings. The search is case sensitive.'), + ); + $form['search']['language'] = array('#type' => 'radios', + '#title' => t('Language'), + '#default_value' => (!empty($query['language']) ? $query['language'] : 'all'), + '#options' => array_merge(array('all' => t('All languages'), 'en' => t('English (provided by Drupal)')), $languages), + ); + $form['search']['translation'] = array('#type' => 'radios', + '#title' => t('Search in'), + '#default_value' => (!empty($query['translation']) ? $query['translation'] : 'all'), + '#options' => array('all' => t('All strings in that language'), 'translated' => t('Only translated strings'), 'untranslated' => t('Only untranslated strings')), + ); + $groups = module_invoke_all('locale', 'groups'); + $form['search']['group'] = array('#type' => 'radios', + '#title' => t('Limit search to'), + '#default_value' => (!empty($query['group']) ? $query['group'] : 'all'), + '#options' => array_merge(array('all' => t('All text groups')), $groups), + ); + + $form['search']['submit'] = array('#type' => 'submit', '#value' => t('Search')); + $form['#redirect'] = FALSE; + + return $form; +} +/** + * @} End of "locale-translate-seek" + */ + +/** + * @defgroup locale-translate-import Translation import screen. + * @{ + */ /** * User interface for the translation import screen. */ -function _locale_admin_import() { - // English means the factory default strings, - // we should not import into it. +function locale_translate_import_form() { + // Get all languages, except English $names = locale_language_list('name', TRUE); unset($names['en']); @@ -427,7 +565,13 @@ function _locale_admin_import() { '#title' => t('Import into'), '#options' => $languages, '#default_value' => $default, - '#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.'), + '#description' => t('Choose the language you want to add strings into. If you choose a language which is not yet set up, it will be added.'), + ); + $form['import']['group'] = array('#type' => 'radios', + '#title' => t('Text group'), + '#default_value' => 'default', + '#options' => module_invoke_all('locale', 'groups'), + '#description' => t('Imported translations will be added to this text group.'), ); $form['import']['mode'] = array('#type' => 'radios', '#title' => t('Mode'), @@ -443,20 +587,21 @@ function _locale_admin_import() { /** * Process the locale import form submission. */ -function _locale_admin_import_submit($form_id, $form_values) { +function locale_translate_import_form_submit($form_id, $form_values) { // Ensure we have the file uploaded if ($file = file_check_upload('file')) { // Add language, if not yet supported $languages = language_list('language', TRUE); - if (!isset($languages[$form_values['langcode']])) { + $langcode = $form_values['langcode']; + if (!isset($languages[$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); + $lang = &$predefined[$langcode]; + locale_add_language($langcode, $lang[0], isset($lang[1]) ? $lang[1] : '', isset($lang[2]) ? $lang[2] : 0, '', '', FALSE); } // Now import strings into the language - if ($ret = _locale_import_po($file, $form_values['langcode'], $form_values['mode']) == FALSE) { + if ($ret = _locale_import_po($file, $langcode, $form_values['mode'], $form_values['group']) == FALSE) { $variables = array('%filename' => $file->filename); drupal_set_message(t('The translation import of %filename failed.', $variables), 'error'); watchdog('locale', 'The translation import of %filename failed.', $variables, WATCHDOG_ERROR); @@ -464,13 +609,43 @@ function _locale_admin_import_submit($form_id, $form_values) { } else { drupal_set_message(t('File to import not found.'), 'error'); - return 'admin/build/locale/string/import'; + return 'admin/build/translate/import'; } - return 'admin/build/locale'; + return 'admin/build/translate'; +} +/** + * @} End of "locale-translate-import" + */ + +/** + * @defgroup locale-translate-export Translation export screen. + * @{ + */ + +/** + * User interface for the translation export screen. + */ +function locale_translate_export_screen() { + // Get all languages, except English + $names = locale_language_list('name', TRUE); + unset($names['en']); + $output = ''; + // Offer translation export if any language is set up. + if (count($names)) { + $output = drupal_get_form('locale_translate_export_po_form', $names); + } + $output .= drupal_get_form('locale_translate_export_pot_form'); + return $output; } -function _locale_export_po_form($names) { +/** + * Form to export PO files for the languages provided. + * + * @param $names + * An associate array with localized language names + */ +function locale_translate_export_po_form($names) { $form['export'] = array('#type' => 'fieldset', '#title' => t('Export translation'), '#collapsible' => TRUE, @@ -480,99 +655,57 @@ function _locale_export_po_form($names) { '#options' => $names, '#description' => t('Select the language you would like to export in gettext Portable Object (.po) format.'), ); + $form['export']['group'] = array('#type' => 'radios', + '#title' => t('Text group'), + '#default_value' => 'default', + '#options' => module_invoke_all('locale', 'groups'), + ); $form['export']['submit'] = array('#type' => 'submit', '#value' => t('Export')); return $form; } -function _locale_export_pot_form() { +/** + * Translation template export form. + */ +function locale_translate_export_pot_form() { // Complete template export of the strings $form['export'] = array('#type' => 'fieldset', '#title' => t('Export template'), '#collapsible' => TRUE, - '#description' => t('Generate a gettext Portable Object Template (.pot) file with all the interface strings from the Drupal locale database.'), + '#description' => t('Generate a gettext Portable Object Template (.pot) file with all strings from the Drupal locale database.'), + ); + $form['export']['group'] = array('#type' => 'radios', + '#title' => t('Text group'), + '#default_value' => 'default', + '#options' => module_invoke_all('locale', 'groups'), ); $form['export']['submit'] = array('#type' => 'submit', '#value' => t('Export')); - $form['#submit']['_locale_export_po_form_submit'] = array(); - $form['#validate']['_locale_export_po_form_validate'] = array(); - $form['#theme'] = '_locale_export_po_form'; + // Reuse PO export submission callback. + $form['#submit']['locale_translate_export_po_form_submit'] = array(); + $form['#validate']['locale_translate_export_po_form_validate'] = array(); return $form; } /** - * User interface for the translation export screen + * Process a translation (or template) export form submission. */ -function _locale_admin_export_screen() { - // Omit English from the exportable languages list - $names = locale_language_list('name', TRUE); - unset($names['en']); - - $output = ''; - // Offer language specific export if any language is set up - if (count($names)) { - $output = drupal_get_form('_locale_export_po_form', $names); - } - - $output .= drupal_get_form('_locale_export_pot_form'); - - return $output; +function locale_translate_export_po_form_submit($form_id, $form_values) { + // If template is required, language code is not given. + _locale_export_po(isset($form_values['langcode']) ? $form_values['langcode'] : NULL, $form_values['group']); } +/** + * @} End of "locale-translate-export" + */ /** - * Process a locale export form submission. + * @defgroup locale-translate-edit Translation text editing + * @{ */ -function _locale_export_po_form_submit($form_id, $form_values) { - // If template is required, language code is not given - _locale_export_po(isset($form_values['langcode']) ? $form_values['langcode'] : NULL); -} - -// --------------------------------------------------------------------------------- -// String search and editing (administration only) - -/** - * User interface for the string search screen - */ -function _locale_string_seek_form() { - // Get *all* languages set up - $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(); - $form = array(); - $form['search'] = array('#type' => 'fieldset', - '#title' => t('Search'), - ); - $form['search']['string'] = array('#type' => 'textfield', - '#title' => t('Strings to search for'), - '#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' => (!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' => (!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')); - $form['#redirect'] = FALSE; - - return $form; -} /** * User interface for string editing. */ -function _locale_string_edit($lid) { +function locale_translate_edit_form($lid) { $languages = language_list(); unset($languages['en']); @@ -596,8 +729,8 @@ function _locale_string_edit($lid) { // Handle erroneous lid. if (!isset($orig)) { - drupal_set_message(t('String not found.')); - drupal_goto('admin/build/locale/string/search'); + drupal_set_message(t('String not found.'), 'error'); + drupal_goto('admin/build/translate/search'); } // Add original text. Assign negative weight so that it floats to the top. @@ -625,7 +758,7 @@ function _locale_string_edit($lid) { * Process string editing form submissions. * Saves all translations of one string submitted from a form. */ -function _locale_string_edit_submit($form_id, $form_values) { +function locale_translate_edit_form_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 language = '%s'", $lid, $key)); @@ -643,23 +776,89 @@ function _locale_string_edit_submit($form_id, $form_values) { // Rebuild the menu, strings may have changed. menu_rebuild(); - return 'admin/build/locale/string/search'; + return 'admin/build/translate/search'; } +/** + * @} End of "locale-translate-edit" + */ + +/** + * @defgroup locale-translate-delete Translation delete interface. + * @{ + */ /** * Delete a language string. */ -function _locale_string_delete($lid) { +function locale_translate_delete($lid) { db_query('DELETE FROM {locales_source} WHERE lid = %d', $lid); db_query('DELETE FROM {locales_target} WHERE lid = %d', $lid); locale_refresh_cache(); drupal_set_message(t('The string has been removed.')); - - drupal_goto('admin/build/locale/string/search'); + drupal_goto('admin/build/translate/search'); } +/** + * @} End of "locale-translate-delete" + */ -// --------------------------------------------------------------------------------- -// Utility functions +/** + * @defgroup locale-api-add Language addition API. + * @{ + */ + +/** + * API function to add a language. + * + * @param $langcode + * Language code. + * @param $name + * English name of the language + * @param $native + * Native name of the language + * @param $direction + * LANGUAGE_LTR or LANGUAGE_RTL + * @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($langcode, $name, $native, $direction = LANGUAGE_RTL, $domain = '', $prefix = '', $verbose = TRUE) { + // Default prefix on language code. + if (empty($prefix)) { + $prefix = $code; + } + + db_query("INSERT INTO {languages} (language, name, native, direction, domain, prefix) VALUES ('%s', '%s', '%s', %d, '%s', '%s')", $langcode, $name, $native, $direction, $domain, $prefix); + + // Add empty translations for strings (to optimize locale()) + $result = db_query("SELECT lid FROM {locales_source}"); + while ($string = db_fetch_object($result)) { + db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d,'%s', '')", $string->lid, $code); + } + + // Set message depending on the verbosity required. + if ($verbose) { + drupal_set_message(t('The language %language has been created and can now be used. More information is available on the help screen.', array('%language' => t($name), '@locale-help' => url('admin/help/locale')))); + } + else { + drupal_set_message(t('The language %language has been created.', array('%language' => t($name)))); + } + + watchdog('locale', 'The %language language (%code) has been created.', array('%language' => $name, '%code' => $langcode)); +} +/** + * @} End of "locale-api-add" + */ + +/** + * @defgroup locale-api-import Translation import API. + * @{ + */ /** * Parses Gettext Portable Object file information and inserts into database @@ -670,8 +869,10 @@ function _locale_string_delete($lid) { * Language code * @param $mode * Should existing translations be replaced ('overwrite' or 'keep') + * @param $group + * Text group to import PO file into (eg. 'default' for interface translations) */ -function _locale_import_po($file, $lang, $mode) { +function _locale_import_po($file, $lang, $mode, $group = NULL) { // If not in 'safe mode', increase the maximum execution time: if (!ini_get('safe_mode')) { set_time_limit(240); @@ -684,7 +885,7 @@ function _locale_import_po($file, $lang, $mode) { } // Get strings from file (returns on failure after a partial import, or on success) - $status = _locale_import_read_po('db-store', $file, $mode, $lang); + $status = _locale_import_read_po('db-store', $file, $mode, $lang, $group); if ($status === FALSE) { // error messages are set in _locale_import_read_po return FALSE; @@ -719,8 +920,10 @@ function _locale_import_po($file, $lang, $mode) { * Should existing translations be replaced ('overwrite' or 'keep') * @param $lang * Language code + * @param $group + * Text group to import PO file into (eg. 'default' for interface translations) */ -function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL) { +function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL, $group = NULL) { $fd = fopen($file->filepath, "rb"); // File will get closed by PHP on return if (!$fd) { @@ -884,8 +1087,10 @@ function _locale_import_message($message, $file, $lineno = NULL) { * Language to store the string in * @param $file * Object representation of file being imported, only required when op is 'db-store' + * @param $group + * Text group to import PO file into (eg. 'default' for interface translations) */ -function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NULL, $file = NULL) { +function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NULL, $file = NULL, $group = 'default') { static $additions = 0; static $updates = 0; static $headerdone = FALSE; @@ -912,7 +1117,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL $hdr = _locale_import_parse_header($value['msgstr']); // Get the plural formula - if ($hdr["Plural-Forms"] && $p = _locale_import_parse_plural_forms($hdr["Plural-Forms"], $file->filename)) { + if (isset($hdr["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($hdr["Plural-Forms"], $file->filename)) { list($nplurals, $plural) = $p; db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", $nplurals, $plural, $lang); } @@ -939,7 +1144,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL if ($key == 0) { $plid = 0; } - $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key])); + $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $english[$key], $group)); if (!empty($loc->lid)) { // a string exists $lid = $loc->lid; // update location field @@ -960,8 +1165,8 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL } } else { // no string - 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])); + db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', '%s')", $comments, $english[$key], $group); + $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $english[$key], $group)); $lid = $loc->lid; db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key); if ($trans != '') { @@ -976,7 +1181,7 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL else { $english = $value['msgid']; $translation = $value['msgstr']; - $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english)); + $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $english, $group)); if (!empty($loc->lid)) { // a string exists $lid = $loc->lid; // update location field @@ -1001,8 +1206,8 @@ function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NUL } } else { // no string - 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)); + db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', '%s')", $comments, $english, $group); + $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $english, $group)); $lid = $loc->lid; db_query("INSERT INTO {locales_target} (lid, language, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation); if ($translation != '') { @@ -1114,7 +1319,7 @@ function _locale_import_parse_arithmetic($string) { } elseif ($ctok == ")") { $topop = array_pop($opstk); - while (($topop != NULL) && ($topop != "(")) { + while (isset($topop) && ($topop != "(")) { $elstk[] = $topop; $topop = array_pop($opstk); } @@ -1123,7 +1328,7 @@ function _locale_import_parse_arithmetic($string) { // If it's an operator, then pop from $oparr into $elarr until the // precedence in $oparr is less than current, then push into $oparr $topop = array_pop($opstk); - while (($topop != NULL) && ($prec[$topop] >= $prec[$ctok]) && !(($prec[$topop] == $prec[$ctok]) && $rasc[$topop] && $rasc[$ctok])) { + while (isset($topop) && ($prec[$topop] >= $prec[$ctok]) && !(($prec[$topop] == $prec[$ctok]) && !empty($rasc[$topop]) && !empty($rasc[$ctok]))) { $elstk[] = $topop; $topop = array_pop($opstk); } @@ -1150,7 +1355,7 @@ function _locale_import_parse_arithmetic($string) { $prevsize = count($elstk); for ($i = 2; $i < count($elstk); $i++) { $op = $elstk[$i]; - if ($prec[$op]) { + if (!empty($prec[$op])) { $f = ""; if ($op == ":") { $f = $elstk[$i - 2] ."):". $elstk[$i - 1] .")"; @@ -1299,6 +1504,14 @@ function _locale_import_parse_quoted($string) { return FALSE; // Unrecognized quote } } +/** + * @} End of "locale-api-import" + */ + +/** + * @defgroup locale-api-export Translation (template) export API. + * @{ + */ /** * Exports a Portable Object (Template) file for a language @@ -1306,17 +1519,19 @@ function _locale_import_parse_quoted($string) { * @param $language * Language code to generate the output for, or NULL if generating * translation template. + * @param $group + * Text group to export PO file from (eg. 'default' for interface translations) */ -function _locale_export_po($language = NULL) { +function _locale_export_po($language = NULL, $group = 'default') { global $user; $header = ''; // Get language specific strings, or all strings if (isset($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); + $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' AND s.textgroup = '%s' ORDER BY t.plid, t.plural", $language, $group); } 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"); + $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 WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $group); } // Build array out of the database results @@ -1325,7 +1540,7 @@ function _locale_export_po($language = NULL) { if ($child->source != '') { $parent[$child->lid]['comment'] = $child->location; $parent[$child->lid]['msgid'] = $child->source; - $parent[$child->lid]['translation'] = $child->translation; + $parent[$child->lid]['translation'] = isset($child->translation) ? $child->translation : ''; if ($child->plid) { $parent[$child->lid]['child'] = 1; $parent[$child->plid]['plural'] = $child->lid; @@ -1497,55 +1712,28 @@ function _locale_export_wrap($str, $len) { function _locale_export_remove_plural($entry) { return preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); } +/** + * @} End of "locale-api-export" + */ /** - * List languages in search result table + * @defgroup locale-api-seek String search functions. + * @{ */ -function _locale_string_language_list($translation) { - // Add CSS - drupal_add_css(drupal_get_path('module', 'locale') .'/locale.css', 'module', 'all', FALSE); - - $languages = language_list(); - unset($languages['en']); - $output = ''; - foreach ($languages as $langcode => $language) { - $output .= (!empty($translation[$langcode])) ? $langcode .' ' : "$langcode "; - } - - return $output; -} - -/** - * Build array out of search criteria specified in request variables - */ -function _locale_string_seek_query() { - static $query = NULL; - - if (!isset($query)) { - $query = array(); - $fields = array('string', 'language', 'searchin'); - foreach ($fields as $field) { - if (isset($_REQUEST[$field])) { - $query[$field] = $_REQUEST[$field]; - } - } - } - return $query; -} /** * Perform a string search and display results in a table */ -function _locale_string_seek() { +function _locale_translate_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.language FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid "; + if ($query = _locale_translate_seek_query()) { + $join = "SELECT s.source, s.location, s.lid, s.textgroup, 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['translation']) { case 'translated': $where = "WHERE (t.translation LIKE '%%%s%%' AND t.translation != '')"; $orderby = "ORDER BY t.translation"; @@ -1564,35 +1752,45 @@ function _locale_string_seek() { $arguments[] = $query['string']; break; } + $grouplimit = ''; + if (!empty($query['group']) && $query['group'] != 'all') { + $grouplimit = " AND s.textgroup = '%s'"; + $arguments[] = $query['group']; + } switch ($query['language']) { // Force search in source strings case "en": - $sql = $join ." WHERE s.source LIKE '%%%s%%' ORDER BY s.source"; + $sql = $join ." WHERE s.source LIKE '%%%s%%' $grouplimit ORDER BY s.source"; $arguments = array($query['string']); // $where is not used, discard its arguments + if (!empty($grouplimit)) { + $arguments[] = $query['group']; + } break; // Search in all languages case "all": - $sql = "$join $where $orderby"; + $sql = "$join $where $grouplimit $orderby"; break; // Some different language default: - $sql = "$join $where AND t.language = '%s' $orderby"; + $sql = "$join $where $grouplimit AND t.language = '%s' $orderby"; $arguments[] = $query['language']; } $result = pager_query($sql, 50, 0, NULL, $arguments); - $header = array(t('String'), t('Locales'), array('data' => t('Operations'), 'colspan' => '2')); + $groups = module_invoke_all('locale', 'groups'); + $header = array(t('Text group'), t('String'), t('Languages'), array('data' => t('Operations'), 'colspan' => '2')); $arr = array(); while ($locale = db_fetch_object($result)) { - $arr[$locale->lid]['locales'][$locale->language] = $locale->translation; + $arr[$locale->lid]['group'] = $groups[$locale->textgroup]; + $arr[$locale->lid]['languages'][$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)) .'
'. $value['location'] .''), 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')); + $rows[] = array($value['group'], array('data' => check_plain(truncate_utf8($value['source'], 150, FALSE, TRUE)) .'
'. $value['location'] .''), array('data' => _locale_translate_language_list($value['languages']), 'align' => 'center'), array('data' => l(t('edit'), "admin/build/translate/edit/$lid"), 'class' => 'nowrap'), array('data' => l(t('delete'), "admin/build/translate/delete/$lid"), 'class' => 'nowrap')); } if (count($rows)) { @@ -1609,8 +1807,47 @@ function _locale_string_seek() { return $output; } -// --------------------------------------------------------------------------------- -// List of some of the most common languages (administration only) +/** + * Build array out of search criteria specified in request variables + */ +function _locale_translate_seek_query() { + static $query = NULL; + if (!isset($query)) { + $query = array(); + $fields = array('string', 'language', 'translation', 'group'); + foreach ($fields as $field) { + if (isset($_REQUEST[$field])) { + $query[$field] = $_REQUEST[$field]; + } + } + } + return $query; +} + +/** + * List languages in search result table + */ +function _locale_translate_language_list($translation) { + // Add CSS + drupal_add_css(drupal_get_path('module', 'locale') .'/locale.css', 'module', 'all', FALSE); + + $languages = language_list(); + unset($languages['en']); + $output = ''; + foreach ($languages as $langcode => $language) { + $output .= (!empty($translation[$langcode])) ? $langcode .' ' : "$langcode "; + } + + return $output; +} +/** + * @} End of "locale-api-seek" + */ + +/** + * @defgroup locale-api-predefined List of predefined languages + * @{ + */ /** * Prepares the language code list for a select form item with only the unsupported ones @@ -1823,3 +2060,6 @@ function _locale_get_predefined_list() { "zu" => array("Zulu", "isiZulu"), ); } +/** + * @} End of "locale-api-languages-predefined" + */ diff --git a/modules/locale/locale.info b/modules/locale/locale.info index 10f58da6f35..88d6ee7bc6c 100644 --- a/modules/locale/locale.info +++ b/modules/locale/locale.info @@ -1,5 +1,5 @@ ; $Id$ name = Locale -description = Enables the translation of the user interface to languages other than English. +description = Add language handling functionality and enables the translation of the user interface to languages other than English. package = Core - optional version = VERSION diff --git a/modules/locale/locale.install b/modules/locale/locale.install index da8b26c6bed..6c26e6c7ccc 100644 --- a/modules/locale/locale.install +++ b/modules/locale/locale.install @@ -28,6 +28,7 @@ function locale_install() { db_query("CREATE TABLE {locales_source} ( lid int NOT NULL auto_increment, location varchar(255) NOT NULL default '', + textgroup varchar(255) NOT NULL default '', source blob NOT NULL, PRIMARY KEY (lid), KEY source (source(30)) @@ -64,6 +65,7 @@ function locale_install() { db_query("CREATE TABLE {locales_source} ( lid serial, location varchar(255) NOT NULL default '', + textgroup varchar(255) NOT NULL default '', source text NOT NULL, PRIMARY KEY (lid) )"); @@ -86,11 +88,14 @@ function locale_install() { } /** - * @defgroup updates-5.0-to-x.x Locale updates from 5.0 to x.x + * @defgroup updates-5.x-to-6.x Locale updates from 5.x to 6.x * @{ */ -function locale_update_2001() { +/** + * {locales_meta} table became {languages}. + */ +function locale_update_6001() { $ret = array(); switch ($GLOBALS['db_type']) { case 'mysql': @@ -142,8 +147,24 @@ function locale_update_2001() { } /** - * @} End of "defgroup updates-5.0-to-x.x" - * The next series of updates should start at 3000. + * Add multiple text group support to allow for user defined string translation. + */ +function locale_update_6002() { + $ret = array(); + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + db_add_column($ret, 'locales_source', 'textgroup', 'varchar(255)', array('default' => "''", 'not null' => TRUE)); + break; + case 'pgsql': + $ret[] = update_sql("ALTER TABLE {locales_source} ADD textgroup varchar(255) NOT NULL default ''"); + break; + } + return $ret; +} + +/** + * @} End of "defgroup updates-5.x-to-6.x" */ /** diff --git a/modules/locale/locale.module b/modules/locale/locale.module index bca48fb3cae..8a8f03f3b7d 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -3,15 +3,28 @@ /** * @file - * Enables administrators to manage the site interface languages. + * Add language handling functionality and enables the translation of the + * user interface to languages other than English. * - * When enabled, the site interface can be displayed in different - * languages. The setup of languages and translations is completely - * web based. Gettext portable object files are supported. + * When enabled, multiple languages can be set up. The site interface + * can be displayed in different languages, as well as nodes can have languages + * assigned. The setup of languages and translations is completely web based. + * Gettext portable object files are supported. */ +/** + * Language written left to right. Possible value of $language->direction. + */ +define('LANGUAGE_LTR', 0); + +/** + * Language written right to left. Possible value of $language->direction. + */ +define('LANGUAGE_RTL', 1); + + // --------------------------------------------------------------------------------- -// Hook implementations (needed on all page loads) +// Hook implementations /** * Implementation of hook_help(). @@ -24,123 +37,149 @@ function locale_help($section) { $output .= '

'. t("If an existing translation does not meet your needs, the .po files are easily edited with special editing tools. The locale module's import feature allows you to add strings from such files into your site's database. The export functionality enables you to share your translations with others, generating Portable Object files from your site strings.") .'

'; $output .= '

'. t('For more information please read the configuration and customization handbook Locale page.', array('@locale' => 'http://drupal.org/handbook/modules/locale/')) .'

'; return $output; - case 'admin/build/locale': - case 'admin/build/locale/language': - case 'admin/build/locale/language/overview': - return t("

Drupal provides support for the translation of its interface text into different languages. This page provides an overview of the installed languages. You can add a language on the add language page, or directly by importing a translation. If multiple languages are enabled, registered users will be able to set their preferred language. The site default will be used for anonymous visitors and for users without their own settings.

Drupal interface translations may be added or extended by several courses: by importing an existing translation, by translating everything from scratch, or by a combination of these approaches.

", array("@search" => url("admin/build/locale/string/search"), "@import" => url("admin/build/locale/language/import"), "@add-language" => url("admin/build/locale/language/add"))); - case 'admin/build/locale/language/add': - return '

'. t("You need to add all languages in which you would like to display the site interface. If you can't find the desired language in the quick-add dropdown, then you will need to provide the proper language code yourself. The language code may be used to negotiate with browsers and to present flags, etc., so it is important to pick a code that is standardised for the desired language. You can also add a language by importing a translation.", array("@import" => url("admin/build/locale/language/import"))) .'

'; - case 'admin/build/locale/language/import': + + case 'admin/settings/language': + case 'admin/settings/language/overview': + return t("

Drupal provides support for the translation of its interface text into different languages. This page provides an overview of the installed languages. You can add a language on the add language page, or directly by importing a translation. If multiple languages are enabled, registered users will be able to set their preferred language. The site default will be used for anonymous visitors and for users without their own settings.

Drupal interface translations may be added or extended by several courses: by importing an existing translation, by translating everything from scratch, or by a combination of these approaches.

", array("@search" => url("admin/build/translate/search"), "@import" => url("admin/build/translate/import"), "@add-language" => url("admin/settings/language/add"))); + case 'admin/settings/language/add': + return '

'. t("You need to add all languages in which you would like to display the site interface. If you can't find the desired language in the quick-add dropdown, then you will need to provide the proper language code yourself. The language code may be used to negotiate with browsers and to present flags, etc., so it is important to pick a code that is standardised for the desired language. You can also add a language by importing a translation.", array("@import" => url("admin/build/translate/import"))) .'

'; + case 'admin/settings/language/configure': + return '

'. t('The language used to display a web page is determined with a negotiation algorithm. You can choose how this algorithm should work. By default, there is no negotiation and the default language is used. You can use path prefixes (like "de" and "it" for German and Italian content) with different fallback options, so you can have web addresses like /de/contact and /it/contact. Alternatively you can use custom domains like de.example.com and it.example.com. Customize path prefixes and set domain names on the language editing pages.', array('@languages' => url('admin/settings/language'))) .'

'; + + case 'admin/build/translate': + case 'admin/build/translate/overview': + return '

'. t("This page provides an overview of interface translation on the site. Drupal groups all translatable strings in so called 'text groups'. Text groups are useful, because you can focus your translation efforts on the groups of text you care most about. For example, a translation team could choose not to fully translate the text group that includes all the long help texts on the administration pages. Similarly, text groups are useful to ensure that certain aspects of the site are always fully translated.") . '

'; + case 'admin/build/translate/import': return '

'. t("This page allows you to import a translation provided in the gettext Portable Object (.po) format. The easiest way to get your site translated is to obtain an existing Drupal translation and to import it. You can find existing translations on the Drupal translation page. Note that importing a translation file might take a while.", array('@url' => 'http://drupal.org/project/translations')) .'

'; - case 'admin/build/locale/language/export': + case 'admin/build/translate/export': return '

'. t("This page allows you to export Drupal strings. The first option is to export a translation so it can be shared. The second option generates a translation template, which contains all Drupal strings, but without their translations. You can use this template to start a new translation using various software packages designed for this task.") .'

'; - case 'admin/build/locale/string': - case 'admin/build/locale/string/search': - return '

'. t("It is often convenient to get the strings from your setup on the export page, and use a desktop Gettext translation editor to edit the translations. On this page you can search in the translated and untranslated strings, and the default English texts provided by Drupal.", array("@export" => url("admin/build/locale/language/export"))) .'

'; - case 'admin/build/locale/language/configure': - return '

'. t('The language used to display a web page is determined with a negotiation algorithm. You can choose how this algorithm should work. By default, there is no negotiation and the default language is used. You can use path prefixes (like "de" and "it" for German and Italian content) with different fallback options, so you can have web addresses like /de/contact and /it/contact. Alternatively you can use custom domains like de.example.com and it.example.com. Customize path prefixes and set domain names on the language editing pages.', array('@languages' => url('admin/build/locale/language/overview'))) .'

'; + case 'admin/build/translate/search': + return '

'. t("It is often convenient to get the strings from your setup on the export page, and use a desktop Gettext translation editor to edit the translations. On this page you can search in the translated and untranslated strings, and the default English texts provided by Drupal.", array("@export" => url("admin/build/translate/export"))) .'

'; } } /** * Implementation of hook_menu(). + * + * Locale module only provides administrative menu items, so all + * menu items are invoked through locale_inc_callback(). */ function locale_menu() { - // Main admin menu item - $items['admin/build/locale'] = array( + // Manage languages + $items['admin/settings/language'] = array( 'title' => 'Languages', - 'description' => 'Configure languages and user interface translation.', - 'page callback' => 'locale_admin_manage', - 'access arguments' => array('administer locales'), + 'description' => 'Configure languages for content and the user interface.', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('drupal_get_form', 'locale_languages_overview_form'), + 'access arguments' => array('administer languages'), ); - - // Top level tabs - $items['admin/build/locale/language'] = array( - 'title' => 'Manage languages', - 'weight' => -10, - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items['admin/build/locale/string'] = array( - 'title' => 'Manage interface strings', - 'page callback' => 'locale_string_search', - 'weight' => 10, - 'type' => MENU_LOCAL_TASK, - 'parent' => 'admin/build/locale', - ); - - // Manage languages subtabs - $items['admin/build/locale/language/overview'] = array( + $items['admin/settings/language/overview'] = array( 'title' => 'List', 'weight' => 0, 'type' => MENU_DEFAULT_LOCAL_TASK, ); - $items['admin/build/locale/language/add'] = array( + $items['admin/settings/language/add'] = array( 'title' => 'Add language', - 'page callback' => 'locale_admin_manage_add', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('locale_languages_add_screen'), // two forms concatenated 'weight' => 5, 'type' => MENU_LOCAL_TASK, ); - $items['admin/build/locale/language/configure'] = array( + $items['admin/settings/language/configure'] = array( 'title' => 'Configure', - 'page callback' => 'locale_admin_manage_configure', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('drupal_get_form', 'locale_languages_configure_form'), 'weight' => 10, 'type' => MENU_LOCAL_TASK, ); - $items['admin/build/locale/language/edit/%'] = array( + $items['admin/settings/language/edit/%'] = array( 'title' => 'Edit language', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('locale_admin_manage_edit', 5), + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('drupal_get_form', 'locale_languages_edit_form', 4), + 'type' => MENU_CALLBACK, + ); + $items['admin/settings/language/delete/%'] = array( + 'title' => 'Confirm', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('drupal_get_form', 'locale_languages_delete_form', 4), 'type' => MENU_CALLBACK, ); - // Manage interface translations subtabs - $items['admin/build/locale/string/search'] = array( - 'title' => 'Search', + // Translation functionality + $items['admin/build/translate'] = array( + 'title' => 'Translate interface', + 'description' => 'Translate the built in interface as well as menu items and taxonomies.', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('locale_translate_overview_screen'), // not a form, just a table + 'access arguments' => array('translate interface'), + ); + $items['admin/build/translate/overview'] = array( + 'title' => 'Overview', 'weight' => 0, 'type' => MENU_DEFAULT_LOCAL_TASK, ); - $items['admin/build/locale/string/import'] = array( - 'title' => 'Import', - 'page callback' => 'locale_admin_import', + $items['admin/build/translate/search'] = array( + 'title' => 'Search', 'weight' => 10, 'type' => MENU_LOCAL_TASK, + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('locale_translate_seek_screen'), // search results and form concatenated ); - $items['admin/build/locale/string/export'] = array( - 'title' => 'Export', - 'page callback' => 'locale_admin_export', + $items['admin/build/translate/import'] = array( + 'title' => 'Import', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('drupal_get_form', 'locale_translate_import_form'), 'weight' => 20, 'type' => MENU_LOCAL_TASK, ); - - // Language related callbacks - $items['admin/build/locale/language/delete'] = array( - 'title' => 'Confirm', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('locale_admin_manage_delete_form'), - 'type' => MENU_CALLBACK, + $items['admin/build/translate/export'] = array( + 'title' => 'Export', + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('locale_translate_export_screen'), // possibly multiple forms concatenated + 'weight' => 30, + 'type' => MENU_LOCAL_TASK, ); - // String related callbacks - $items['admin/build/locale/string/edit/%'] = array( + $items['admin/build/translate/edit/%'] = array( 'title' => 'Edit string', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('locale_admin_string_edit', 5), + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('drupal_get_form', 'locale_translate_edit_form', 4), 'type' => MENU_CALLBACK, ); - $items['admin/build/locale/string/delete/%'] = array( + $items['admin/build/translate/delete/%'] = array( 'title' => 'Delete string', - 'page callback' => 'locale_admin_string_delete', - 'page arguments' => array(5), + 'page callback' => 'locale_inc_callback', + 'page arguments' => array('locale_translate_delete', 4), // directly deletes, no confirmation 'type' => MENU_CALLBACK, ); return $items; } +/** + * Wrapper function to be able to set callbacks in locale.inc + */ +function locale_inc_callback() { + $args = func_get_args(); + $function = array_shift($args); + include_once './includes/locale.inc'; + return call_user_func_array($function, $args); +} + /** * Implementation of hook_perm(). */ function locale_perm() { - return array('administer locales'); + return array('administer languages', 'translate interface'); +} + +/** + * Implementation of hook_locale(). + */ +function locale_locale($op = 'groups') { + switch ($op) { + case 'groups': + return array('default' => t('Built-in interface')); + } } /** @@ -198,7 +237,7 @@ function locale_form_alter(&$form, $form_id) { '#title' => t('Multilingual support'), '#default_value' => variable_get('language_'. $form['#node_type']->type, 0), '#options' => array(t('Disabled'), t('Enabled')), - '#description' => t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the enabled languages. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/build/locale'))), + '#description' => t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the enabled languages. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/settings/language'))), ); } break; @@ -206,7 +245,7 @@ function locale_form_alter(&$form, $form_id) { // Language field for nodes default: if ($form['#id'] == 'node-form') { - if (variable_get('language_' . $form['#node']->type, 0)) { + if (isset($form['#node']->type) && variable_get('language_' . $form['#node']->type, 0)) { $form['language'] = array( '#type' => 'select', '#title' => t('Language'), @@ -226,9 +265,19 @@ function locale_form_alter(&$form, $form_id) { } } +/** + * Implementation of hook_theme() + */ +function locale_theme() { + return array( + 'locale_languages_overview_form' => array( + 'arguments' => array('form' => array()), + ), + ); +} // --------------------------------------------------------------------------------- -// Locale core functionality (needed on all page loads) +// Locale core functionality /** * Provides interface translation services. @@ -241,11 +290,14 @@ function locale($string) { // Store database cached translations in a static var. if (!isset($locale_t)) { + $locale_t = array(); if (!($cache = cache_get('locale:'. $language->language, 'cache'))) { locale_refresh_cache(); $cache = cache_get('locale:'. $language->language, 'cache'); } - $locale_t = $cache->data; + if ($cache) { + $locale_t = $cache->data; + } } // We have the translation cached (if it is TRUE, then there is no @@ -256,7 +308,7 @@ function locale($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'", $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, $language->language); // Translation found if ($trans = db_fetch_object($result)) { if (!empty($trans->translation)) { @@ -267,7 +319,7 @@ function locale($string) { // Either we have no such source string, or no translation else { - $result = db_query("SELECT lid, source FROM {locales_source} WHERE source = '%s'", $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) { @@ -276,9 +328,9 @@ function locale($string) { } // We have no such source string else { - db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", request_uri(), $string); + db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', 'default')", request_uri(), $string); if ($language) { - $lid = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $string)); + $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); } } @@ -300,7 +352,7 @@ function locale_refresh_cache() { $languages = $languages['1']; foreach ($languages as $language) { - $result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.language = '%s' AND LENGTH(s.source) < 75", $language->language); + $result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.language = '%s' AND s.textgroup = 'default' AND LENGTH(s.source) < 75", $language->language); $t = array(); while ($data = db_fetch_object($result)) { $t[$data->source] = (empty($data->translation) ? TRUE : $data->translation); @@ -370,140 +422,3 @@ function locale_language_list($field = 'name', $all = FALSE) { } return $list; } - -// --------------------------------------------------------------------------------- -// Language management functionality (administration only) - -/** - * Page handler for the language management screen. - */ -function locale_admin_manage() { - include_once './includes/locale.inc'; - return drupal_get_form('_locale_admin_manage_screen'); -} - -/** - * User interface for the language deletion confirmation screen. - */ -function locale_admin_manage_delete_form($langcode) { - include_once './includes/locale.inc'; - - // Do not allow deletion of English locale. - if ($langcode == 'en') { - drupal_set_message(t('The English locale cannot be deleted.')); - drupal_goto('admin/build/locale/language/overview'); - } - - $default = language_default(); - if ($default->language == $langcode) { - drupal_set_message(t('The default language cannot be deleted.')); - drupal_goto('admin/build/locale/language/overview'); - } - - // For other locales, warn user that data loss is ahead. - $languages = language_list(); - - if (!isset($languages[$langcode])) { - drupal_not_found(); - } - else { - $form['langcode'] = array('#type' => 'value', '#value' => $langcode); - return confirm_form($form, t('Are you sure you want to delete the language %name?', array('%name' => t($languages[$langcode]->name))), 'admin/build/locale/language/overview', t('Deleting a language will remove all data associated with it. This action cannot be undone.'), t('Delete'), t('Cancel')); - } -} - -/** - * Process language deletion submissions. - */ -function locale_admin_manage_delete_form_submit($form_id, $form_values) { - $languages = language_list(); - if (isset($languages[$form_values['langcode']])) { - db_query("DELETE FROM {languages} WHERE language = '%s'", $form_values['langcode']); - db_query("DELETE FROM {locales_target} WHERE language = '%s'", $form_values['langcode']); - $variables = array('%locale' => $languages[$form_values['langcode']]->name); - drupal_set_message(t('The language %locale has been removed.', $variables)); - watchdog('locale', 'The language %locale has been removed.', $variables); - } - - // Changing the locale settings impacts the interface: - cache_clear_all('*', 'cache_page', TRUE); - - return 'admin/build/locale/language/overview'; -} - -/** - * Page handler for the language addition screen - */ -function locale_admin_manage_add() { - include_once './includes/locale.inc'; - return _locale_admin_manage_add_screen(); -} - -function locale_admin_manage_edit($langcode) { - include_once './includes/locale.inc'; - return _locale_admin_manage_edit_screen($langcode); -} - -function locale_admin_manage_configure() { - include_once './includes/locale.inc'; - return drupal_get_form("locale_configure_language_form"); -} - -// --------------------------------------------------------------------------------- -// Gettext Portable Object import functionality (administration only) - -/** - * Page handler for the translation import screen - */ -function locale_admin_import() { - include_once './includes/locale.inc'; - return drupal_get_form('_locale_admin_import'); -} - -// --------------------------------------------------------------------------------- -// Gettext Portable Object export functionality (administration only) - -/** - * Page handler for the translation export screen - */ -function locale_admin_export() { - include_once './includes/locale.inc'; - return _locale_admin_export_screen(); -} - -// --------------------------------------------------------------------------------- -// String search and editing functionality (administration only) - -/** - * Page handler for the string search. - */ -function locale_string_search() { - include_once './includes/locale.inc'; - $output = _locale_string_seek(); - $output .= drupal_get_form('_locale_string_seek_form'); - return $output; -} - -/** - * Display the string edit form. - */ -function locale_admin_string_edit($lid) { - include_once './includes/locale.inc'; - return _locale_string_edit($lid); -} - -/** - * Process the string edit form. - */ -function locale_admin_string_edit_submit($form_id, $form_values) { - include_once './includes/locale.inc'; - return _locale_string_edit_submit($form_id, $form_values); -} - -/** - * Delete a string. - */ -function locale_admin_string_delete($lid) { - include_once './includes/locale.inc'; - _locale_string_delete($lid); -}