Index: install.php =================================================================== RCS file: /cvs/drupal/drupal/install.php,v retrieving revision 1.83 diff -u -p -r1.83 install.php --- install.php 22 Oct 2007 15:48:26 -0000 1.83 +++ install.php 29 Oct 2007 02:07:12 -0000 @@ -559,7 +559,7 @@ function install_select_locale($profilen } function install_select_locale_form(&$form_state, $locales) { - include_once './includes/locale.inc'; + include_once 'includes/locale.inc'; $languages = _locale_get_predefined_list(); foreach ($locales as $locale) { // Try to use verbose locale name Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.708 diff -u -p -r1.708 common.inc --- includes/common.inc 25 Oct 2007 15:38:24 -0000 1.708 +++ includes/common.inc 29 Oct 2007 02:07:12 -0000 @@ -2822,10 +2822,6 @@ function drupal_common_themes() { 'pager_link' => array( 'arguments' => array('text' => NULL, 'page_new' => NULL, 'element' => NULL, 'parameters' => array(), 'attributes' => array()), ), - // from locale.inc - 'locale_admin_manage_screen' => array( - 'arguments' => array('form' => NULL), - ), // from menu.inc 'menu_item_link' => array( 'arguments' => array('item' => NULL), Index: includes/install.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/install.inc,v retrieving revision 1.48 diff -u -p -r1.48 install.inc --- includes/install.inc 7 Sep 2007 10:48:24 -0000 1.48 +++ includes/install.inc 29 Oct 2007 02:07:12 -0000 @@ -606,7 +606,7 @@ function st($string, $args = array()) { $locale_strings = array(); $filename = './profiles/'. $profile .'/translations/'. $install_locale .'.po'; if (file_exists($filename)) { - require_once './includes/locale.inc'; + include_once 'includes/locale.inc'; $file = (object) array('filepath' => $filename); _locale_import_read_po('mem-store', $file); $locale_strings = _locale_import_one_string('mem-report'); Index: includes/locale.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/locale.inc,v retrieving revision 1.156 diff -u -p -r1.156 locale.inc --- includes/locale.inc 15 Oct 2007 19:51:06 -0000 1.156 +++ includes/locale.inc 29 Oct 2007 02:07:12 -0000 @@ -6,2545 +6,715 @@ * Administration functions for locale.module. */ -define('LOCALE_JS_STRING', '(?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+'); /** - * Translation import mode overwriting all existing translations - * if new translated version available. + * @defgroup locale-api-predefined List of predefined languages + * @{ */ -define('LOCALE_IMPORT_OVERWRITE', 0); + +/** + * Some of the common languages with their English and native names + * + * Based on ISO 639 and http://people.w3.org/rishida/names/languages.html + */ +function _locale_get_predefined_list() { + return array( + "aa" => array("Afar"), + "ab" => array("Abkhazian", "аҧсуа бызшәа"), + "ae" => array("Avestan"), + "af" => array("Afrikaans"), + "ak" => array("Akan"), + "am" => array("Amharic", "አማርኛ"), + "ar" => array("Arabic", /* Left-to-right marker "‭" */ "العربية", LANGUAGE_RTL), + "as" => array("Assamese"), + "av" => array("Avar"), + "ay" => array("Aymara"), + "az" => array("Azerbaijani", "azərbaycan"), + "ba" => array("Bashkir"), + "be" => array("Belarusian", "Беларуская"), + "bg" => array("Bulgarian", "Български"), + "bh" => array("Bihari"), + "bi" => array("Bislama"), + "bm" => array("Bambara", "Bamanankan"), + "bn" => array("Bengali"), + "bo" => array("Tibetan"), + "br" => array("Breton"), + "bs" => array("Bosnian", "Bosanski"), + "ca" => array("Catalan", "Català"), + "ce" => array("Chechen"), + "ch" => array("Chamorro"), + "co" => array("Corsican"), + "cr" => array("Cree"), + "cs" => array("Czech", "Čeština"), + "cu" => array("Old Slavonic"), + "cv" => array("Chuvash"), + "cy" => array("Welsh", "Cymraeg"), + "da" => array("Danish", "Dansk"), + "de" => array("German", "Deutsch"), + "dv" => array("Maldivian"), + "dz" => array("Bhutani"), + "ee" => array("Ewe", "Ɛʋɛ"), + "el" => array("Greek", "Ελληνικά"), + "en" => array("English"), + "eo" => array("Esperanto"), + "es" => array("Spanish", "Español"), + "et" => array("Estonian", "Eesti"), + "eu" => array("Basque", "Euskera"), + "fa" => array("Persian", /* Left-to-right marker "‭" */ "فارسی", LANGUAGE_RTL), + "ff" => array("Fulah", "Fulfulde"), + "fi" => array("Finnish", "Suomi"), + "fj" => array("Fiji"), + "fo" => array("Faeroese"), + "fr" => array("French", "Français"), + "fy" => array("Frisian", "Frysk"), + "ga" => array("Irish", "Gaeilge"), + "gd" => array("Scots Gaelic"), + "gl" => array("Galician", "Galego"), + "gn" => array("Guarani"), + "gu" => array("Gujarati"), + "gv" => array("Manx"), + "ha" => array("Hausa"), + "he" => array("Hebrew", /* Left-to-right marker "‭" */ "עברית", LANGUAGE_RTL), + "hi" => array("Hindi", "हिन्दी"), + "ho" => array("Hiri Motu"), + "hr" => array("Croatian", "Hrvatski"), + "hu" => array("Hungarian", "Magyar"), + "hy" => array("Armenian", "Հայերեն"), + "hz" => array("Herero"), + "ia" => array("Interlingua"), + "id" => array("Indonesian", "Bahasa Indonesia"), + "ie" => array("Interlingue"), + "ig" => array("Igbo"), + "ik" => array("Inupiak"), + "is" => array("Icelandic", "Íslenska"), + "it" => array("Italian", "Italiano"), + "iu" => array("Inuktitut"), + "ja" => array("Japanese", "日本語"), + "jv" => array("Javanese"), + "ka" => array("Georgian"), + "kg" => array("Kongo"), + "ki" => array("Kikuyu"), + "kj" => array("Kwanyama"), + "kk" => array("Kazakh", "Қазақ"), + "kl" => array("Greenlandic"), + "km" => array("Cambodian"), + "kn" => array("Kannada", "ಕನ್ನಡ"), + "ko" => array("Korean", "한국어"), + "kr" => array("Kanuri"), + "ks" => array("Kashmiri"), + "ku" => array("Kurdish", "Kurdî"), + "kv" => array("Komi"), + "kw" => array("Cornish"), + "ky" => array("Kirghiz", "Кыргыз"), + "la" => array("Latin", "Latina"), + "lb" => array("Luxembourgish"), + "lg" => array("Luganda"), + "ln" => array("Lingala"), + "lo" => array("Laothian"), + "lt" => array("Lithuanian", "Lietuviškai"), + "lv" => array("Latvian", "Latviešu"), + "mg" => array("Malagasy"), + "mh" => array("Marshallese"), + "mi" => array("Maori"), + "mk" => array("Macedonian", "Македонски"), + "ml" => array("Malayalam", "മലയാളം"), + "mn" => array("Mongolian"), + "mo" => array("Moldavian"), + "mr" => array("Marathi"), + "ms" => array("Malay", "Bahasa Melayu"), + "mt" => array("Maltese", "Malti"), + "my" => array("Burmese"), + "na" => array("Nauru"), + "nd" => array("North Ndebele"), + "ne" => array("Nepali"), + "ng" => array("Ndonga"), + "nl" => array("Dutch", "Nederlands"), + "nb" => array("Norwegian Bokmål", "Bokmål"), + "nn" => array("Norwegian Nynorsk", "Nynorsk"), + "nr" => array("South Ndebele"), + "nv" => array("Navajo"), + "ny" => array("Chichewa"), + "oc" => array("Occitan"), + "om" => array("Oromo"), + "or" => array("Oriya"), + "os" => array("Ossetian"), + "pa" => array("Punjabi"), + "pi" => array("Pali"), + "pl" => array("Polish", "Polski"), + "ps" => array("Pashto", /* Left-to-right marker "‭" */ "پښتو", LANGUAGE_RTL), + "pt" => array("Portuguese, Portugal", "Português"), + "pt-br" => array("Portuguese, Brazil", "Português"), + "qu" => array("Quechua"), + "rm" => array("Rhaeto-Romance"), + "rn" => array("Kirundi"), + "ro" => array("Romanian", "Română"), + "ru" => array("Russian", "Русский"), + "rw" => array("Kinyarwanda"), + "sa" => array("Sanskrit"), + "sc" => array("Sardinian"), + "sd" => array("Sindhi"), + "se" => array("Northern Sami"), + "sg" => array("Sango"), + "sh" => array("Serbo-Croatian"), + "si" => array("Singhalese"), + "sk" => array("Slovak", "Slovenčina"), + "sl" => array("Slovenian", "Slovenščina"), + "sm" => array("Samoan"), + "sn" => array("Shona"), + "so" => array("Somali"), + "sq" => array("Albanian", "Shqip"), + "sr" => array("Serbian", "Српски"), + "ss" => array("Siswati"), + "st" => array("Sesotho"), + "su" => array("Sudanese"), + "sv" => array("Swedish", "Svenska"), + "sw" => array("Swahili", "Kiswahili"), + "ta" => array("Tamil", "தமிழ்"), + "te" => array("Telugu", "తెలుగు"), + "tg" => array("Tajik"), + "th" => array("Thai", "ภาษาไทย"), + "ti" => array("Tigrinya"), + "tk" => array("Turkmen"), + "tl" => array("Tagalog"), + "tn" => array("Setswana"), + "to" => array("Tonga"), + "tr" => array("Turkish", "Türkçe"), + "ts" => array("Tsonga"), + "tt" => array("Tatar", "Tatarça"), + "tw" => array("Twi"), + "ty" => array("Tahitian"), + "ug" => array("Uighur"), + "uk" => array("Ukrainian", "Українська"), + "ur" => array("Urdu", /* Left-to-right marker "‭" */ "اردو", LANGUAGE_RTL), + "uz" => array("Uzbek", "o'zbek"), + "ve" => array("Venda"), + "vi" => array("Vietnamese", "Tiếng Việt"), + "wo" => array("Wolof"), + "xh" => array("Xhosa", "isiXhosa"), + "yi" => array("Yiddish"), + "yo" => array("Yoruba", "Yorùbá"), + "za" => array("Zhuang"), + "zh-hans" => array("Chinese, Simplified", "简体中文"), + "zh-hant" => array("Chinese, Traditional", "繁體中文"), + "zu" => array("Zulu", "isiZulu"), + ); +} /** - * Translation import mode keeping existing translations and only - * inserting new strings. + * @} End of "locale-api-languages-predefined" */ -define('LOCALE_IMPORT_KEEP', 1); + /** - * @defgroup locale-language-overview Language overview functionality + * @defgroup locale-api-add Language addition API. * @{ */ /** - * User interface for the language overview screen. + * 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 $enabled + * Optionally TRUE to enable the language when created or FALSE to disable. + * @param $default + * Optionally set this language to be the default. */ -function locale_languages_overview_form() { - $languages = language_list('language', TRUE); - - $options = array(); - $form['weight'] = array('#tree' => TRUE); - foreach ($languages as $langcode => $language) { - - $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 == LANGUAGE_RTL ? 'Right to left' : 'Left to right')); +function locale_add_language($langcode, $name = NULL, $native = NULL, $direction = LANGUAGE_LTR, $domain = '', $prefix = '', $enabled = TRUE, $default = FALSE) { + // Default prefix on language code. + if (empty($prefix)) { + $prefix = $langcode; } - $form['enabled'] = array('#type' => 'checkboxes', - '#options' => $options, - '#default_value' => $enabled, - ); - $form['site_default'] = array('#type' => 'radios', - '#options' => $options, - '#default_value' => language_default('language'), - ); - $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); - $form['#theme'] = 'locale_languages_overview_form'; - - return $form; -} -/** - * Theme the language overview 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)) { - // Disable checkbox for the default language, because it cannot be disabled. - if ($key == $default->language) { - $form['enabled'][$key]['#attributes']['disabled'] = TRUE; - } - $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) : '') - ); - } + // If name was not set, we add a predefined language. + if (!isset($name)) { + $predefined = _locale_get_predefined_list(); + $name = $predefined[$langcode][0]; + $native = isset($predefined[$langcode][1]) ? $predefined[$langcode][1] : $predefined[$langcode][0]; + $direction = isset($predefined[$langcode][2]) ? $predefined[$langcode][2] : LANGUAGE_LTR; } - $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); - return $output; -} + db_query("INSERT INTO {languages} (language, name, native, direction, domain, prefix, enabled) VALUES ('%s', '%s', '%s', %d, '%s', '%s', %d)", $langcode, $name, $native, $direction, $domain, $prefix, $enabled); -/** - * Process language overview form submissions, updating existing languages. - */ -function locale_languages_overview_form_submit($form, &$form_state) { - $languages = language_list(); - $default = language_default(); - $enabled_count = 0; - foreach ($languages as $langcode => $language) { - if ($form_state['values']['site_default'] == $langcode || $default->language == $langcode) { - // Automatically enable the default language and the language - // which was default previously (because we will not get the - // value from that disabled checkox). - $form_state['values']['enabled'][$langcode] = 1; - } - if ($form_state['values']['enabled'][$langcode]) { - $enabled_count++; - $language->enabled = 1; - } - else { - $language->enabled = 0; - } - $language->weight = $form_state['values']['weight'][$langcode]; - db_query("UPDATE {languages} SET enabled = %d, weight = %d WHERE language = '%s'", $language->enabled, $language->weight, $langcode); - $languages[$langcode] = $language; + // Only set it as default if enabled. + if ($enabled && $default) { + variable_set('language_default', (object) array('language' => $langcode, 'name' => $name, 'native' => $native, 'direction' => $direction, 'enabled' => (int) $enabled, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => $prefix, 'weight' => 0)); } - drupal_set_message(t('Configuration saved.')); - variable_set('language_default', $languages[$form_state['values']['site_default']]); - variable_set('language_count', $enabled_count); - // Changing the language settings impacts the interface. - cache_clear_all('*', 'cache_page', TRUE); + if ($enabled) { + // Increment enabled language count if we are adding an enabled language. + variable_set('language_count', variable_get('language_count', 1) + 1); + } - $form_state['redirect'] = 'admin/settings/language'; - return; + watchdog('locale', 'The %language language (%code) has been created.', array('%language' => $name, '%code' => $langcode)); } /** - * @} End of "locale-language-overview" + * @} End of "locale-api-add" */ /** - * @defgroup locale-language-add-edit Language addition and editing functionality + * @defgroup locale-autoimport Automatic interface translation import * @{ */ /** - * 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_languages_predefined_form() { - $predefined = _locale_prepare_predefined_list(); - $form = array(); - $form['language list'] = array('#type' => 'fieldset', - '#title' => t('Predefined language'), - '#collapsible' => TRUE, - ); - $form['language list']['langcode'] = array('#type' => 'select', - '#title' => t('Language name'), - '#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_languages_custom_form() { - $form = array(); - $form['custom language'] = array('#type' => 'fieldset', - '#title' => t('Custom language'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - ); - _locale_languages_common_controls($form['custom language']); - $form['custom language']['submit'] = array( - '#type' => 'submit', - '#value' => t('Add custom language') - ); - // Reuse the validation and submit functions of the predefined language setup form. - $form['#submit'][] = 'locale_languages_predefined_form_submit'; - $form['#validate'][] = 'locale_languages_predefined_form_validate'; - return $form; -} - -/** - * Editing screen for a particular language. + * Prepare a batch to import translations for all enabled + * modules in a given language. * * @param $langcode - * Language code of the language to edit. - */ -function locale_languages_edit_form(&$form_state, $langcode) { - if ($language = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $langcode))) { - $form = array(); - _locale_languages_common_controls($form, $language); - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Save language') - ); - $form['#submit'][] = 'locale_languages_edit_form_submit'; - $form['#validate'][] = 'locale_languages_edit_form_validate'; - return $form; - } - else { - drupal_not_found(); - } -} - -/** - * 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_languages_common_controls(&$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 RFC 4646 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['name'] = 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['native'] = 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(LANGUAGE_LTR => t('Left to right'), LANGUAGE_RTL => t('Right to left')) - ); - return $form; -} - -/** - * Validate the language addition form. - */ -function locale_languages_predefined_form_validate($form, &$form_state) { - $langcode = $form_state['values']['langcode']; - - if ($duplicate = db_result(db_query("SELECT COUNT(*) FROM {languages} WHERE language = '%s'", $langcode)) != 0) { - form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_state['values']['name'], '%code' => $langcode))); - } - - if (!isset($form_state['values']['name'])) { - // Predefined language selection. - $predefined = _locale_get_predefined_list(); - 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_languages_edit_form_validate($form, $form_state); - } -} - -/** - * Process the language addition form submission. + * Language code to import translations for. + * @param $finished + * Optional finished callback for the batch. + * @return + * A batch structure or FALSE if no files found. */ -function locale_languages_predefined_form_submit($form, &$form_state) { - $langcode = $form_state['values']['langcode']; - if (isset($form_state['values']['name'])) { - // Custom language form. - locale_add_language($langcode, $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['direction'], $form_state['values']['domain'], $form_state['values']['prefix']); - 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($form_state['values']['name']), '@locale-help' => url('admin/help/locale')))); - } - else { - // Predefined language selection. - $predefined = _locale_get_predefined_list(); - locale_add_language($langcode); - 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($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale')))); - } - - // See if we have language files to import for the newly added - // language, collect and import them. - if ($batch = locale_batch_by_language($langcode, '_locale_batch_language_finished')) { - batch_set($batch); +function locale_batch_by_language($langcode, $finished = '_locale_batch_installer_finished') { + // Collect all files to import for all enabled modules and themes. + $files = array(); + $result = db_query("SELECT name, filename FROM {system} WHERE status = 1"); + while ($component = db_fetch_object($result)) { + // Collect all files for all components, names as $langcode.po or + // with names ending with $langcode.po. This allows for filenames + // like node-module.de.po to let translators use small files and + // be able to import in smaller chunks. + $files = array_merge($files, file_scan_directory(dirname($component->filename) .'/translations', '(^|\.)'. $langcode .'\.po$', array('.', '..', 'CVS'), 0, FALSE)); } - $form_state['redirect'] = 'admin/settings/language'; - return; -} - -/** - * Validate the language editing form. Reused for custom language addition too. - */ -function locale_languages_edit_form_validate($form, &$form_state) { - if (!empty($form_state['values']['domain']) && !empty($form_state['values']['prefix'])) { - form_set_error('prefix', t('Domain and path prefix values should not be set at the same time.')); - } - if (!empty($form_state['values']['domain']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE domain = '%s' AND language != '%s'", $form_state['values']['domain'], $form_state['values']['langcode']))) { - form_set_error('domain', t('The domain (%domain) is already tied to a language (%language).', array('%domain' => $form_state['values']['domain'], '%language' => $duplicate->language))); - } - if (empty($form_state['values']['prefix']) && language_default('language') != $form_state['values']['langcode'] && empty($form_state['values']['domain'])) { - form_set_error('prefix', t('Only the default language can have both the domain and prefix empty.')); - } - if (!empty($form_state['values']['prefix']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE prefix = '%s' AND language != '%s'", $form_state['values']['prefix'], $form_state['values']['langcode']))) { - form_set_error('prefix', t('The prefix (%prefix) is already tied to a language (%language).', array('%prefix' => $form_state['values']['prefix'], '%language' => $duplicate->language))); - } + return _locale_batch_build($files, $finished); } /** - * Process the language editing form submission. + * Build a locale batch from an array of files. + * + * @param $files + * Array of files to import + * @param $finished + * Optional finished callback for the batch. + * @return + * A batch structure */ -function locale_languages_edit_form_submit($form, &$form_state) { - db_query("UPDATE {languages} SET name = '%s', native = '%s', domain = '%s', prefix = '%s', direction = %d WHERE language = '%s'", $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['domain'], $form_state['values']['prefix'], $form_state['values']['direction'], $form_state['values']['langcode']); - $default = language_default(); - if ($default->language == $form_state['values']['langcode']) { - $properties = array('name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight'); - foreach ($properties as $keyname) { - if (isset($form_state['values'][$keyname])) { - $default->$keyname = $form_state['values'][$keyname]; +function _locale_batch_build($files, $finished = NULL) { + $t = get_t(); + if (count($files)) { + $operations = array(); + foreach ($files as $file) { + // We call _locale_batch_import for every batch operation. + $operations[] = array('_locale_batch_import', array($file->filename)); } + $batch = array( + 'operations' => $operations, + 'title' => $t('Importing interface translations'), + 'init_message' => $t('Starting import'), + 'error_message' => $t('Error importing interface translations'), + 'file' => './includes/locale.inc', + ); + if (isset($finished)) { + $batch['finished'] = $finished; } - } - variable_set('language_default', $default); + return $batch; } - $form_state['redirect'] = 'admin/settings/language'; - return; + return FALSE; } + /** - * @} End of "locale-language-add-edit" + * @} End of "locale-autoimport" */ /** - * @defgroup locale-language-delete Language deletion functionality + * @defgroup locale-api-import Translation import API. * @{ */ /** - * User interface for the language deletion confirmation screen. + * Parses Gettext Portable Object file into an array + * + * @param $op + * Storage operation type: db-store or mem-store + * @param $file + * Drupal file object corresponding to the PO file to import + * @param $mode + * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE + * @param $lang + * Language code + * @param $group + * Text group to import PO file into (eg. 'default' for interface translations) */ -function locale_languages_delete_form(&$form_state, $langcode) { +function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL, $group = NULL) { - // 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'); + $fd = fopen($file->filepath, "rb"); // File will get closed by PHP on return + if (!$fd) { + _locale_import_message('The translation import failed, because the file %filename could not be read.', $file); + return FALSE; } - if (language_default('language') == $langcode) { - drupal_set_message(t('The default language cannot be deleted.')); - drupal_goto('admin/settings/language'); - } + $context = "COMMENT"; // Parser context: COMMENT, MSGID, MSGID_PLURAL, MSGSTR and MSGSTR_ARR + $current = array(); // Current entry being read + $plural = 0; // Current plural form + $lineno = 0; // Current line - // For other languages, warn user that data loss is ahead. - $languages = language_list(); + while (!feof($fd)) { + $line = fgets($fd, 10*1024); // A line should not be this long + $lineno++; + $line = trim(strtr($line, array("\\\n" => ""))); - 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')); + if (!strncmp("#", $line, 1)) { // A comment + if ($context == "COMMENT") { // Already in comment context: add + $current["#"][] = substr($line, 1); + } + elseif (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { // End current entry, start a new one + _locale_import_one_string($op, $current, $mode, $lang, $file); + $current = array(); + $current["#"][] = substr($line, 1); + $context = "COMMENT"; + } + else { // Parse error + _locale_import_message('The translation file %filename contains an error: "msgstr" was expected but not found on line %line.', $file, $lineno); + return FALSE; + } + } + elseif (!strncmp("msgid_plural", $line, 12)) { + if ($context != "MSGID") { // Must be plural form for current entry + _locale_import_message('The translation file %filename contains an error: "msgid_plural" was expected but not found on line %line.', $file, $lineno); + return FALSE; + } + $line = trim(substr($line, 12)); + $quoted = _locale_import_parse_quoted($line); + if ($quoted === FALSE) { + _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); + return FALSE; + } + $current["msgid"] = $current["msgid"] ."\0". $quoted; + $context = "MSGID_PLURAL"; + } + elseif (!strncmp("msgid", $line, 5)) { + if ($context == "MSGSTR") { // End current entry, start a new one + _locale_import_one_string($op, $current, $mode, $lang, $file); + $current = array(); + } + elseif ($context == "MSGID") { // Already in this context? Parse error + _locale_import_message('The translation file %filename contains an error: "msgid" is unexpected on line %line.', $file, $lineno); + return FALSE; + } + $line = trim(substr($line, 5)); + $quoted = _locale_import_parse_quoted($line); + if ($quoted === FALSE) { + _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); + return FALSE; + } + $current["msgid"] = $quoted; + $context = "MSGID"; + } + elseif (!strncmp("msgstr[", $line, 7)) { + if (($context != "MSGID") && ($context != "MSGID_PLURAL") && ($context != "MSGSTR_ARR")) { // Must come after msgid, msgid_plural, or msgstr[] + _locale_import_message('The translation file %filename contains an error: "msgstr[]" is unexpected on line %line.', $file, $lineno); + return FALSE; + } + if (strpos($line, "]") === FALSE) { + _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); + return FALSE; + } + $frombracket = strstr($line, "["); + $plural = substr($frombracket, 1, strpos($frombracket, "]") - 1); + $line = trim(strstr($line, " ")); + $quoted = _locale_import_parse_quoted($line); + if ($quoted === FALSE) { + _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); + return FALSE; + } + $current["msgstr"][$plural] = $quoted; + $context = "MSGSTR_ARR"; + } + elseif (!strncmp("msgstr", $line, 6)) { + if ($context != "MSGID") { // Should come just after a msgid block + _locale_import_message('The translation file %filename contains an error: "msgstr" is unexpected on line %line.', $file, $lineno); + return FALSE; + } + $line = trim(substr($line, 6)); + $quoted = _locale_import_parse_quoted($line); + if ($quoted === FALSE) { + _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); + return FALSE; + } + $current["msgstr"] = $quoted; + $context = "MSGSTR"; + } + elseif ($line != "") { + $quoted = _locale_import_parse_quoted($line); + if ($quoted === FALSE) { + _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); + return FALSE; + } + if (($context == "MSGID") || ($context == "MSGID_PLURAL")) { + $current["msgid"] .= $quoted; + } + elseif ($context == "MSGSTR") { + $current["msgstr"] .= $quoted; + } + elseif ($context == "MSGSTR_ARR") { + $current["msgstr"][$plural] .= $quoted; + } + else { + _locale_import_message('The translation file %filename contains an error: there is an unexpected string on line %line.', $file, $lineno); + return FALSE; + } + } } -} -/** - * Process language deletion submissions. - */ -function locale_languages_delete_form_submit($form, &$form_state) { - $languages = language_list(); - if (isset($languages[$form_state['values']['langcode']])) { - db_query("DELETE FROM {languages} WHERE language = '%s'", $form_state['values']['langcode']); - db_query("DELETE FROM {locales_target} WHERE language = '%s'", $form_state['values']['langcode']); - db_query("UPDATE {node} SET language = '' WHERE language = '%s'", $form_state['values']['langcode']); - $variables = array('%locale' => $languages[$form_state['values']['langcode']]->name); - drupal_set_message(t('The language %locale has been removed.', $variables)); - watchdog('locale', 'The language %locale has been removed.', $variables); + // End of PO file, flush last entry + if (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { + _locale_import_one_string($op, $current, $mode, $lang, $file); + } + elseif ($context != "COMMENT") { + _locale_import_message('The translation file %filename ended unexpectedly at line %line.', $file, $lineno); + return FALSE; } - - // Changing the language settings impacts the interface: - cache_clear_all('*', 'cache_page', TRUE); - - $form_state['redirect'] = 'admin/settings/language'; - return; } -/** - * @} End of "locale-language-add-edit" - */ - -/** - * @defgroup locale-languages-negotiation Language negotiation options screen - * @{ - */ - -/** - * Setting for language negotiation options - */ -function locale_languages_configure_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('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', - '#value' => t('Save settings') - ); - return $form; -} - -/** - * Submit function for language negotiation settings. - */ -function locale_languages_configure_form_submit($form, &$form_state) { - variable_set('language_negotiation', $form_state['values']['language_negotiation']); - drupal_set_message(t('Language negotiation configuration saved.')); - $form_state['redirect'] = 'admin/settings/language'; - return; -} -/** - * @} 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 INNER JOIN {locales_target} t ON s.lid = t.lid 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; -} - -/** - * 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('Both translated and untranslated strings'), '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_translate_import_form() { - // Get all languages, except English - $names = locale_language_list('name', TRUE); - unset($names['en']); - - if (!count($names)) { - $languages = _locale_prepare_predefined_list(); - $default = array_shift(array_keys($languages)); - } - else { - $languages = array( - t('Already added languages') => $names, - t('Languages not yet added') => _locale_prepare_predefined_list() - ); - $default = array_shift(array_keys($names)); - } - - $form = array(); - $form['import'] = array('#type' => 'fieldset', - '#title' => t('Import translation'), - ); - $form['import']['file'] = array('#type' => 'file', - '#title' => t('Language file'), - '#size' => 50, - '#description' => t('A gettext Portable Object (.po) file.'), - ); - $form['import']['langcode'] = array('#type' => 'select', - '#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, 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'), - '#default_value' => 'overwrite', - '#options' => array(LOCALE_IMPORT_OVERWRITE => t('Strings in the uploaded file replace existing ones, new ones are added'), LOCALE_IMPORT_KEEP => t('Existing strings are kept, only new strings are added')), - ); - $form['import']['submit'] = array('#type' => 'submit', '#value' => t('Import')); - $form['#attributes']['enctype'] = 'multipart/form-data'; - - return $form; -} - -/** - * Process the locale import form submission. - */ -function locale_translate_import_form_submit($form, &$form_state) { - // Ensure we have the file uploaded - if ($file = file_save_upload('file')) { - - // Add language, if not yet supported - $languages = language_list('language', TRUE); - $langcode = $form_state['values']['langcode']; - if (!isset($languages[$langcode])) { - $predefined = _locale_get_predefined_list(); - locale_add_language($langcode); - drupal_set_message(t('The language %language has been created.', array('%language' => t($predefined[$langcode][0])))); - } - - // Now import strings into the language - if ($ret = _locale_import_po($file, $langcode, $form_state['values']['mode'], $form_state['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); - } - } - else { - drupal_set_message(t('File to import not found.'), 'error'); - return 'admin/build/translate/import'; - } - - $form_state['redirect'] = 'admin/build/translate'; - return; -} -/** - * @} 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; -} - -/** - * Form to export PO files for the languages provided. - * - * @param $names - * An associate array with localized language names - */ -function locale_translate_export_po_form(&$form_state, $names) { - $form['export'] = array('#type' => 'fieldset', - '#title' => t('Export translation'), - '#collapsible' => TRUE, - ); - $form['export']['langcode'] = array('#type' => 'select', - '#title' => t('Language name'), - '#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; -} - -/** - * 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 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')); - // Reuse PO export submission callback. - $form['#submit'][] = 'locale_translate_export_po_form_submit'; - $form['#validate'][] = 'locale_translate_export_po_form_validate'; - return $form; -} - -/** - * Process a translation (or template) export form submission. - */ -function locale_translate_export_po_form_submit($form, &$form_state) { - // If template is required, language code is not given. - $language = NULL; - if (isset($form_state['values']['langcode'])) { - $languages = language_list(); - $language = $languages[$form_state['values']['langcode']]; - } - _locale_export_po($language, _locale_export_po_generate($language, _locale_export_get_strings($language, $form_state['values']['group']))); -} -/** - * @} End of "locale-translate-export" - */ - -/** - * @defgroup locale-translate-edit Translation text editing - * @{ - */ - -/** - * User interface for string editing. - */ -function locale_translate_edit_form(&$form_state, $lid) { - // Fetch source string, if possible. - $source = db_fetch_object(db_query('SELECT source, textgroup, location FROM {locales_source} WHERE lid = %d', $lid)); - if (!$source) { - drupal_set_message(t('String not found.'), 'error'); - drupal_goto('admin/build/translate/search'); - } - - // Add original text to the top and some values for form altering. - $form = array( - 'original' => array( - '#type' => 'item', - '#title' => t('Original text'), - '#value' => check_plain(wordwrap($source->source, 0)), - ), - 'lid' => array( - '#type' => 'value', - '#value' => $lid - ), - 'textgroup' => array( - '#type' => 'value', - '#value' => $source->textgroup, - ), - 'location' => array( - '#type' => 'value', - '#value' => $source->location - ), - ); - - // Include default form controls with empty values for all languages. - // This ensures that the languages are always in the same order in forms. - $languages = language_list(); - $default = language_default(); - // We don't need the default language value, that value is in $source. - $omit = $source->textgroup == 'default' ? 'en' : $default->language; - unset($languages[($omit)]); - $form['translations'] = array('#tree' => TRUE); - // Approximate the number of rows to use in the default textarea. - $rows = min(ceil(str_word_count($source->source) / 12), 10); - foreach ($languages as $langcode => $language) { - $form['translations'][$langcode] = array( - '#type' => 'textarea', - '#title' => t($language->name), - '#rows' => $rows, - '#default_value' => '', - ); - } - - // Fetch translations and fill in default values in the form. - $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = %d AND language != '%s'", $lid, $omit); - while ($translation = db_fetch_object($result)) { - $form['translations'][$translation->language]['#default_value'] = $translation->translation; - } - - $form['submit'] = array('#type' => 'submit', '#value' => t('Save translations')); - return $form; -} - -/** - * Process string editing form submissions. - * Saves all translations of one string submitted from a form. - */ -function locale_translate_edit_form_submit($form, &$form_state) { - $lid = $form_state['values']['lid']; - foreach ($form_state['values']['translations'] as $key => $value) { - $translation = db_result(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key)); - if (!empty($value)) { - // Only update or insert if we have a value to use. - if (!empty($translation)) { - 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, language) VALUES (%d, '%s', '%s')", $lid, $value, $key); - } - } - elseif (!empty($translation)) { - // Empty translation entered: remove existing entry from database. - db_query("DELETE FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key); - } - - // Refresh the JS file for this language. - _locale_rebuild_js($key); - } - - drupal_set_message(t('The string has been saved.')); - - // Clear locale cache. - cache_clear_all('locale:', 'cache', TRUE); - - $form_state['redirect'] = 'admin/build/translate/search'; - return; -} -/** - * @} End of "locale-translate-edit" - */ - -/** - * @defgroup locale-translate-delete Translation delete interface. - * @{ - */ - -/** - * Delete a language string. - */ -function locale_translate_delete($lid) { - $langcode = db_result(db_query('SELECT language FROM {locales_target} WHERE lid = %d', $lid)); - db_query('DELETE FROM {locales_source} WHERE lid = %d', $lid); - db_query('DELETE FROM {locales_target} WHERE lid = %d', $lid); - if ($langcode) { - _locale_rebuild_js($langcode); - } - cache_clear_all('locale:', 'cache', TRUE); - drupal_set_message(t('The string has been removed.')); - drupal_goto('admin/build/translate/search'); -} -/** - * @} End of "locale-translate-delete" - */ - -/** - * @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 $enabled - * Optionally TRUE to enable the language when created or FALSE to disable. - * @param $default - * Optionally set this language to be the default. - */ -function locale_add_language($langcode, $name = NULL, $native = NULL, $direction = LANGUAGE_LTR, $domain = '', $prefix = '', $enabled = TRUE, $default = FALSE) { - // Default prefix on language code. - if (empty($prefix)) { - $prefix = $langcode; - } - - // If name was not set, we add a predefined language. - if (!isset($name)) { - $predefined = _locale_get_predefined_list(); - $name = $predefined[$langcode][0]; - $native = isset($predefined[$langcode][1]) ? $predefined[$langcode][1] : $predefined[$langcode][0]; - $direction = isset($predefined[$langcode][2]) ? $predefined[$langcode][2] : LANGUAGE_LTR; - } - - db_query("INSERT INTO {languages} (language, name, native, direction, domain, prefix, enabled) VALUES ('%s', '%s', '%s', %d, '%s', '%s', %d)", $langcode, $name, $native, $direction, $domain, $prefix, $enabled); - - // Only set it as default if enabled. - if ($enabled && $default) { - variable_set('language_default', (object) array('language' => $langcode, 'name' => $name, 'native' => $native, 'direction' => $direction, 'enabled' => (int) $enabled, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => $prefix, 'weight' => 0)); - } - - if ($enabled) { - // Increment enabled language count if we are adding an enabled language. - variable_set('language_count', variable_get('language_count', 1) + 1); - } - - 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 - * - * @param $file - * Drupal file object corresponding to the PO file to import - * @param $langcode - * Language code - * @param $mode - * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE - * @param $group - * Text group to import PO file into (eg. 'default' for interface translations) - */ -function _locale_import_po($file, $langcode, $mode, $group = NULL) { - // If not in 'safe mode', increase the maximum execution time. - if (!ini_get('safe_mode')) { - set_time_limit(240); - } - - // Check if we have the language already in the database. - if (!db_fetch_object(db_query("SELECT language FROM {languages} WHERE language = '%s'", $langcode))) { - drupal_set_message(t('The language selected for import is not supported.'), 'error'); - return FALSE; - } - - // Get strings from file (returns on failure after a partial import, or on success) - $status = _locale_import_read_po('db-store', $file, $mode, $langcode, $group); - if ($status === FALSE) { - // Error messages are set in _locale_import_read_po(). - return FALSE; - } - - // Get status information on import process. - list($headerdone, $additions, $updates, $deletes) = _locale_import_one_string('db-report'); - - if (!$headerdone) { - drupal_set_message(t('The translation file %filename appears to have a missing or malformed header.', array('%filename' => $file->filename)), 'error'); - } - - // Clear cache and refresh JavaScript translations. - _locale_rebuild_js($langcode); - cache_clear_all('locale:', 'cache', TRUE); - - // Rebuild the menu, strings may have changed. - menu_rebuild(); - - drupal_set_message(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => $additions, '%update' => $updates, '%delete' => $deletes))); - watchdog('locale', 'Imported %file into %locale: %number new strings added, %update updated and %delete removed.', array('%file' => $file->filename, '%locale' => $langcode, '%number' => $additions, '%update' => $updates, '%delete' => $deletes)); - return TRUE; -} - -/** - * Parses Gettext Portable Object file into an array - * - * @param $op - * Storage operation type: db-store or mem-store - * @param $file - * Drupal file object corresponding to the PO file to import - * @param $mode - * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE - * @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, $group = NULL) { - - $fd = fopen($file->filepath, "rb"); // File will get closed by PHP on return - if (!$fd) { - _locale_import_message('The translation import failed, because the file %filename could not be read.', $file); - return FALSE; - } - - $context = "COMMENT"; // Parser context: COMMENT, MSGID, MSGID_PLURAL, MSGSTR and MSGSTR_ARR - $current = array(); // Current entry being read - $plural = 0; // Current plural form - $lineno = 0; // Current line - - while (!feof($fd)) { - $line = fgets($fd, 10*1024); // A line should not be this long - $lineno++; - $line = trim(strtr($line, array("\\\n" => ""))); - - if (!strncmp("#", $line, 1)) { // A comment - if ($context == "COMMENT") { // Already in comment context: add - $current["#"][] = substr($line, 1); - } - elseif (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { // End current entry, start a new one - _locale_import_one_string($op, $current, $mode, $lang, $file); - $current = array(); - $current["#"][] = substr($line, 1); - $context = "COMMENT"; - } - else { // Parse error - _locale_import_message('The translation file %filename contains an error: "msgstr" was expected but not found on line %line.', $file, $lineno); - return FALSE; - } - } - elseif (!strncmp("msgid_plural", $line, 12)) { - if ($context != "MSGID") { // Must be plural form for current entry - _locale_import_message('The translation file %filename contains an error: "msgid_plural" was expected but not found on line %line.', $file, $lineno); - return FALSE; - } - $line = trim(substr($line, 12)); - $quoted = _locale_import_parse_quoted($line); - if ($quoted === FALSE) { - _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); - return FALSE; - } - $current["msgid"] = $current["msgid"] ."\0". $quoted; - $context = "MSGID_PLURAL"; - } - elseif (!strncmp("msgid", $line, 5)) { - if ($context == "MSGSTR") { // End current entry, start a new one - _locale_import_one_string($op, $current, $mode, $lang, $file); - $current = array(); - } - elseif ($context == "MSGID") { // Already in this context? Parse error - _locale_import_message('The translation file %filename contains an error: "msgid" is unexpected on line %line.', $file, $lineno); - return FALSE; - } - $line = trim(substr($line, 5)); - $quoted = _locale_import_parse_quoted($line); - if ($quoted === FALSE) { - _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); - return FALSE; - } - $current["msgid"] = $quoted; - $context = "MSGID"; - } - elseif (!strncmp("msgstr[", $line, 7)) { - if (($context != "MSGID") && ($context != "MSGID_PLURAL") && ($context != "MSGSTR_ARR")) { // Must come after msgid, msgid_plural, or msgstr[] - _locale_import_message('The translation file %filename contains an error: "msgstr[]" is unexpected on line %line.', $file, $lineno); - return FALSE; - } - if (strpos($line, "]") === FALSE) { - _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); - return FALSE; - } - $frombracket = strstr($line, "["); - $plural = substr($frombracket, 1, strpos($frombracket, "]") - 1); - $line = trim(strstr($line, " ")); - $quoted = _locale_import_parse_quoted($line); - if ($quoted === FALSE) { - _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); - return FALSE; - } - $current["msgstr"][$plural] = $quoted; - $context = "MSGSTR_ARR"; - } - elseif (!strncmp("msgstr", $line, 6)) { - if ($context != "MSGID") { // Should come just after a msgid block - _locale_import_message('The translation file %filename contains an error: "msgstr" is unexpected on line %line.', $file, $lineno); - return FALSE; - } - $line = trim(substr($line, 6)); - $quoted = _locale_import_parse_quoted($line); - if ($quoted === FALSE) { - _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); - return FALSE; - } - $current["msgstr"] = $quoted; - $context = "MSGSTR"; - } - elseif ($line != "") { - $quoted = _locale_import_parse_quoted($line); - if ($quoted === FALSE) { - _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); - return FALSE; - } - if (($context == "MSGID") || ($context == "MSGID_PLURAL")) { - $current["msgid"] .= $quoted; - } - elseif ($context == "MSGSTR") { - $current["msgstr"] .= $quoted; - } - elseif ($context == "MSGSTR_ARR") { - $current["msgstr"][$plural] .= $quoted; - } - else { - _locale_import_message('The translation file %filename contains an error: there is an unexpected string on line %line.', $file, $lineno); - return FALSE; - } - } - } - - // End of PO file, flush last entry - if (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { - _locale_import_one_string($op, $current, $mode, $lang, $file); - } - elseif ($context != "COMMENT") { - _locale_import_message('The translation file %filename ended unexpectedly at line %line.', $file, $lineno); - return FALSE; - } - -} - -/** - * Sets an error message occurred during locale file parsing. - * - * @param $message - * The message to be translated - * @param $file - * Drupal file object corresponding to the PO file to import - * @param $lineno - * An optional line number argument - */ -function _locale_import_message($message, $file, $lineno = NULL) { - $vars = array('%filename' => $file->filename); - if (isset($lineno)) { - $vars['%line'] = $lineno; - } - $t = get_t(); - drupal_set_message($t($message, $vars), 'error'); -} - -/** - * Imports a string into the database - * - * @param $op - * Operation to perform: 'db-store', 'db-report', 'mem-store' or 'mem-report' - * @param $value - * Details of the string stored - * @param $mode - * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE - * @param $lang - * 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, $group = 'default') { - static $report = array(0, 0, 0); - static $headerdone = FALSE; - static $strings = array(); - - switch ($op) { - // Return stored strings - case 'mem-report': - return $strings; - - // Store string in memory (only supports single strings) - case 'mem-store': - $strings[$value['msgid']] = $value['msgstr']; - return; - - // Called at end of import to inform the user - case 'db-report': - return array($headerdone, $report[0], $report[1], $report[2]); - - // Store the string we got in the database. - case 'db-store': - // We got header information. - if ($value['msgid'] == '') { - $header = _locale_import_parse_header($value['msgstr']); - - // Get the plural formula and update in database. - if (isset($header["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($header["Plural-Forms"], $file->filename)) { - list($nplurals, $plural) = $p; - db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", $nplurals, $plural, $lang); - } - else { - db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", 0, '', $lang); - } - $headerdone = TRUE; - } - - else { - // Some real string to import. - $comments = _locale_import_shorten_comments(empty($value['#']) ? array() : $value['#']); - - if (strpos($value['msgid'], "\0")) { - // This string has plural versions. - $english = explode("\0", $value['msgid'], 2); - $entries = array_keys($value['msgstr']); - for ($i = 3; $i <= count($entries); $i++) { - $english[] = $english[1]; - } - $translation = array_map('_locale_import_append_plural', $value['msgstr'], $entries); - $english = array_map('_locale_import_append_plural', $english, $entries); - foreach ($translation as $key => $trans) { - if ($key == 0) { - $plid = 0; - } - $plid = _locale_import_one_string_db($report, $lang, $english[$key], $trans, $group, $comments, $mode, $plid, $key); - } - } - - else { - // A simple string to import. - $english = $value['msgid']; - $translation = $value['msgstr']; - _locale_import_one_string_db($report, $lang, $english, $translation, $group, $comments, $mode); - } - } - } // end of db-store operation -} - -/** - * Import one string into the database. - * - * @param $report - * Report array summarizing the number of changes done in the form: - * array(inserts, updates, deletes). - * @param $langcode - * Language code to import string into. - * @param $source - * Source string. - * @param $translation - * Translation to language specified in $langcode. - * @param $textgroup - * Name of textgroup to store translation in. - * @param $location - * Location value to save with source string. - * @param $mode - * Import mode to use, LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE. - * @param $plid - * Optional plural ID to use. - * @param $plural - * Optional plural value to use. - * @return - * The string ID of the existing string modified or the new string added. - */ -function _locale_import_one_string_db(&$report, $langcode, $source, $translation, $textgroup, $location, $mode, $plid = NULL, $plural = NULL) { - $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup)); - - if (!empty($translation)) { - if ($lid) { - // We have this source string saved already. - db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $location, $lid); - $exists = (bool) db_result(db_query("SELECT lid FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $langcode)); - if (!$exists) { - // No translation in this language. - db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural); - $report[0]++; - } - else if ($mode == LOCALE_IMPORT_OVERWRITE) { - // Translation exists, only overwrite if instructed. - db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE language = '%s' AND lid = %d", $translation, $plid, $plural, $langcode, $lid); - $report[1]++; - } - } - else { - // No such source string in the database yet. - db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', '%s')", $location, $source, $textgroup); - $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup)); - db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural); - $report[0]++; - } - } - elseif ($mode == LOCALE_IMPORT_OVERWRITE) { - // Empty translation, remove existing if instructed. - db_query("DELETE FROM {locales_target} WHERE language = '%s' AND lid = %d AND plid = %d AND plural = %d", $translation, $langcode, $lid, $plid, $plural); - $report[2]++; - } - - return $lid; -} - -/** - * Parses a Gettext Portable Object file header - * - * @param $header - * A string containing the complete header - * @return - * An associative array of key-value pairs - */ -function _locale_import_parse_header($header) { - $header_parsed = array(); - $lines = array_map('trim', explode("\n", $header)); - foreach ($lines as $line) { - if ($line) { - list($tag, $contents) = explode(":", $line, 2); - $header_parsed[trim($tag)] = trim($contents); - } - } - return $header_parsed; -} - -/** - * Parses a Plural-Forms entry from a Gettext Portable Object file header - * - * @param $pluralforms - * A string containing the Plural-Forms entry - * @param $filename - * A string containing the filename - * @return - * An array containing the number of plurals and a - * formula in PHP for computing the plural form - */ -function _locale_import_parse_plural_forms($pluralforms, $filename) { - // First, delete all whitespace - $pluralforms = strtr($pluralforms, array(" " => "", "\t" => "")); - - // Select the parts that define nplurals and plural - $nplurals = strstr($pluralforms, "nplurals="); - if (strpos($nplurals, ";")) { - $nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9); - } - else { - return FALSE; - } - $plural = strstr($pluralforms, "plural="); - if (strpos($plural, ";")) { - $plural = substr($plural, 7, strpos($plural, ";") - 7); - } - else { - return FALSE; - } - - // Get PHP version of the plural formula - $plural = _locale_import_parse_arithmetic($plural); - - if ($plural !== FALSE) { - return array($nplurals, $plural); - } - else { - drupal_set_message(t('The translation file %filename contains an error: the plural formula could not be parsed.', array('%filename' => $filename)), 'error'); - return FALSE; - } -} - -/** - * Parses and sanitizes an arithmetic formula into a PHP expression - * - * While parsing, we ensure, that the operators have the right - * precedence and associativity. - * - * @param $string - * A string containing the arithmetic formula - * @return - * The PHP version of the formula - */ -function _locale_import_parse_arithmetic($string) { - // Operator precedence table - $prec = array("(" => -1, ")" => -1, "?" => 1, ":" => 1, "||" => 3, "&&" => 4, "==" => 5, "!=" => 5, "<" => 6, ">" => 6, "<=" => 6, ">=" => 6, "+" => 7, "-" => 7, "*" => 8, "/" => 8, "%" => 8); - // Right associativity - $rasc = array("?" => 1, ":" => 1); - - $tokens = _locale_import_tokenize_formula($string); - - // Parse by converting into infix notation then back into postfix - $opstk = array(); - $elstk = array(); - - foreach ($tokens as $token) { - $ctok = $token; - - // Numbers and the $n variable are simply pushed into $elarr - if (is_numeric($token)) { - $elstk[] = $ctok; - } - elseif ($ctok == "n") { - $elstk[] = '$n'; - } - elseif ($ctok == "(") { - $opstk[] = $ctok; - } - elseif ($ctok == ")") { - $topop = array_pop($opstk); - while (isset($topop) && ($topop != "(")) { - $elstk[] = $topop; - $topop = array_pop($opstk); - } - } - elseif (!empty($prec[$ctok])) { - // 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 (isset($topop) && ($prec[$topop] >= $prec[$ctok]) && !(($prec[$topop] == $prec[$ctok]) && !empty($rasc[$topop]) && !empty($rasc[$ctok]))) { - $elstk[] = $topop; - $topop = array_pop($opstk); - } - if ($topop) { - $opstk[] = $topop; // Return element to top - } - $opstk[] = $ctok; // Parentheses are not needed - } - else { - return FALSE; - } - } - - // Flush operator stack - $topop = array_pop($opstk); - while ($topop != NULL) { - $elstk[] = $topop; - $topop = array_pop($opstk); - } - - // Now extract formula from stack - $prevsize = count($elstk) + 1; - while (count($elstk) < $prevsize) { - $prevsize = count($elstk); - for ($i = 2; $i < count($elstk); $i++) { - $op = $elstk[$i]; - if (!empty($prec[$op])) { - $f = ""; - if ($op == ":") { - $f = $elstk[$i - 2] ."):". $elstk[$i - 1] .")"; - } - elseif ($op == "?") { - $f = "(". $elstk[$i - 2] ."?(". $elstk[$i - 1]; - } - else { - $f = "(". $elstk[$i - 2] . $op . $elstk[$i - 1] .")"; - } - array_splice($elstk, $i - 2, 3, $f); - break; - } - } - } - - // If only one element is left, the number of operators is appropriate - if (count($elstk) == 1) { - return $elstk[0]; - } - else { - return FALSE; - } -} - -/** - * Backward compatible implementation of token_get_all() for formula parsing - * - * @param $string - * A string containing the arithmetic formula - * @return - * The PHP version of the formula - */ -function _locale_import_tokenize_formula($formula) { - $formula = str_replace(" ", "", $formula); - $tokens = array(); - for ($i = 0; $i < strlen($formula); $i++) { - if (is_numeric($formula[$i])) { - $num = $formula[$i]; - $j = $i + 1; - while ($j < strlen($formula) && is_numeric($formula[$j])) { - $num .= $formula[$j]; - $j++; - } - $i = $j - 1; - $tokens[] = $num; - } - elseif ($pos = strpos(" =<>!&|", $formula[$i])) { // We won't have a space - $next = $formula[$i + 1]; - switch ($pos) { - case 1: - case 2: - case 3: - case 4: - if ($next == '=') { - $tokens[] = $formula[$i] .'='; - $i++; - } - else { - $tokens[] = $formula[$i]; - } - break; - case 5: - if ($next == '&') { - $tokens[] = '&&'; - $i++; - } - else { - $tokens[] = $formula[$i]; - } - break; - case 6: - if ($next == '|') { - $tokens[] = '||'; - $i++; - } - else { - $tokens[] = $formula[$i]; - } - break; - } - } - else { - $tokens[] = $formula[$i]; - } - } - return $tokens; -} - -/** - * Modify a string to contain proper count indices - * - * This is a callback function used via array_map() - * - * @param $entry - * An array element - * @param $key - * Index of the array element - */ -function _locale_import_append_plural($entry, $key) { - // No modifications for 0, 1 - if ($key == 0 || $key == 1) { - return $entry; - } - - // First remove any possibly false indices, then add new ones - $entry = preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); - return preg_replace('/(@count)/', "\\1[$key]", $entry); -} - -/** - * Generate a short, one string version of the passed comment array - * - * @param $comment - * An array of strings containing a comment - * @return - * Short one string version of the comment - */ -function _locale_import_shorten_comments($comment) { - $comm = ''; - while (count($comment)) { - $test = $comm . substr(array_shift($comment), 1) .', '; - if (strlen($comm) < 130) { - $comm = $test; - } - else { - break; - } - } - return substr($comm, 0, -2); -} - -/** - * Parses a string in quotes - * - * @param $string - * A string specified with enclosing quotes - * @return - * The string parsed from inside the quotes - */ -function _locale_import_parse_quoted($string) { - if (substr($string, 0, 1) != substr($string, -1, 1)) { - return FALSE; // Start and end quotes must be the same - } - $quote = substr($string, 0, 1); - $string = substr($string, 1, -1); - if ($quote == '"') { // Double quotes: strip slashes - return stripcslashes($string); - } - elseif ($quote == "'") { // Simple quote: return as-is - return $string; - } - else { - return FALSE; // Unrecognized quote - } -} -/** - * @} End of "locale-api-import" - */ - -/** - * This function checks all JavaScript files currently added via drupal_add_js() - * and invokes parsing if they have not yet been parsed for Drupal.t() calls. - */ -function _locale_update_js_files() { - global $language; - - $parsed = variable_get('javascript_parsed', array()); - - // The first three parameters are NULL in order to get an array with all - // scopes. This is necessary to prevent recreation of JS translation files - // when new files are added for example in the footer. - $javascript = drupal_add_js(NULL, NULL, NULL); - $files = FALSE; - - foreach ($javascript as $scope) { - foreach ($scope as $type => $data) { - if ($type != 'setting' && $type != 'inline') { - foreach ($data as $filepath => $info) { - $files = TRUE; - if (!in_array($filepath, $parsed)) { - _locale_parse_js_file($filepath); - watchdog('locale', 'Parsed JavaScript file %file.', array('%file' => $filepath)); - $parsed[] = $filepath; - } - } - } - } - } - - // Update the parsed files storage array. - variable_set('javascript_parsed', $parsed); - _locale_rebuild_js(); - - // Add the translation JavaScript file to the page. - if ($files && !empty($language->javascript)) { - drupal_add_js(file_create_path(variable_get('locale_js_directory', 'languages') .'/'. $language->language .'_'. $language->javascript .'.js'), 'core'); - } -} - -/** - * Parses a JavaScript file, extracts strings wrapped in Drupal.t() and - * Drupal.formatPlural() and inserts them into the database. - */ -function _locale_parse_js_file($filepath) { - global $language; - - // Load the JavaScript file. - $file = file_get_contents($filepath); - - // Match all calls to Drupal.t() in an array. - // Note: \s also matches newlines with the 's' modifier. - preg_match_all('~[^\w]Drupal\s*\.\s*t\s*\(\s*('. LOCALE_JS_STRING .')\s*[,\)]~s', $file, $t_matches); - - // Match all Drupal.formatPlural() calls in another array. - preg_match_all('~[^\w]Drupal\s*\.\s*formatPlural\s*\(\s*.+?\s*,\s*('. LOCALE_JS_STRING .')\s*,\s*((?:(?:\'(?:\\\\\'|[^\'])*@count(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*@count(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*[,\)]~s', $file, $plural_matches); - - // Loop through all matches and process them. - $all_matches = array_merge($plural_matches[1], $t_matches[1]); - foreach ($all_matches as $key => $string) { - $strings = array($string); - - // If there is also a plural version of this string, add it to the strings array. - if (isset($plural_matches[2][$key])) { - $strings[] = $plural_matches[2][$key]; - } - - foreach ($strings as $key => $string) { - // Remove the quotes and string concatenations from the string. - $string = implode('', preg_split('~(?location); - - if (!in_array($filepath, $locations)) { - $locations[] = $filepath; - $locations = implode('; ', $locations); - - // Save the new locations string to the database. - db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $locations, $source->lid); - } - } - else { - // We don't have the source string yet, thus we insert it into the database. - db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', 'default')", $filepath, $string); - } - } - } -} - -/** - * @defgroup locale-api-export Translation (template) export API. - * @{ - */ - -/** - * Generates a structured array of all strings with translations in - * $language, if given. This array can be used to generate an export - * of the string in the database. - * - * @param $language - * Language object 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_get_strings($language = NULL, $group = 'default') { - if (isset($language)) { - $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $language->language, $group); - } - else { - $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $group); - } - $strings = array(); - while ($child = db_fetch_object($result)) { - $string = array( - 'comment' => $child->location, - 'source' => $child->source, - 'translation' => isset($child->translation) ? $child->translation : '' - ); - if ($child->plid) { - // Has a parent lid. Since we process in the order of plids, - // we already have the parent in the array, so we can add the - // lid to the next plural version to it. This builds a linked - // list of plurals. - $string['child'] = TRUE; - $strings[$child->plid]['plural'] = $child->lid; - } - $strings[$child->lid] = $string; - } - return $strings; -} - -/** - * Generates the PO(T) file contents for given strings. - * - * @param $language - * Language object to generate the output for, or NULL if generating - * translation template. - * @param $strings - * Array of strings to export. See _locale_export_get_strings() - * on how it should be formatted. - * @param $header - * The header portion to use for the output file. Defaults - * are provided for PO and POT files. - */ -function _locale_export_po_generate($language = NULL, $strings = array(), $header = NULL) { - global $user; - - if (!isset($header)) { - if (isset($language)) { - $header = '# '. $language->name .' translation of '. variable_get('site_name', 'Drupal') ."\n"; - $header .= '# Generated by '. $user->name .' <'. $user->mail .">\n"; - $header .= "#\n"; - $header .= "msgid \"\"\n"; - $header .= "msgstr \"\"\n"; - $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n"; - $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; - $header .= "\"PO-Revision-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; - $header .= "\"Last-Translator: NAME \\n\"\n"; - $header .= "\"Language-Team: LANGUAGE \\n\"\n"; - $header .= "\"MIME-Version: 1.0\\n\"\n"; - $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n"; - $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; - if ($language->formula && $language->plurals) { - $header .= "\"Plural-Forms: nplurals=". $language->plurals ."; plural=". strtr($language->formula, array('$' => '')) .";\\n\"\n"; - } - } - else { - $header = "# LANGUAGE translation of PROJECT\n"; - $header .= "# Copyright (c) YEAR NAME \n"; - $header .= "#\n"; - $header .= "msgid \"\"\n"; - $header .= "msgstr \"\"\n"; - $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n"; - $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; - $header .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n"; - $header .= "\"Last-Translator: NAME \\n\"\n"; - $header .= "\"Language-Team: LANGUAGE \\n\"\n"; - $header .= "\"MIME-Version: 1.0\\n\"\n"; - $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n"; - $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; - $header .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n"; - } - } - - $output = $header ."\n"; - - foreach ($strings as $lid => $string) { - // Only process non-children, children are output below their parent. - if (!isset($string['child'])) { - if ($string['comment']) { - $output .= '#: '. $string['comment'] ."\n"; - } - $output .= 'msgid '. _locale_export_string($string['source']); - if (!empty($string['plural'])) { - $plural = $string['plural']; - $output .= 'msgid_plural '. _locale_export_string($strings[$plural]['source']); - if (isset($language)) { - $translation = $string['translation']; - for ($i = 0; $i < $language->plurals; $i++) { - $output .= 'msgstr['. $i .'] '. _locale_export_string($translation); - if ($plural) { - $translation = _locale_export_remove_plural($strings[$plural]['translation']); - $plural = isset($strings[$plural]['plural']) ? $strings[$plural]['plural'] : 0; - } - else { - $translation = ''; - } - } - } - else { - $output .= 'msgstr[0] ""'."\n"; - $output .= 'msgstr[1] ""'."\n"; - } - } - else { - $output .= 'msgstr '. _locale_export_string($string['translation']); - } - $output .= "\n"; - } - } - return $output; -} - -/** - * Write a generated PO or POT file to the output. - * - * @param $language - * Language object to generate the output for, or NULL if generating - * translation template. - * @param $output - * The PO(T) file to output as a string. See _locale_export_generate_po() - * on how it can be generated. - */ -function _locale_export_po($language = NULL, $output = NULL) { - // Log the export event. - if (isset($language)) { - $filename = $language->language .'.po'; - watchdog('locale', 'Exported %locale translation file: %filename.', array('%locale' => $language->name, '%filename' => $filename)); - } - else { - $filename = 'drupal.pot'; - watchdog('locale', 'Exported translation file: %filename.', array('%filename' => $filename)); - } - // Download the file fo the client. - header("Content-Disposition: attachment; filename=$filename"); - header("Content-Type: text/plain; charset=utf-8"); - print $output; - die(); -} - -/** - * Print out a string on multiple lines - */ -function _locale_export_string($str) { - $stri = addcslashes($str, "\0..\37\\\""); - $parts = array(); - - // Cut text into several lines - while ($stri != "") { - $i = strpos($stri, "\\n"); - if ($i === FALSE) { - $curstr = $stri; - $stri = ""; - } - else { - $curstr = substr($stri, 0, $i + 2); - $stri = substr($stri, $i + 2); - } - $curparts = explode("\n", _locale_export_wrap($curstr, 70)); - $parts = array_merge($parts, $curparts); - } - - // Multiline string - if (count($parts) > 1) { - return "\"\"\n\"". implode("\"\n\"", $parts) ."\"\n"; - } - // Single line string - elseif (count($parts) == 1) { - return "\"$parts[0]\"\n"; - } - // No translation - else { - return "\"\"\n"; - } -} - -/** - * Custom word wrapping for Portable Object (Template) files. - */ -function _locale_export_wrap($str, $len) { - $words = explode(' ', $str); - $ret = array(); - - $cur = ""; - $nstr = 1; - while (count($words)) { - $word = array_shift($words); - if ($nstr) { - $cur = $word; - $nstr = 0; - } - elseif (strlen("$cur $word") > $len) { - $ret[] = $cur ." "; - $cur = $word; - } - else { - $cur = "$cur $word"; - } - } - $ret[] = $cur; - - return implode("\n", $ret); -} - -/** - * Removes plural index information from a string - */ -function _locale_export_remove_plural($entry) { - return preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); -} -/** - * @} End of "locale-api-export" - */ - -/** - * @defgroup locale-api-seek String search functions. - * @{ - */ - -/** - * Perform a string search and display results in a table - */ -function _locale_translate_seek() { - $output = ''; - - // We have at least one criterion to match - if ($query = _locale_translate_seek_query()) { - $join = "SELECT s.source, s.location, s.lid, s.textgroup, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid "; - - $arguments = array(); - $limit_language = FALSE; - // Compute LIKE section - switch ($query['translation']) { - case 'translated': - $where = "WHERE (t.translation LIKE '%%%s%%')"; - $orderby = "ORDER BY t.translation"; - $arguments[] = $query['string']; - break; - case 'untranslated': - $where = "WHERE (s.source LIKE '%%%s%%' AND t.translation IS NULL)"; - $orderby = "ORDER BY s.source"; - $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']; - 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%%' $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 $grouplimit $orderby"; - break; - // Some different language - default: - $sql = "$join AND t.language = '%s' $where $grouplimit $orderby"; - array_unshift($arguments, $query['language']); - // Don't show translation flags for other languages, we can't see them with this search. - $limit_language = $query['language']; - } - - $result = pager_query($sql, 50, 0, NULL, $arguments); - - $groups = module_invoke_all('locale', 'groups'); - $header = array(t('Text group'), t('String'), ($limit_language) ? t('Language') : t('Languages'), array('data' => t('Operations'), 'colspan' => '2')); - $arr = array(); - while ($locale = db_fetch_object($result)) { - $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( - $value['group'], - array('data' => check_plain(truncate_utf8($value['source'], 150, FALSE, TRUE)) .'
'. $value['location'] .''), - array('data' => _locale_translate_language_list($value['languages'], $limit_language), '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)) { - $output .= theme('table', $header, $rows); - if ($pager = theme('pager', NULL, 50)) { - $output .= $pager; - } - } - else { - $output .= t('No strings found for your search.'); - } - } - - return $output; -} - -/** - * 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; -} - -/** - * (Re-)Creates the JavaScript translation file for a language. - * - * @param $language - * The language, the translation file should be (re)created for. - */ -function _locale_rebuild_js($langcode = NULL) { - if (!isset($langcode)) { - global $language; - } - else { - // Get information about the locale. - $languages = language_list(); - $language = $languages[$langcode]; - } - - // Construct the array for JavaScript translations. - // We sort on plural so that we have all plural forms before singular forms. - $result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.location LIKE '%%.js%%' AND s.textgroup = 'default' ORDER BY t.plural DESC", $language->language); - - $translations = $plurals = array(); - while ($data = db_fetch_object($result)) { - // Only add this to the translations array when there is actually a translation. - if (!empty($data->translation)) { - if ($data->plural) { - // When the translation is a plural form, first add it to another array and - // wait for the singular (parent) translation. - if (!isset($plurals[$data->plid])) { - $plurals[$data->plid] = array($data->plural => $data->translation); - } - else { - $plurals[$data->plid] += array($data->plural => $data->translation); - } - } - elseif (isset($plurals[$data->lid])) { - // There are plural translations for this translation, so get them from - // the plurals array and add them to the final translations array. - $translations[$data->source] = array($data->plural => $data->translation) + $plurals[$data->lid]; - unset($plurals[$data->lid]); - } - else { - // There are no plural forms for this translation, so just add it to - // the translations array. - $translations[$data->source] = $data->translation; - } - } - } - - // Only operate when there are translations. - if (!empty($translations)) { - // Construct the JavaScript file. - $data = "Drupal.locale = { "; - - if (!empty($language->formula)) { - $data .= "'pluralFormula': function(\$n) { return Number({$language->formula}); }, "; - } - - $data .= "'strings': ". drupal_to_js($translations) ." };"; - - // Construct the directory where JS translation files are stored. - // There is (on purpose) no front end to edit that variable. - $dir = file_create_path(variable_get('locale_js_directory', 'languages')); - - // Only create a new file if the content has changed. - $data_hash = md5($data); - if ($language->javascript != $data_hash) { - if (!empty($language->javascript)) { - // We are recreating the new file, so delete the old one. - file_delete(file_create_path($dir .'/'. $language->language .'_'. $language->javascript .'.js')); - $language->javascript = ''; - } - else { - // The directory might not exist when there has not been a translation - // file before, so create it. - file_check_directory($dir, TRUE); - } - - // The file name of the new JavaScript translation file. - $dest = $dir .'/'. $language->language .'_'. $data_hash .'.js'; - $filepath = file_save_data($data, $dest); - - // Update the global $language object. - $language->javascript = $filepath ? $data_hash : ''; - - // Save the new JavaScript hash. - db_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $language->javascript, $language->language); - - // Update the default language variable if the default language has been altered. - // This is necessary to keep the variable consistent with the database - // version of the language and to prevent checking against an outdated hash. - $default_langcode = language_default('language'); - if ($default_langcode == $language->language) { - $default = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $default_langcode)); - variable_set('language_default', $default); - } - - // Only report updated or creation when there is actually a file created. - if ($filepath) { - // There has already been a translation file before. - if (!empty($language->javascript)) { - watchdog('locale', 'Updated JavaScript translation file for the language %language.', array('%language' => t($language->name))); - } - else { - watchdog('locale', 'Created JavaScript translation file for the language %language.', array('%language' => t($language->name))); - } - } - else { - watchdog('locale', 'An error occurred during creation of the JavaScript translation file for the language %language.', array('%language' => t($language->name))); - } - } - } - - // There are no strings for JavaScript translation. However, if there is a file, - // delete it and reset the database. - elseif (!empty($language->javascript)) { - // Delete the old JavaScript file - file_delete(file_create_path(variable_get('locale_js_directory', 'languages') .'/'. $language->language .'_'. $language->javascript .'.js')); - db_query("UPDATE {languages} SET javascript = '' WHERE language = '%s'", $language->language); - watchdog('locale', 'Deleted JavaScript translation file for the locale %language.', array('%language' => t($language->name))); - } -} - -/** - * List languages in search result table - */ -function _locale_translate_language_list($translation, $limit_language) { - // 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) { - if (!$limit_language || $limit_language == $langcode) { - $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 - */ -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) > 1) { - $tname = t($value[0]); - $predefined[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])"; - } - else { - $predefined[$key] = t($value[0]); - } - } - asort($predefined); - return $predefined; -} - -/** - * Some of the common languages with their English and native names - * - * Based on ISO 639 and http://people.w3.org/rishida/names/languages.html - */ -function _locale_get_predefined_list() { - return array( - "aa" => array("Afar"), - "ab" => array("Abkhazian", "аҧсуа бызшәа"), - "ae" => array("Avestan"), - "af" => array("Afrikaans"), - "ak" => array("Akan"), - "am" => array("Amharic", "አማርኛ"), - "ar" => array("Arabic", /* Left-to-right marker "‭" */ "العربية", LANGUAGE_RTL), - "as" => array("Assamese"), - "av" => array("Avar"), - "ay" => array("Aymara"), - "az" => array("Azerbaijani", "azərbaycan"), - "ba" => array("Bashkir"), - "be" => array("Belarusian", "Беларуская"), - "bg" => array("Bulgarian", "Български"), - "bh" => array("Bihari"), - "bi" => array("Bislama"), - "bm" => array("Bambara", "Bamanankan"), - "bn" => array("Bengali"), - "bo" => array("Tibetan"), - "br" => array("Breton"), - "bs" => array("Bosnian", "Bosanski"), - "ca" => array("Catalan", "Català"), - "ce" => array("Chechen"), - "ch" => array("Chamorro"), - "co" => array("Corsican"), - "cr" => array("Cree"), - "cs" => array("Czech", "Čeština"), - "cu" => array("Old Slavonic"), - "cv" => array("Chuvash"), - "cy" => array("Welsh", "Cymraeg"), - "da" => array("Danish", "Dansk"), - "de" => array("German", "Deutsch"), - "dv" => array("Maldivian"), - "dz" => array("Bhutani"), - "ee" => array("Ewe", "Ɛʋɛ"), - "el" => array("Greek", "Ελληνικά"), - "en" => array("English"), - "eo" => array("Esperanto"), - "es" => array("Spanish", "Español"), - "et" => array("Estonian", "Eesti"), - "eu" => array("Basque", "Euskera"), - "fa" => array("Persian", /* Left-to-right marker "‭" */ "فارسی", LANGUAGE_RTL), - "ff" => array("Fulah", "Fulfulde"), - "fi" => array("Finnish", "Suomi"), - "fj" => array("Fiji"), - "fo" => array("Faeroese"), - "fr" => array("French", "Français"), - "fy" => array("Frisian", "Frysk"), - "ga" => array("Irish", "Gaeilge"), - "gd" => array("Scots Gaelic"), - "gl" => array("Galician", "Galego"), - "gn" => array("Guarani"), - "gu" => array("Gujarati"), - "gv" => array("Manx"), - "ha" => array("Hausa"), - "he" => array("Hebrew", /* Left-to-right marker "‭" */ "עברית", LANGUAGE_RTL), - "hi" => array("Hindi", "हिन्दी"), - "ho" => array("Hiri Motu"), - "hr" => array("Croatian", "Hrvatski"), - "hu" => array("Hungarian", "Magyar"), - "hy" => array("Armenian", "Հայերեն"), - "hz" => array("Herero"), - "ia" => array("Interlingua"), - "id" => array("Indonesian", "Bahasa Indonesia"), - "ie" => array("Interlingue"), - "ig" => array("Igbo"), - "ik" => array("Inupiak"), - "is" => array("Icelandic", "Íslenska"), - "it" => array("Italian", "Italiano"), - "iu" => array("Inuktitut"), - "ja" => array("Japanese", "日本語"), - "jv" => array("Javanese"), - "ka" => array("Georgian"), - "kg" => array("Kongo"), - "ki" => array("Kikuyu"), - "kj" => array("Kwanyama"), - "kk" => array("Kazakh", "Қазақ"), - "kl" => array("Greenlandic"), - "km" => array("Cambodian"), - "kn" => array("Kannada", "ಕನ್ನಡ"), - "ko" => array("Korean", "한국어"), - "kr" => array("Kanuri"), - "ks" => array("Kashmiri"), - "ku" => array("Kurdish", "Kurdî"), - "kv" => array("Komi"), - "kw" => array("Cornish"), - "ky" => array("Kirghiz", "Кыргыз"), - "la" => array("Latin", "Latina"), - "lb" => array("Luxembourgish"), - "lg" => array("Luganda"), - "ln" => array("Lingala"), - "lo" => array("Laothian"), - "lt" => array("Lithuanian", "Lietuviškai"), - "lv" => array("Latvian", "Latviešu"), - "mg" => array("Malagasy"), - "mh" => array("Marshallese"), - "mi" => array("Maori"), - "mk" => array("Macedonian", "Македонски"), - "ml" => array("Malayalam", "മലയാളം"), - "mn" => array("Mongolian"), - "mo" => array("Moldavian"), - "mr" => array("Marathi"), - "ms" => array("Malay", "Bahasa Melayu"), - "mt" => array("Maltese", "Malti"), - "my" => array("Burmese"), - "na" => array("Nauru"), - "nd" => array("North Ndebele"), - "ne" => array("Nepali"), - "ng" => array("Ndonga"), - "nl" => array("Dutch", "Nederlands"), - "nb" => array("Norwegian Bokmål", "Bokmål"), - "nn" => array("Norwegian Nynorsk", "Nynorsk"), - "nr" => array("South Ndebele"), - "nv" => array("Navajo"), - "ny" => array("Chichewa"), - "oc" => array("Occitan"), - "om" => array("Oromo"), - "or" => array("Oriya"), - "os" => array("Ossetian"), - "pa" => array("Punjabi"), - "pi" => array("Pali"), - "pl" => array("Polish", "Polski"), - "ps" => array("Pashto", /* Left-to-right marker "‭" */ "پښتو", LANGUAGE_RTL), - "pt" => array("Portuguese, Portugal", "Português"), - "pt-br" => array("Portuguese, Brazil", "Português"), - "qu" => array("Quechua"), - "rm" => array("Rhaeto-Romance"), - "rn" => array("Kirundi"), - "ro" => array("Romanian", "Română"), - "ru" => array("Russian", "Русский"), - "rw" => array("Kinyarwanda"), - "sa" => array("Sanskrit"), - "sc" => array("Sardinian"), - "sd" => array("Sindhi"), - "se" => array("Northern Sami"), - "sg" => array("Sango"), - "sh" => array("Serbo-Croatian"), - "si" => array("Singhalese"), - "sk" => array("Slovak", "Slovenčina"), - "sl" => array("Slovenian", "Slovenščina"), - "sm" => array("Samoan"), - "sn" => array("Shona"), - "so" => array("Somali"), - "sq" => array("Albanian", "Shqip"), - "sr" => array("Serbian", "Српски"), - "ss" => array("Siswati"), - "st" => array("Sesotho"), - "su" => array("Sudanese"), - "sv" => array("Swedish", "Svenska"), - "sw" => array("Swahili", "Kiswahili"), - "ta" => array("Tamil", "தமிழ்"), - "te" => array("Telugu", "తెలుగు"), - "tg" => array("Tajik"), - "th" => array("Thai", "ภาษาไทย"), - "ti" => array("Tigrinya"), - "tk" => array("Turkmen"), - "tl" => array("Tagalog"), - "tn" => array("Setswana"), - "to" => array("Tonga"), - "tr" => array("Turkish", "Türkçe"), - "ts" => array("Tsonga"), - "tt" => array("Tatar", "Tatarça"), - "tw" => array("Twi"), - "ty" => array("Tahitian"), - "ug" => array("Uighur"), - "uk" => array("Ukrainian", "Українська"), - "ur" => array("Urdu", /* Left-to-right marker "‭" */ "اردو", LANGUAGE_RTL), - "uz" => array("Uzbek", "o'zbek"), - "ve" => array("Venda"), - "vi" => array("Vietnamese", "Tiếng Việt"), - "wo" => array("Wolof"), - "xh" => array("Xhosa", "isiXhosa"), - "yi" => array("Yiddish"), - "yo" => array("Yoruba", "Yorùbá"), - "za" => array("Zhuang"), - "zh-hans" => array("Chinese, Simplified", "简体中文"), - "zh-hant" => array("Chinese, Traditional", "繁體中文"), - "zu" => array("Zulu", "isiZulu"), - ); -} -/** - * @} End of "locale-api-languages-predefined" - */ -/** - * @defgroup locale-autoimport Automatic interface translation import - * @{ - */ /** - * Prepare a batch to import translations for all enabled - * modules in a given language. + * Imports a string into the database * - * @param $langcode - * Language code to import translations for. - * @param $finished - * Optional finished callback for the batch. - * @return - * A batch structure or FALSE if no files found. + * @param $op + * Operation to perform: 'db-store', 'db-report', 'mem-store' or 'mem-report' + * @param $value + * Details of the string stored + * @param $mode + * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE + * @param $lang + * 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_batch_by_language($langcode, $finished = '_locale_batch_installer_finished') { - // Collect all files to import for all enabled modules and themes. - $files = array(); - $result = db_query("SELECT name, filename FROM {system} WHERE status = 1"); - while ($component = db_fetch_object($result)) { - // Collect all files for all components, names as $langcode.po or - // with names ending with $langcode.po. This allows for filenames - // like node-module.de.po to let translators use small files and - // be able to import in smaller chunks. - $files = array_merge($files, file_scan_directory(dirname($component->filename) .'/translations', '(^|\.)'. $langcode .'\.po$', array('.', '..', 'CVS'), 0, FALSE)); - } +function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NULL, $file = NULL, $group = 'default') { + static $report = array(0, 0, 0); + static $headerdone = FALSE; + static $strings = array(); - return _locale_batch_build($files, $finished); + switch ($op) { + // Return stored strings + case 'mem-report': + return $strings; + + // Store string in memory (only supports single strings) + case 'mem-store': + $strings[$value['msgid']] = $value['msgstr']; + return; + + // Called at end of import to inform the user + case 'db-report': + return array($headerdone, $report[0], $report[1], $report[2]); + + // Store the string we got in the database. + case 'db-store': + // We got header information. + if ($value['msgid'] == '') { + $header = _locale_import_parse_header($value['msgstr']); + + // Get the plural formula and update in database. + if (isset($header["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($header["Plural-Forms"], $file->filename)) { + list($nplurals, $plural) = $p; + db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", $nplurals, $plural, $lang); + } + else { + db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", 0, '', $lang); + } + $headerdone = TRUE; + } + + else { + // Some real string to import. + $comments = _locale_import_shorten_comments(empty($value['#']) ? array() : $value['#']); + + if (strpos($value['msgid'], "\0")) { + // This string has plural versions. + $english = explode("\0", $value['msgid'], 2); + $entries = array_keys($value['msgstr']); + for ($i = 3; $i <= count($entries); $i++) { + $english[] = $english[1]; + } + $translation = array_map('_locale_import_append_plural', $value['msgstr'], $entries); + $english = array_map('_locale_import_append_plural', $english, $entries); + foreach ($translation as $key => $trans) { + if ($key == 0) { + $plid = 0; + } + $plid = _locale_import_one_string_db($report, $lang, $english[$key], $trans, $group, $comments, $mode, $plid, $key); + } + } + + else { + // A simple string to import. + $english = $value['msgid']; + $translation = $value['msgstr']; + _locale_import_one_string_db($report, $lang, $english, $translation, $group, $comments, $mode); + } + } + } // end of db-store operation } + /** - * Prepare a batch to run when installing modules or enabling themes. - * This batch will import translations for the newly added components - * in all the languages already set up on the site. + * Import one string into the database. * - * @param $components - * An array of component (theme and/or module) names to import - * translations for. - * @param $finished - * Optional finished callback for the batch. + * @param $report + * Report array summarizing the number of changes done in the form: + * array(inserts, updates, deletes). + * @param $langcode + * Language code to import string into. + * @param $source + * Source string. + * @param $translation + * Translation to language specified in $langcode. + * @param $textgroup + * Name of textgroup to store translation in. + * @param $location + * Location value to save with source string. + * @param $mode + * Import mode to use, LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE. + * @param $plid + * Optional plural ID to use. + * @param $plural + * Optional plural value to use. + * @return + * The string ID of the existing string modified or the new string added. */ -function locale_batch_by_component($components, $finished = '_locale_batch_system_finished') { - $files = array(); - $languages = language_list('enabled'); - unset($languages[1]['en']); - if (count($languages[1])) { - $language_list = join('|', array_keys($languages[1])); - // Collect all files to import for all $components. - $result = db_query("SELECT name, filename FROM {system} WHERE status = 1"); - while ($component = db_fetch_object($result)) { - if (in_array($component->name, $components)) { - // Collect all files for this component in all enabled languages, named - // as $langcode.po or with names ending with $langcode.po. This allows - // for filenames like node-module.de.po to let translators use small - // files and be able to import in smaller chunks. - $files = array_merge($files, file_scan_directory(dirname($component->filename) .'/translations', '(^|\.)('. $language_list .')\.po$', array('.', '..', 'CVS'), 0, FALSE)); +function _locale_import_one_string_db(&$report, $langcode, $source, $translation, $textgroup, $location, $mode, $plid = NULL, $plural = NULL) { + $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup)); + + if (!empty($translation)) { + if ($lid) { + // We have this source string saved already. + db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $location, $lid); + $exists = (bool) db_result(db_query("SELECT lid FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $langcode)); + if (!$exists) { + // No translation in this language. + db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural); + $report[0]++; + } + else if ($mode == LOCALE_IMPORT_OVERWRITE) { + // Translation exists, only overwrite if instructed. + db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE language = '%s' AND lid = %d", $translation, $plid, $plural, $langcode, $lid); + $report[1]++; } } - return _locale_batch_build($files, $finished); + else { + // No such source string in the database yet. + db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', '%s')", $location, $source, $textgroup); + $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup)); + db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural); + $report[0]++; + } } - return FALSE; + elseif ($mode == LOCALE_IMPORT_OVERWRITE) { + // Empty translation, remove existing if instructed. + db_query("DELETE FROM {locales_target} WHERE language = '%s' AND lid = %d AND plid = %d AND plural = %d", $translation, $langcode, $lid, $plid, $plural); + $report[2]++; + } + + return $lid; } /** - * Build a locale batch from an array of files. + * Modify a string to contain proper count indices * - * @param $files - * Array of files to import - * @param $finished - * Optional finished callback for the batch. - * @return - * A batch structure + * This is a callback function used via array_map() + * + * @param $entry + * An array element + * @param $key + * Index of the array element */ -function _locale_batch_build($files, $finished = NULL) { - $t = get_t(); - if (count($files)) { - $operations = array(); - foreach ($files as $file) { - // We call _locale_batch_import for every batch operation. - $operations[] = array('_locale_batch_import', array($file->filename)); } - $batch = array( - 'operations' => $operations, - 'title' => $t('Importing interface translations'), - 'init_message' => $t('Starting import'), - 'error_message' => $t('Error importing interface translations'), - 'file' => './includes/locale.inc', - ); - if (isset($finished)) { - $batch['finished'] = $finished; - } - return $batch; +function _locale_import_append_plural($entry, $key) { + // No modifications for 0, 1 + if ($key == 0 || $key == 1) { + return $entry; } - return FALSE; + + // First remove any possibly false indices, then add new ones + $entry = preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); + return preg_replace('/(@count)/', "\\1[$key]", $entry); } /** - * Perform interface translation import as a batch step. + * Parses a string in quotes * - * @param $filepath - * Path to a file to import. - * @param $results - * Contains a list of files imported. + * @param $string + * A string specified with enclosing quotes + * @return + * The string parsed from inside the quotes */ -function _locale_batch_import($filepath, &$context) { - // The filename is either {langcode}.po or {prefix}.{langcode}.po, so - // we can extract the language code to use for the import from the end. - if (preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $langcode)) { - $file = (object) array('filename' => basename($filepath), 'filepath' => $filepath); - _locale_import_read_po('db-store', $file, LOCALE_IMPORT_KEEP, $langcode[2]); - $context['results'][] = $filepath; +function _locale_import_parse_quoted($string) { + if (substr($string, 0, 1) != substr($string, -1, 1)) { + return FALSE; // Start and end quotes must be the same + } + $quote = substr($string, 0, 1); + $string = substr($string, 1, -1); + if ($quote == '"') { // Double quotes: strip slashes + return stripcslashes($string); + } + elseif ($quote == "'") { // Simple quote: return as-is + return $string; + } + else { + return FALSE; // Unrecognized quote } } /** - * Finished callback of system page locale import batch. - * Inform the user of translation files imported. + * Generate a short, one string version of the passed comment array + * + * @param $comment + * An array of strings containing a comment + * @return + * Short one string version of the comment */ -function _locale_batch_system_finished($success, $results) { - if ($success) { - drupal_set_message(format_plural(count($results), 'One translation file imported for the newly installed modules.', '@count translation files imported for the newly installed modules.')); +function _locale_import_shorten_comments($comment) { + $comm = ''; + while (count($comment)) { + $test = $comm . substr(array_shift($comment), 1) .', '; + if (strlen($comm) < 130) { + $comm = $test; + } + else { + break; + } } + return substr($comm, 0, -2); } /** - * Finished callback of language addition locale import batch. - * Inform the user of translation files imported. + * Sets an error message occurred during locale file parsing. + * + * @param $message + * The message to be translated + * @param $file + * Drupal file object corresponding to the PO file to import + * @param $lineno + * An optional line number argument */ -function _locale_batch_language_finished($success, $results) { - if ($success) { - drupal_set_message(format_plural(count($results), 'One translation file imported for the enabled modules.', '@count translation files imported for the enabled modules.')); +function _locale_import_message($message, $file, $lineno = NULL) { + $vars = array('%filename' => $file->filename); + if (isset($lineno)) { + $vars['%line'] = $lineno; } + $t = get_t(); + drupal_set_message($t($message, $vars), 'error'); } -/** - * Finished callback of installer locale import batch. - * Advance installer task to the finished screen. - */ -function _locale_batch_installer_finished($success, $results) { - variable_set('install_task', 'finished'); -} /** - * @} End of "locale-autoimport" - */ + * @} End of "locale-api-import" + */ \ No newline at end of file Index: modules/locale/locale.admin.inc =================================================================== RCS file: modules/locale/locale.admin.inc diff -N modules/locale/locale.admin.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/locale/locale.admin.inc 29 Oct 2007 02:07:13 -0000 @@ -0,0 +1,1896 @@ + TRUE); + foreach ($languages as $langcode => $language) { + + $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 == LANGUAGE_RTL ? 'Right to left' : 'Left to right')); + } + $form['enabled'] = array('#type' => 'checkboxes', + '#options' => $options, + '#default_value' => $enabled, + ); + $form['site_default'] = array('#type' => 'radios', + '#options' => $options, + '#default_value' => language_default('language'), + ); + $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); + $form['#theme'] = 'locale_languages_overview_form'; + + return $form; +} + +/** + * Theme the language overview form. + */ +function theme_locale_languages_overview_form($form) { + include_once('includes/locale.inc.'); + + $default = language_default(); + foreach ($form['name'] as $key => $element) { + // Do not take form control structures. + if (is_array($element) && element_child($key)) { + // Disable checkbox for the default language, because it cannot be disabled. + if ($key == $default->language) { + $form['enabled'][$key]['#attributes']['disabled'] = TRUE; + } + $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'))); + $output = theme('table', $header, $rows); + $output .= drupal_render($form); + + return $output; +} + +/** + * Process language overview form submissions, updating existing languages. + */ +function locale_languages_overview_form_submit($form, &$form_state) { + $languages = language_list(); + $default = language_default(); + $enabled_count = 0; + foreach ($languages as $langcode => $language) { + if ($form_state['values']['site_default'] == $langcode || $default->language == $langcode) { + // Automatically enable the default language and the language + // which was default previously (because we will not get the + // value from that disabled checkox). + $form_state['values']['enabled'][$langcode] = 1; + } + if ($form_state['values']['enabled'][$langcode]) { + $enabled_count++; + $language->enabled = 1; + } + else { + $language->enabled = 0; + } + $language->weight = $form_state['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_state['values']['site_default']]); + variable_set('language_count', $enabled_count); + + // Changing the language settings impacts the interface. + cache_clear_all('*', 'cache_page', TRUE); + + $form_state['redirect'] = 'admin/settings/language'; + return; +} +/** + * @} 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() { + include_once('includes/locale.inc.'); + + $output = drupal_get_form('locale_languages_predefined_form'); + $output .= drupal_get_form('locale_languages_custom_form'); + return $output; +} + +/** + * Predefined language setup form. + */ +function locale_languages_predefined_form() { + $predefined = _locale_prepare_predefined_list(); + $form = array(); + $form['language list'] = array('#type' => 'fieldset', + '#title' => t('Predefined language'), + '#collapsible' => TRUE, + ); + $form['language list']['langcode'] = array('#type' => 'select', + '#title' => t('Language name'), + '#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_languages_custom_form() { + $form = array(); + $form['custom language'] = array('#type' => 'fieldset', + '#title' => t('Custom language'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + _locale_languages_common_controls($form['custom language']); + $form['custom language']['submit'] = array( + '#type' => 'submit', + '#value' => t('Add custom language') + ); + // Reuse the validation and submit functions of the predefined language setup form. + $form['#submit'][] = 'locale_languages_predefined_form_submit'; + $form['#validate'][] = 'locale_languages_predefined_form_validate'; + return $form; +} + +/** + * Editing screen for a particular language. + * + * @param $langcode + * Language code of the language to edit. + */ +function locale_languages_edit_form(&$form_state, $langcode) { + include_once('includes/locale.inc.'); + + if ($language = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $langcode))) { + $form = array(); + _locale_languages_common_controls($form, $language); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save language') + ); + $form['#submit'][] = 'locale_languages_edit_form_submit'; + $form['#validate'][] = 'locale_languages_edit_form_validate'; + return $form; + } + else { + drupal_not_found(); + } +} + +/** + * 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_languages_common_controls(&$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 RFC 4646 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['name'] = 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['native'] = 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(LANGUAGE_LTR => t('Left to right'), LANGUAGE_RTL => t('Right to left')) + ); + return $form; +} + +/** + * Validate the language addition form. + */ +function locale_languages_predefined_form_validate($form, &$form_state) { + $langcode = $form_state['values']['langcode']; + + if ($duplicate = db_result(db_query("SELECT COUNT(*) FROM {languages} WHERE language = '%s'", $langcode)) != 0) { + form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_state['values']['name'], '%code' => $langcode))); + } + + if (!isset($form_state['values']['name'])) { + // Predefined language selection. + $predefined = _locale_get_predefined_list(); + 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_languages_edit_form_validate($form, $form_state); + } +} + +/** + * Process the language addition form submission. + */ +function locale_languages_predefined_form_submit($form, &$form_state) { + $langcode = $form_state['values']['langcode']; + if (isset($form_state['values']['name'])) { + // Custom language form. + locale_add_language($langcode, $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['direction'], $form_state['values']['domain'], $form_state['values']['prefix']); + 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($form_state['values']['name']), '@locale-help' => url('admin/help/locale')))); + } + else { + // Predefined language selection. + $predefined = _locale_get_predefined_list(); + locale_add_language($langcode); + 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($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale')))); + } + + // See if we have language files to import for the newly added + // language, collect and import them. + if ($batch = locale_batch_by_language($langcode, '_locale_batch_language_finished')) { + batch_set($batch); + } + + $form_state['redirect'] = 'admin/settings/language'; + return; +} + +/** + * Validate the language editing form. Reused for custom language addition too. + */ +function locale_languages_edit_form_validate($form, &$form_state) { + if (!empty($form_state['values']['domain']) && !empty($form_state['values']['prefix'])) { + form_set_error('prefix', t('Domain and path prefix values should not be set at the same time.')); + } + if (!empty($form_state['values']['domain']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE domain = '%s' AND language != '%s'", $form_state['values']['domain'], $form_state['values']['langcode']))) { + form_set_error('domain', t('The domain (%domain) is already tied to a language (%language).', array('%domain' => $form_state['values']['domain'], '%language' => $duplicate->language))); + } + if (empty($form_state['values']['prefix']) && language_default('language') != $form_state['values']['langcode'] && empty($form_state['values']['domain'])) { + form_set_error('prefix', t('Only the default language can have both the domain and prefix empty.')); + } + if (!empty($form_state['values']['prefix']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE prefix = '%s' AND language != '%s'", $form_state['values']['prefix'], $form_state['values']['langcode']))) { + form_set_error('prefix', t('The prefix (%prefix) is already tied to a language (%language).', array('%prefix' => $form_state['values']['prefix'], '%language' => $duplicate->language))); + } +} + +/** + * Process the language editing form submission. + */ +function locale_languages_edit_form_submit($form, &$form_state) { + db_query("UPDATE {languages} SET name = '%s', native = '%s', domain = '%s', prefix = '%s', direction = %d WHERE language = '%s'", $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['domain'], $form_state['values']['prefix'], $form_state['values']['direction'], $form_state['values']['langcode']); + $default = language_default(); + if ($default->language == $form_state['values']['langcode']) { + $properties = array('name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight'); + foreach ($properties as $keyname) { + if (isset($form_state['values'][$keyname])) { + $default->$keyname = $form_state['values'][$keyname]; + } + } + variable_set('language_default', $default); + } + $form_state['redirect'] = 'admin/settings/language'; + return; +} +/** + * @} 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(&$form_state, $langcode) { + include_once('includes/locale.inc.'); + + // 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'); + } + + if (language_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, &$form_state) { + $languages = language_list(); + if (isset($languages[$form_state['values']['langcode']])) { + db_query("DELETE FROM {languages} WHERE language = '%s'", $form_state['values']['langcode']); + db_query("DELETE FROM {locales_target} WHERE language = '%s'", $form_state['values']['langcode']); + db_query("UPDATE {node} SET language = '' WHERE language = '%s'", $form_state['values']['langcode']); + $variables = array('%locale' => $languages[$form_state['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); + + $form_state['redirect'] = 'admin/settings/language'; + return; +} +/** + * @} End of "locale-language-add-edit" + */ + +/** + * @defgroup locale-languages-negotiation Language negotiation options screen + * @{ + */ + +/** + * Setting for language negotiation options + */ +function locale_languages_configure_form() { + include_once('includes/locale.inc.'); + + $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('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', + '#value' => t('Save settings') + ); + return $form; +} + +/** + * Submit function for language negotiation settings. + */ +function locale_languages_configure_form_submit($form, &$form_state) { + variable_set('language_negotiation', $form_state['values']['language_negotiation']); + drupal_set_message(t('Language negotiation configuration saved.')); + $form_state['redirect'] = 'admin/settings/language'; + return; +} +/** + * @} End of "locale-languages-negotiation" + */ + +/** + * @defgroup locale-translate-overview Translation overview screen. + * @{ + */ + +/** + * Overview screen for translations. + */ +function locale_translate_overview_screen() { + include_once('includes/locale.inc.'); + + $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 INNER JOIN {locales_target} t ON s.lid = t.lid 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() { + include_once('includes/locale.inc.'); + $output = _locale_translate_seek(); + $output .= drupal_get_form('locale_translate_seek_form'); + return $output; +} + +/** + * 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('Both translated and untranslated strings'), '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_translate_import_form() { + // Get all languages, except English + $names = locale_language_list('name', TRUE); + unset($names['en']); + + if (!count($names)) { + $languages = _locale_prepare_predefined_list(); + $default = array_shift(array_keys($languages)); + } + else { + $languages = array( + t('Already added languages') => $names, + t('Languages not yet added') => _locale_prepare_predefined_list() + ); + $default = array_shift(array_keys($names)); + } + + $form = array(); + $form['import'] = array('#type' => 'fieldset', + '#title' => t('Import translation'), + ); + $form['import']['file'] = array('#type' => 'file', + '#title' => t('Language file'), + '#size' => 50, + '#description' => t('A gettext Portable Object (.po) file.'), + ); + $form['import']['langcode'] = array('#type' => 'select', + '#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, 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'), + '#default_value' => 'overwrite', + '#options' => array(LOCALE_IMPORT_OVERWRITE => t('Strings in the uploaded file replace existing ones, new ones are added'), LOCALE_IMPORT_KEEP => t('Existing strings are kept, only new strings are added')), + ); + $form['import']['submit'] = array('#type' => 'submit', '#value' => t('Import')); + $form['#attributes']['enctype'] = 'multipart/form-data'; + + return $form; +} + +/** + * Process the locale import form submission. + */ +function locale_translate_import_form_submit($form, &$form_state) { + // Ensure we have the file uploaded + if ($file = file_save_upload('file')) { + + // Add language, if not yet supported + $languages = language_list('language', TRUE); + $langcode = $form_state['values']['langcode']; + if (!isset($languages[$langcode])) { + $predefined = _locale_get_predefined_list(); + locale_add_language($langcode); + drupal_set_message(t('The language %language has been created.', array('%language' => t($predefined[$langcode][0])))); + } + + // Now import strings into the language + if ($ret = _locale_import_po($file, $langcode, $form_state['values']['mode'], $form_state['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); + } + } + else { + drupal_set_message(t('File to import not found.'), 'error'); + return 'admin/build/translate/import'; + } + + $form_state['redirect'] = 'admin/build/translate'; + return; +} +/** + * @} 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; +} + +/** + * Form to export PO files for the languages provided. + * + * @param $names + * An associate array with localized language names + */ +function locale_translate_export_po_form(&$form_state, $names) { + $form['export'] = array('#type' => 'fieldset', + '#title' => t('Export translation'), + '#collapsible' => TRUE, + ); + $form['export']['langcode'] = array('#type' => 'select', + '#title' => t('Language name'), + '#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; +} + +/** + * 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 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')); + // Reuse PO export submission callback. + $form['#submit'][] = 'locale_translate_export_po_form_submit'; + $form['#validate'][] = 'locale_translate_export_po_form_validate'; + return $form; +} + +/** + * Process a translation (or template) export form submission. + */ +function locale_translate_export_po_form_submit($form, &$form_state) { + // If template is required, language code is not given. + $language = NULL; + if (isset($form_state['values']['langcode'])) { + $languages = language_list(); + $language = $languages[$form_state['values']['langcode']]; + } + _locale_export_po($language, _locale_export_po_generate($language, _locale_export_get_strings($language, $form_state['values']['group']))); +} +/** + * @} End of "locale-translate-export" + */ + +/** + * @defgroup locale-translate-edit Translation text editing + * @{ + */ + +/** + * User interface for string editing. + */ +function locale_translate_edit_form(&$form_state, $lid) { + include_once('includes/locale.inc.'); + + // Fetch source string, if possible. + $source = db_fetch_object(db_query('SELECT source, textgroup, location FROM {locales_source} WHERE lid = %d', $lid)); + if (!$source) { + drupal_set_message(t('String not found.'), 'error'); + drupal_goto('admin/build/translate/search'); + } + + // Add original text to the top and some values for form altering. + $form = array( + 'original' => array( + '#type' => 'item', + '#title' => t('Original text'), + '#value' => check_plain(wordwrap($source->source, 0)), + ), + 'lid' => array( + '#type' => 'value', + '#value' => $lid + ), + 'textgroup' => array( + '#type' => 'value', + '#value' => $source->textgroup, + ), + 'location' => array( + '#type' => 'value', + '#value' => $source->location + ), + ); + + // Include default form controls with empty values for all languages. + // This ensures that the languages are always in the same order in forms. + $languages = language_list(); + $default = language_default(); + // We don't need the default language value, that value is in $source. + $omit = $source->textgroup == 'default' ? 'en' : $default->language; + unset($languages[($omit)]); + $form['translations'] = array('#tree' => TRUE); + // Approximate the number of rows to use in the default textarea. + $rows = min(ceil(str_word_count($source->source) / 12), 10); + foreach ($languages as $langcode => $language) { + $form['translations'][$langcode] = array( + '#type' => 'textarea', + '#title' => t($language->name), + '#rows' => $rows, + '#default_value' => '', + ); + } + + // Fetch translations and fill in default values in the form. + $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = %d AND language != '%s'", $lid, $omit); + while ($translation = db_fetch_object($result)) { + $form['translations'][$translation->language]['#default_value'] = $translation->translation; + } + + $form['submit'] = array('#type' => 'submit', '#value' => t('Save translations')); + return $form; +} + +/** + * Process string editing form submissions. + * Saves all translations of one string submitted from a form. + */ +function locale_translate_edit_form_submit($form, &$form_state) { + $lid = $form_state['values']['lid']; + foreach ($form_state['values']['translations'] as $key => $value) { + $translation = db_result(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key)); + if (!empty($value)) { + // Only update or insert if we have a value to use. + if (!empty($translation)) { + 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, language) VALUES (%d, '%s', '%s')", $lid, $value, $key); + } + } + elseif (!empty($translation)) { + // Empty translation entered: remove existing entry from database. + db_query("DELETE FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key); + } + + // Refresh the JS file for this language. + _locale_rebuild_js($key); + } + + drupal_set_message(t('The string has been saved.')); + + // Clear locale cache. + cache_clear_all('locale:', 'cache', TRUE); + + $form_state['redirect'] = 'admin/build/translate/search'; + return; +} +/** + * @} End of "locale-translate-edit" + */ + +/** + * @defgroup locale-translate-delete Translation delete interface. + * @{ + */ + +/** + * Delete a language string. + */ +function locale_translate_delete($lid) { + include_once('includes/locale.inc.'); + + $langcode = db_result(db_query('SELECT language FROM {locales_target} WHERE lid = %d', $lid)); + db_query('DELETE FROM {locales_source} WHERE lid = %d', $lid); + db_query('DELETE FROM {locales_target} WHERE lid = %d', $lid); + if ($langcode) { + _locale_rebuild_js($langcode); + } + cache_clear_all('locale:', 'cache', TRUE); + drupal_set_message(t('The string has been removed.')); + drupal_goto('admin/build/translate/search'); +} +/** + * @} End of "locale-translate-delete" + */ + +/** + * @defgroup locale-api-import Translation import API. + * @{ + */ + +/** + * Parses Gettext Portable Object file information and inserts into database + * + * @param $file + * Drupal file object corresponding to the PO file to import + * @param $langcode + * Language code + * @param $mode + * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE + * @param $group + * Text group to import PO file into (eg. 'default' for interface translations) + */ +function _locale_import_po($file, $langcode, $mode, $group = NULL) { + // If not in 'safe mode', increase the maximum execution time. + if (!ini_get('safe_mode')) { + set_time_limit(240); + } + + // Check if we have the language already in the database. + if (!db_fetch_object(db_query("SELECT language FROM {languages} WHERE language = '%s'", $langcode))) { + drupal_set_message(t('The language selected for import is not supported.'), 'error'); + return FALSE; + } + + // Get strings from file (returns on failure after a partial import, or on success) + $status = _locale_import_read_po('db-store', $file, $mode, $langcode, $group); + if ($status === FALSE) { + // Error messages are set in _locale_import_read_po(). + return FALSE; + } + + // Get status information on import process. + list($headerdone, $additions, $updates, $deletes) = _locale_import_one_string('db-report'); + + if (!$headerdone) { + drupal_set_message(t('The translation file %filename appears to have a missing or malformed header.', array('%filename' => $file->filename)), 'error'); + } + + // Clear cache and refresh JavaScript translations. + _locale_rebuild_js($langcode); + cache_clear_all('locale:', 'cache', TRUE); + + // Rebuild the menu, strings may have changed. + menu_rebuild(); + + drupal_set_message(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => $additions, '%update' => $updates, '%delete' => $deletes))); + watchdog('locale', 'Imported %file into %locale: %number new strings added, %update updated and %delete removed.', array('%file' => $file->filename, '%locale' => $langcode, '%number' => $additions, '%update' => $updates, '%delete' => $deletes)); + return TRUE; +} + +/** + * Parses a Gettext Portable Object file header + * + * @param $header + * A string containing the complete header + * @return + * An associative array of key-value pairs + */ +function _locale_import_parse_header($header) { + $header_parsed = array(); + $lines = array_map('trim', explode("\n", $header)); + foreach ($lines as $line) { + if ($line) { + list($tag, $contents) = explode(":", $line, 2); + $header_parsed[trim($tag)] = trim($contents); + } + } + return $header_parsed; +} + +/** + * Parses a Plural-Forms entry from a Gettext Portable Object file header + * + * @param $pluralforms + * A string containing the Plural-Forms entry + * @param $filename + * A string containing the filename + * @return + * An array containing the number of plurals and a + * formula in PHP for computing the plural form + */ +function _locale_import_parse_plural_forms($pluralforms, $filename) { + // First, delete all whitespace + $pluralforms = strtr($pluralforms, array(" " => "", "\t" => "")); + + // Select the parts that define nplurals and plural + $nplurals = strstr($pluralforms, "nplurals="); + if (strpos($nplurals, ";")) { + $nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9); + } + else { + return FALSE; + } + $plural = strstr($pluralforms, "plural="); + if (strpos($plural, ";")) { + $plural = substr($plural, 7, strpos($plural, ";") - 7); + } + else { + return FALSE; + } + + // Get PHP version of the plural formula + $plural = _locale_import_parse_arithmetic($plural); + + if ($plural !== FALSE) { + return array($nplurals, $plural); + } + else { + drupal_set_message(t('The translation file %filename contains an error: the plural formula could not be parsed.', array('%filename' => $filename)), 'error'); + return FALSE; + } +} + +/** + * Parses and sanitizes an arithmetic formula into a PHP expression + * + * While parsing, we ensure, that the operators have the right + * precedence and associativity. + * + * @param $string + * A string containing the arithmetic formula + * @return + * The PHP version of the formula + */ +function _locale_import_parse_arithmetic($string) { + // Operator precedence table + $prec = array("(" => -1, ")" => -1, "?" => 1, ":" => 1, "||" => 3, "&&" => 4, "==" => 5, "!=" => 5, "<" => 6, ">" => 6, "<=" => 6, ">=" => 6, "+" => 7, "-" => 7, "*" => 8, "/" => 8, "%" => 8); + // Right associativity + $rasc = array("?" => 1, ":" => 1); + + $tokens = _locale_import_tokenize_formula($string); + + // Parse by converting into infix notation then back into postfix + $opstk = array(); + $elstk = array(); + + foreach ($tokens as $token) { + $ctok = $token; + + // Numbers and the $n variable are simply pushed into $elarr + if (is_numeric($token)) { + $elstk[] = $ctok; + } + elseif ($ctok == "n") { + $elstk[] = '$n'; + } + elseif ($ctok == "(") { + $opstk[] = $ctok; + } + elseif ($ctok == ")") { + $topop = array_pop($opstk); + while (isset($topop) && ($topop != "(")) { + $elstk[] = $topop; + $topop = array_pop($opstk); + } + } + elseif (!empty($prec[$ctok])) { + // 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 (isset($topop) && ($prec[$topop] >= $prec[$ctok]) && !(($prec[$topop] == $prec[$ctok]) && !empty($rasc[$topop]) && !empty($rasc[$ctok]))) { + $elstk[] = $topop; + $topop = array_pop($opstk); + } + if ($topop) { + $opstk[] = $topop; // Return element to top + } + $opstk[] = $ctok; // Parentheses are not needed + } + else { + return FALSE; + } + } + + // Flush operator stack + $topop = array_pop($opstk); + while ($topop != NULL) { + $elstk[] = $topop; + $topop = array_pop($opstk); + } + + // Now extract formula from stack + $prevsize = count($elstk) + 1; + while (count($elstk) < $prevsize) { + $prevsize = count($elstk); + for ($i = 2; $i < count($elstk); $i++) { + $op = $elstk[$i]; + if (!empty($prec[$op])) { + $f = ""; + if ($op == ":") { + $f = $elstk[$i - 2] ."):". $elstk[$i - 1] .")"; + } + elseif ($op == "?") { + $f = "(". $elstk[$i - 2] ."?(". $elstk[$i - 1]; + } + else { + $f = "(". $elstk[$i - 2] . $op . $elstk[$i - 1] .")"; + } + array_splice($elstk, $i - 2, 3, $f); + break; + } + } + } + + // If only one element is left, the number of operators is appropriate + if (count($elstk) == 1) { + return $elstk[0]; + } + else { + return FALSE; + } +} + +/** + * Backward compatible implementation of token_get_all() for formula parsing + * + * @param $string + * A string containing the arithmetic formula + * @return + * The PHP version of the formula + */ +function _locale_import_tokenize_formula($formula) { + $formula = str_replace(" ", "", $formula); + $tokens = array(); + for ($i = 0; $i < strlen($formula); $i++) { + if (is_numeric($formula[$i])) { + $num = $formula[$i]; + $j = $i + 1; + while ($j < strlen($formula) && is_numeric($formula[$j])) { + $num .= $formula[$j]; + $j++; + } + $i = $j - 1; + $tokens[] = $num; + } + elseif ($pos = strpos(" =<>!&|", $formula[$i])) { // We won't have a space + $next = $formula[$i + 1]; + switch ($pos) { + case 1: + case 2: + case 3: + case 4: + if ($next == '=') { + $tokens[] = $formula[$i] .'='; + $i++; + } + else { + $tokens[] = $formula[$i]; + } + break; + case 5: + if ($next == '&') { + $tokens[] = '&&'; + $i++; + } + else { + $tokens[] = $formula[$i]; + } + break; + case 6: + if ($next == '|') { + $tokens[] = '||'; + $i++; + } + else { + $tokens[] = $formula[$i]; + } + break; + } + } + else { + $tokens[] = $formula[$i]; + } + } + return $tokens; +} + +/** + * @} End of "locale-api-import" + */ + +/** + * This function checks all JavaScript files currently added via drupal_add_js() + * and invokes parsing if they have not yet been parsed for Drupal.t() calls. + */ +function _locale_update_js_files() { + global $language; + + $parsed = variable_get('javascript_parsed', array()); + + // The first three parameters are NULL in order to get an array with all + // scopes. This is necessary to prevent recreation of JS translation files + // when new files are added for example in the footer. + $javascript = drupal_add_js(NULL, NULL, NULL); + $files = FALSE; + + foreach ($javascript as $scope) { + foreach ($scope as $type => $data) { + if ($type != 'setting' && $type != 'inline') { + foreach ($data as $filepath => $info) { + $files = TRUE; + if (!in_array($filepath, $parsed)) { + _locale_parse_js_file($filepath); + watchdog('locale', 'Parsed JavaScript file %file.', array('%file' => $filepath)); + $parsed[] = $filepath; + } + } + } + } + } + + // Update the parsed files storage array. + variable_set('javascript_parsed', $parsed); + _locale_rebuild_js(); + + // Add the translation JavaScript file to the page. + if ($files && !empty($language->javascript)) { + drupal_add_js(file_create_path(variable_get('locale_js_directory', 'languages') .'/'. $language->language .'_'. $language->javascript .'.js'), 'core'); + } +} + +/** + * Parses a JavaScript file, extracts strings wrapped in Drupal.t() and + * Drupal.formatPlural() and inserts them into the database. + */ +function _locale_parse_js_file($filepath) { + global $language; + + // Load the JavaScript file. + $file = file_get_contents($filepath); + + // Match all calls to Drupal.t() in an array. + // Note: \s also matches newlines with the 's' modifier. + preg_match_all('~[^\w]Drupal\s*\.\s*t\s*\(\s*('. LOCALE_JS_STRING .')\s*[,\)]~s', $file, $t_matches); + + // Match all Drupal.formatPlural() calls in another array. + preg_match_all('~[^\w]Drupal\s*\.\s*formatPlural\s*\(\s*.+?\s*,\s*('. LOCALE_JS_STRING .')\s*,\s*((?:(?:\'(?:\\\\\'|[^\'])*@count(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*@count(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*[,\)]~s', $file, $plural_matches); + + // Loop through all matches and process them. + $all_matches = array_merge($plural_matches[1], $t_matches[1]); + foreach ($all_matches as $key => $string) { + $strings = array($string); + + // If there is also a plural version of this string, add it to the strings array. + if (isset($plural_matches[2][$key])) { + $strings[] = $plural_matches[2][$key]; + } + + foreach ($strings as $key => $string) { + // Remove the quotes and string concatenations from the string. + $string = implode('', preg_split('~(?location); + + if (!in_array($filepath, $locations)) { + $locations[] = $filepath; + $locations = implode('; ', $locations); + + // Save the new locations string to the database. + db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $locations, $source->lid); + } + } + else { + // We don't have the source string yet, thus we insert it into the database. + db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', 'default')", $filepath, $string); + } + } + } +} + +/** + * @defgroup locale-api-export Translation (template) export API. + * @{ + */ + +/** + * Generates a structured array of all strings with translations in + * $language, if given. This array can be used to generate an export + * of the string in the database. + * + * @param $language + * Language object 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_get_strings($language = NULL, $group = 'default') { + if (isset($language)) { + $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $language->language, $group); + } + else { + $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $group); + } + $strings = array(); + while ($child = db_fetch_object($result)) { + $string = array( + 'comment' => $child->location, + 'source' => $child->source, + 'translation' => isset($child->translation) ? $child->translation : '' + ); + if ($child->plid) { + // Has a parent lid. Since we process in the order of plids, + // we already have the parent in the array, so we can add the + // lid to the next plural version to it. This builds a linked + // list of plurals. + $string['child'] = TRUE; + $strings[$child->plid]['plural'] = $child->lid; + } + $strings[$child->lid] = $string; + } + return $strings; +} + +/** + * Generates the PO(T) file contents for given strings. + * + * @param $language + * Language object to generate the output for, or NULL if generating + * translation template. + * @param $strings + * Array of strings to export. See _locale_export_get_strings() + * on how it should be formatted. + * @param $header + * The header portion to use for the output file. Defaults + * are provided for PO and POT files. + */ +function _locale_export_po_generate($language = NULL, $strings = array(), $header = NULL) { + global $user; + + if (!isset($header)) { + if (isset($language)) { + $header = '# '. $language->name .' translation of '. variable_get('site_name', 'Drupal') ."\n"; + $header .= '# Generated by '. $user->name .' <'. $user->mail .">\n"; + $header .= "#\n"; + $header .= "msgid \"\"\n"; + $header .= "msgstr \"\"\n"; + $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n"; + $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; + $header .= "\"PO-Revision-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; + $header .= "\"Last-Translator: NAME \\n\"\n"; + $header .= "\"Language-Team: LANGUAGE \\n\"\n"; + $header .= "\"MIME-Version: 1.0\\n\"\n"; + $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n"; + $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; + if ($language->formula && $language->plurals) { + $header .= "\"Plural-Forms: nplurals=". $language->plurals ."; plural=". strtr($language->formula, array('$' => '')) .";\\n\"\n"; + } + } + else { + $header = "# LANGUAGE translation of PROJECT\n"; + $header .= "# Copyright (c) YEAR NAME \n"; + $header .= "#\n"; + $header .= "msgid \"\"\n"; + $header .= "msgstr \"\"\n"; + $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n"; + $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; + $header .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n"; + $header .= "\"Last-Translator: NAME \\n\"\n"; + $header .= "\"Language-Team: LANGUAGE \\n\"\n"; + $header .= "\"MIME-Version: 1.0\\n\"\n"; + $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n"; + $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; + $header .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n"; + } + } + + $output = $header ."\n"; + + foreach ($strings as $lid => $string) { + // Only process non-children, children are output below their parent. + if (!isset($string['child'])) { + if ($string['comment']) { + $output .= '#: '. $string['comment'] ."\n"; + } + $output .= 'msgid '. _locale_export_string($string['source']); + if (!empty($string['plural'])) { + $plural = $string['plural']; + $output .= 'msgid_plural '. _locale_export_string($strings[$plural]['source']); + if (isset($language)) { + $translation = $string['translation']; + for ($i = 0; $i < $language->plurals; $i++) { + $output .= 'msgstr['. $i .'] '. _locale_export_string($translation); + if ($plural) { + $translation = _locale_export_remove_plural($strings[$plural]['translation']); + $plural = isset($strings[$plural]['plural']) ? $strings[$plural]['plural'] : 0; + } + else { + $translation = ''; + } + } + } + else { + $output .= 'msgstr[0] ""'."\n"; + $output .= 'msgstr[1] ""'."\n"; + } + } + else { + $output .= 'msgstr '. _locale_export_string($string['translation']); + } + $output .= "\n"; + } + } + return $output; +} + +/** + * Write a generated PO or POT file to the output. + * + * @param $language + * Language object to generate the output for, or NULL if generating + * translation template. + * @param $output + * The PO(T) file to output as a string. See _locale_export_generate_po() + * on how it can be generated. + */ +function _locale_export_po($language = NULL, $output = NULL) { + // Log the export event. + if (isset($language)) { + $filename = $language->language .'.po'; + watchdog('locale', 'Exported %locale translation file: %filename.', array('%locale' => $language->name, '%filename' => $filename)); + } + else { + $filename = 'drupal.pot'; + watchdog('locale', 'Exported translation file: %filename.', array('%filename' => $filename)); + } + // Download the file fo the client. + header("Content-Disposition: attachment; filename=$filename"); + header("Content-Type: text/plain; charset=utf-8"); + print $output; + die(); +} + +/** + * Print out a string on multiple lines + */ +function _locale_export_string($str) { + $stri = addcslashes($str, "\0..\37\\\""); + $parts = array(); + + // Cut text into several lines + while ($stri != "") { + $i = strpos($stri, "\\n"); + if ($i === FALSE) { + $curstr = $stri; + $stri = ""; + } + else { + $curstr = substr($stri, 0, $i + 2); + $stri = substr($stri, $i + 2); + } + $curparts = explode("\n", _locale_export_wrap($curstr, 70)); + $parts = array_merge($parts, $curparts); + } + + // Multiline string + if (count($parts) > 1) { + return "\"\"\n\"". implode("\"\n\"", $parts) ."\"\n"; + } + // Single line string + elseif (count($parts) == 1) { + return "\"$parts[0]\"\n"; + } + // No translation + else { + return "\"\"\n"; + } +} + +/** + * Custom word wrapping for Portable Object (Template) files. + */ +function _locale_export_wrap($str, $len) { + $words = explode(' ', $str); + $ret = array(); + + $cur = ""; + $nstr = 1; + while (count($words)) { + $word = array_shift($words); + if ($nstr) { + $cur = $word; + $nstr = 0; + } + elseif (strlen("$cur $word") > $len) { + $ret[] = $cur ." "; + $cur = $word; + } + else { + $cur = "$cur $word"; + } + } + $ret[] = $cur; + + return implode("\n", $ret); +} + +/** + * Removes plural index information from a string + */ +function _locale_export_remove_plural($entry) { + return preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); +} +/** + * @} End of "locale-api-export" + */ + +/** + * @defgroup locale-api-seek String search functions. + * @{ + */ + +/** + * Perform a string search and display results in a table + */ +function _locale_translate_seek() { + $output = ''; + + // We have at least one criterion to match + if ($query = _locale_translate_seek_query()) { + $join = "SELECT s.source, s.location, s.lid, s.textgroup, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid "; + + $arguments = array(); + $limit_language = FALSE; + // Compute LIKE section + switch ($query['translation']) { + case 'translated': + $where = "WHERE (t.translation LIKE '%%%s%%')"; + $orderby = "ORDER BY t.translation"; + $arguments[] = $query['string']; + break; + case 'untranslated': + $where = "WHERE (s.source LIKE '%%%s%%' AND t.translation IS NULL)"; + $orderby = "ORDER BY s.source"; + $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']; + 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%%' $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 $grouplimit $orderby"; + break; + // Some different language + default: + $sql = "$join AND t.language = '%s' $where $grouplimit $orderby"; + array_unshift($arguments, $query['language']); + // Don't show translation flags for other languages, we can't see them with this search. + $limit_language = $query['language']; + } + + $result = pager_query($sql, 50, 0, NULL, $arguments); + + $groups = module_invoke_all('locale', 'groups'); + $header = array(t('Text group'), t('String'), ($limit_language) ? t('Language') : t('Languages'), array('data' => t('Operations'), 'colspan' => '2')); + $arr = array(); + while ($locale = db_fetch_object($result)) { + $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( + $value['group'], + array('data' => check_plain(truncate_utf8($value['source'], 150, FALSE, TRUE)) .'
'. $value['location'] .''), + array('data' => _locale_translate_language_list($value['languages'], $limit_language), '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)) { + $output .= theme('table', $header, $rows); + if ($pager = theme('pager', NULL, 50)) { + $output .= $pager; + } + } + else { + $output .= t('No strings found for your search.'); + } + } + + return $output; +} + +/** + * 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; +} + +/** + * (Re-)Creates the JavaScript translation file for a language. + * + * @param $language + * The language, the translation file should be (re)created for. + */ +function _locale_rebuild_js($langcode = NULL) { + if (!isset($langcode)) { + global $language; + } + else { + // Get information about the locale. + $languages = language_list(); + $language = $languages[$langcode]; + } + + // Construct the array for JavaScript translations. + // We sort on plural so that we have all plural forms before singular forms. + $result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.location LIKE '%%.js%%' AND s.textgroup = 'default' ORDER BY t.plural DESC", $language->language); + + $translations = $plurals = array(); + while ($data = db_fetch_object($result)) { + // Only add this to the translations array when there is actually a translation. + if (!empty($data->translation)) { + if ($data->plural) { + // When the translation is a plural form, first add it to another array and + // wait for the singular (parent) translation. + if (!isset($plurals[$data->plid])) { + $plurals[$data->plid] = array($data->plural => $data->translation); + } + else { + $plurals[$data->plid] += array($data->plural => $data->translation); + } + } + elseif (isset($plurals[$data->lid])) { + // There are plural translations for this translation, so get them from + // the plurals array and add them to the final translations array. + $translations[$data->source] = array($data->plural => $data->translation) + $plurals[$data->lid]; + unset($plurals[$data->lid]); + } + else { + // There are no plural forms for this translation, so just add it to + // the translations array. + $translations[$data->source] = $data->translation; + } + } + } + + // Only operate when there are translations. + if (!empty($translations)) { + // Construct the JavaScript file. + $data = "Drupal.locale = { "; + + if (!empty($language->formula)) { + $data .= "'pluralFormula': function(\$n) { return Number({$language->formula}); }, "; + } + + $data .= "'strings': ". drupal_to_js($translations) ." };"; + + // Construct the directory where JS translation files are stored. + // There is (on purpose) no front end to edit that variable. + $dir = file_create_path(variable_get('locale_js_directory', 'languages')); + + // Only create a new file if the content has changed. + $data_hash = md5($data); + if ($language->javascript != $data_hash) { + if (!empty($language->javascript)) { + // We are recreating the new file, so delete the old one. + file_delete(file_create_path($dir .'/'. $language->language .'_'. $language->javascript .'.js')); + $language->javascript = ''; + } + else { + // The directory might not exist when there has not been a translation + // file before, so create it. + file_check_directory($dir, TRUE); + } + + // The file name of the new JavaScript translation file. + $dest = $dir .'/'. $language->language .'_'. $data_hash .'.js'; + $filepath = file_save_data($data, $dest); + + // Update the global $language object. + $language->javascript = $filepath ? $data_hash : ''; + + // Save the new JavaScript hash. + db_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $language->javascript, $language->language); + + // Update the default language variable if the default language has been altered. + // This is necessary to keep the variable consistent with the database + // version of the language and to prevent checking against an outdated hash. + $default_langcode = language_default('language'); + if ($default_langcode == $language->language) { + $default = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $default_langcode)); + variable_set('language_default', $default); + } + + // Only report updated or creation when there is actually a file created. + if ($filepath) { + // There has already been a translation file before. + if (!empty($language->javascript)) { + watchdog('locale', 'Updated JavaScript translation file for the language %language.', array('%language' => t($language->name))); + } + else { + watchdog('locale', 'Created JavaScript translation file for the language %language.', array('%language' => t($language->name))); + } + } + else { + watchdog('locale', 'An error occurred during creation of the JavaScript translation file for the language %language.', array('%language' => t($language->name))); + } + } + } + + // There are no strings for JavaScript translation. However, if there is a file, + // delete it and reset the database. + elseif (!empty($language->javascript)) { + // Delete the old JavaScript file + file_delete(file_create_path(variable_get('locale_js_directory', 'languages') .'/'. $language->language .'_'. $language->javascript .'.js')); + db_query("UPDATE {languages} SET javascript = '' WHERE language = '%s'", $language->language); + watchdog('locale', 'Deleted JavaScript translation file for the locale %language.', array('%language' => t($language->name))); + } +} + +/** + * List languages in search result table + */ +function _locale_translate_language_list($translation, $limit_language) { + // 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) { + if (!$limit_language || $limit_language == $langcode) { + $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 + */ +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) > 1) { + $tname = t($value[0]); + $predefined[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])"; + } + else { + $predefined[$key] = t($value[0]); + } + } + asort($predefined); + return $predefined; +} + +/** + * @defgroup locale-autoimport Automatic interface translation import + * @{ + */ + +/** + * Prepare a batch to run when installing modules or enabling themes. + * This batch will import translations for the newly added components + * in all the languages already set up on the site. + * + * @param $components + * An array of component (theme and/or module) names to import + * translations for. + * @param $finished + * Optional finished callback for the batch. + */ +function locale_batch_by_component($components, $finished = '_locale_batch_system_finished') { + $files = array(); + $languages = language_list('enabled'); + unset($languages[1]['en']); + if (count($languages[1])) { + $language_list = join('|', array_keys($languages[1])); + // Collect all files to import for all $components. + $result = db_query("SELECT name, filename FROM {system} WHERE status = 1"); + while ($component = db_fetch_object($result)) { + if (in_array($component->name, $components)) { + // Collect all files for this component in all enabled languages, named + // as $langcode.po or with names ending with $langcode.po. This allows + // for filenames like node-module.de.po to let translators use small + // files and be able to import in smaller chunks. + $files = array_merge($files, file_scan_directory(dirname($component->filename) .'/translations', '(^|\.)('. $language_list .')\.po$', array('.', '..', 'CVS'), 0, FALSE)); + } + } + return _locale_batch_build($files, $finished); + } + return FALSE; +} + +/** + * Perform interface translation import as a batch step. + * + * @param $filepath + * Path to a file to import. + * @param $results + * Contains a list of files imported. + */ +function _locale_batch_import($filepath, &$context) { + // The filename is either {langcode}.po or {prefix}.{langcode}.po, so + // we can extract the language code to use for the import from the end. + if (preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $langcode)) { + $file = (object) array('filename' => basename($filepath), 'filepath' => $filepath); + _locale_import_read_po('db-store', $file, LOCALE_IMPORT_KEEP, $langcode[2]); + $context['results'][] = $filepath; + } +} + +/** + * Finished callback of system page locale import batch. + * Inform the user of translation files imported. + */ +function _locale_batch_system_finished($success, $results) { + if ($success) { + drupal_set_message(format_plural(count($results), 'One translation file imported for the newly installed modules.', '@count translation files imported for the newly installed modules.')); + } +} + +/** + * Finished callback of language addition locale import batch. + * Inform the user of translation files imported. + */ +function _locale_batch_language_finished($success, $results) { + if ($success) { + drupal_set_message(format_plural(count($results), 'One translation file imported for the enabled modules.', '@count translation files imported for the enabled modules.')); + } +} + +/** + * Finished callback of installer locale import batch. + * Advance installer task to the finished screen. + */ +function _locale_batch_installer_finished($success, $results) { + variable_set('install_task', 'finished'); +} + +/** + * @} End of "locale-autoimport" + */ Index: modules/locale/locale.module =================================================================== RCS file: /cvs/drupal/drupal/modules/locale/locale.module,v retrieving revision 1.197 diff -u -p -r1.197 locale.module --- modules/locale/locale.module 27 Oct 2007 14:03:36 -0000 1.197 +++ modules/locale/locale.module 29 Oct 2007 02:07:13 -0000 @@ -73,103 +73,102 @@ function locale_menu() { $items['admin/settings/language'] = array( 'title' => 'Languages', 'description' => 'Configure languages for content and the user interface.', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('drupal_get_form', 'locale_languages_overview_form'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_languages_overview_form'), 'access arguments' => array('administer languages'), + 'file' => 'locale.admin.inc', ); $items['admin/settings/language/overview'] = array( 'title' => 'List', 'weight' => 0, 'type' => MENU_DEFAULT_LOCAL_TASK, + 'file' => 'locale.admin.inc', ); $items['admin/settings/language/add'] = array( 'title' => 'Add language', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('locale_languages_add_screen'), // two forms concatenated + 'page callback' => 'locale_languages_add_screen', // two forms concatenated 'weight' => 5, 'type' => MENU_LOCAL_TASK, + 'file' => 'locale.admin.inc', ); $items['admin/settings/language/configure'] = array( 'title' => 'Configure', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('drupal_get_form', 'locale_languages_configure_form'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_languages_configure_form'), 'weight' => 10, 'type' => MENU_LOCAL_TASK, + 'file' => 'locale.admin.inc', ); $items['admin/settings/language/edit/%'] = array( 'title' => 'Edit language', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('drupal_get_form', 'locale_languages_edit_form', 4), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_languages_edit_form', 4), 'type' => MENU_CALLBACK, + 'file' => 'locale.admin.inc', ); $items['admin/settings/language/delete/%'] = array( 'title' => 'Confirm', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('drupal_get_form', 'locale_languages_delete_form', 4), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_languages_delete_form', 4), 'type' => MENU_CALLBACK, + 'file' => 'locale.admin.inc', ); // 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 + 'page callback' => 'locale_translate_overview_screen', // not a form, just a table 'access arguments' => array('translate interface'), + 'file' => 'locale.admin.inc', ); $items['admin/build/translate/overview'] = array( 'title' => 'Overview', 'weight' => 0, 'type' => MENU_DEFAULT_LOCAL_TASK, + 'file' => 'locale.admin.inc', ); $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 + 'page callback' => 'locale_translate_seek_screen', // search results and form concatenated + 'file' => 'locale.admin.inc', ); $items['admin/build/translate/import'] = array( 'title' => 'Import', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('drupal_get_form', 'locale_translate_import_form'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_translate_import_form'), 'weight' => 20, 'type' => MENU_LOCAL_TASK, + 'file' => 'locale.admin.inc', ); $items['admin/build/translate/export'] = array( 'title' => 'Export', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('locale_translate_export_screen'), // possibly multiple forms concatenated + 'page callback' => 'locale_translate_export_screen', // possibly multiple forms concatenated 'weight' => 30, 'type' => MENU_LOCAL_TASK, + 'file' => 'locale.admin.inc', ); $items['admin/build/translate/edit/%'] = array( 'title' => 'Edit string', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('drupal_get_form', 'locale_translate_edit_form', 4), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_translate_edit_form', 4), 'type' => MENU_CALLBACK, + 'file' => 'locale.admin.inc', ); $items['admin/build/translate/delete/%'] = array( 'title' => 'Delete string', - 'page callback' => 'locale_inc_callback', - 'page arguments' => array('locale_translate_delete', 4), // directly deletes, no confirmation + 'page callback' => 'locale_translate_delete', + 'page arguments' => array(4), // directly deletes, no confirmation 'type' => MENU_CALLBACK, + 'file' => 'locale.admin.inc', ); 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() { @@ -284,6 +283,9 @@ function locale_theme() { 'locale_languages_overview_form' => array( 'arguments' => array('form' => array()), ), + 'locale_admin_manage_screen' => array( + 'arguments' => array('form' => NULL), + ), ); } @@ -449,7 +451,7 @@ function locale_language_list($field = ' * translations for. */ function locale_system_update($components) { - include_once 'includes/locale.inc'; + include_once(drupal_get_path('module', 'locale') .'/locale.admin.inc'); if ($batch = locale_batch_by_component($components)) { batch_set($batch); } Index: sites/default/settings.php =================================================================== RCS file: /cvs/drupal/drupal/sites/default/Attic/settings.php,v retrieving revision 1.54 diff -u -p -r1.54 settings.php --- sites/default/settings.php 28 Mar 2007 14:08:22 -0000 1.54 +++ sites/default/settings.php 29 Oct 2007 02:07:13 -0000 @@ -1,5 +1,5 @@ 2) { - ini_set('session.cookie_domain', $domain); - } -} - -/** - * On some sites, multiple domains or subdomains may point to the same site. - * For instance, example.com may redirect to foo.example.com. In that case, - * the browser may confuse the cookies between the two domains, resulting in - * an inability to log in. In that case, uncomment the line below and set - * it to the more generic domain name. For instance, .example.com is more - * generic than .foo.example.com. Remember the leading period on the domain - * name, even if you wouldn't type it in your browser. + * Drupal automatically generates a unique session cookie name for each site + * based on on its full domain name. If you have multiple domains pointing at + * the same Drupal site, you can either redirect them all to a single domain + * (see comment in .htaccess), or uncomment the line below and specify their + * shared base domain. Doing so assures that users remain logged in as they + * cross between your various domains. */ -#ini_set('session.cookie_domain', '.example.com'); +# $cookie_domain = 'example.com'; /** * Variable overrides: @@ -176,3 +174,16 @@ if (isset($_SERVER['HTTP_HOST'])) { # 'anonymous' => 'Visitor', # ); +/** + * String overrides: + * + * To override specific strings on your site with or without enabling locale + * module, add an entry to this list. This functionality allows you to change + * a small number of your site's default English language interface strings. + * + * Remove the leading hash signs to enable. + */ +# $conf['locale_custom_strings_en'] = array( +# 'forum' => 'Discussion board', +# '@count min' => '@count minutes', +# );