From 56d173d9825a4ce7b596548b24f863f4f924448a Mon Sep 17 00:00:00 2001 From: droplet Date: Wed, 6 Jun 2012 20:37:39 +0800 Subject: [PATCH] fix few tests --- .../locale/Tests/LocaleImportFunctionalTest.php | 12 +- .../Drupal/locale/Tests/LocalePluralFormatTest.php | 3 +- .../Drupal/locale/Tests/LocaleTranslationTest.php | 24 +- .../Drupal/locale/Tests/LocaleUninstallTest.php | 8 +- core/modules/locale/locale.css | 15 +- core/modules/locale/locale.module | 20 +- core/modules/locale/locale.pages.inc | 469 ++++++++------------ 7 files changed, 216 insertions(+), 335 deletions(-) diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php index 3beee64..21378a6 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php @@ -91,7 +91,7 @@ class LocaleImportFunctionalTest extends WebTestBase { // Ensure string wasn't overwritten. $search = array( 'string' => 'Montag', - 'language' => 'fr', + 'langcode' => 'fr', 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -113,7 +113,7 @@ class LocaleImportFunctionalTest extends WebTestBase { // Ensure string was overwritten. $search = array( 'string' => 'Montag', - 'language' => 'fr', + 'langcode' => 'fr', 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -149,7 +149,7 @@ class LocaleImportFunctionalTest extends WebTestBase { // Ensure string wasn't overwritten. $search = array( 'string' => 'januari', - 'language' => 'fr', + 'langcode' => 'fr', 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -168,7 +168,7 @@ class LocaleImportFunctionalTest extends WebTestBase { // Ensure string was overwritten. $search = array( 'string' => 'januari', - 'language' => 'fr', + 'langcode' => 'fr', 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -201,7 +201,7 @@ class LocaleImportFunctionalTest extends WebTestBase { // Ensure strings were successfully imported. $search = array( 'string' => 'lundi', - 'language' => $langcode, + 'langcode' => $langcode, 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -247,7 +247,7 @@ class LocaleImportFunctionalTest extends WebTestBase { $str = "Operations"; $search = array( 'string' => $str, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php index 6d45d21..5cfa726 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocalePluralFormatTest.php @@ -160,7 +160,8 @@ class LocalePluralFormatTest extends WebTestBase { // Check if the source appears on the translation page. $this->drupalGet('admin/config/regional/translate'); - $this->assertText("1 hour, @count hours"); + $this->assertText("1 hour"); + $this->assertText("@count hours"); // Look up editing page for this plural string and check fields. $lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(':source' => "1 hour" . LOCALE_PLURAL_DELIMITER . "@count hours"))->fetchField(); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php index 28e0834..d7269d0 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php @@ -67,7 +67,7 @@ class LocaleTranslationTest extends WebTestBase { $this->drupalLogin($translate_user); $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -147,7 +147,7 @@ class LocaleTranslationTest extends WebTestBase { $this->drupalLogin($translate_user); $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -256,7 +256,7 @@ class LocaleTranslationTest extends WebTestBase { // Reset locale cache. $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -316,7 +316,7 @@ class LocaleTranslationTest extends WebTestBase { $this->drupalLogin($translate_user); $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -329,7 +329,7 @@ class LocaleTranslationTest extends WebTestBase { // translated strings'. $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -339,7 +339,7 @@ class LocaleTranslationTest extends WebTestBase { // strings'. $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'untranslated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -361,7 +361,7 @@ class LocaleTranslationTest extends WebTestBase { // translated strings'. $search = array( 'string' => $translation, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'translated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -371,7 +371,7 @@ class LocaleTranslationTest extends WebTestBase { // untranslated strings'. $search = array( 'string' => $name, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'untranslated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -381,7 +381,7 @@ class LocaleTranslationTest extends WebTestBase { // untranslated strings'. $search = array( 'string' => $translation, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'untranslated', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -390,7 +390,7 @@ class LocaleTranslationTest extends WebTestBase { // Ensure translated string does appear if searching on the custom language. $search = array( 'string' => $translation, - 'language' => $langcode, + 'langcode' => $langcode, 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -399,7 +399,7 @@ class LocaleTranslationTest extends WebTestBase { // Ensure translated string doesn't appear if searching in System (English). $search = array( 'string' => $translation, - 'language' => LANGUAGE_SYSTEM, + 'langcode' => LANGUAGE_SYSTEM, 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); @@ -409,7 +409,7 @@ class LocaleTranslationTest extends WebTestBase { $unavailable_string = $this->randomName(16); $search = array( 'string' => $unavailable_string, - 'language' => 'all', + 'langcode' => 'all', 'translation' => 'all', ); $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter')); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php index 72868e9..71aff68 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php @@ -61,11 +61,13 @@ class LocaleUninstallTest extends WebTestBase { $user = $this->drupalCreateUser(array('translate interface', 'access administration pages')); $this->drupalLogin($user); $this->drupalGet('admin/config/regional/translate/translate'); - $string = db_query('SELECT min(lid) AS lid FROM {locales_source} WHERE location LIKE :location', array( + $string = db_query('SELECT min(lid) AS lid, source FROM {locales_source} WHERE location LIKE :location', array( ':location' => '%.js%', ))->fetchObject(); - $edit = array('translations[fr][0]' => 'french translation'); - $this->drupalPost('admin/config/regional/translate/edit/' . $string->lid, $edit, t('Save translations')); + $edit = array('string' => $string->source); + $this->drupalPost('admin/config/regional/translate', $edit, t('Filter')); + $edit = array('strings[' . $string->lid . '][translations][0]' => 'french translation'); + $this->drupalPost('admin/config/regional/translate', $edit, t('Save translations')); _locale_rebuild_js('fr'); $locale_javascripts = variable_get('locale_translation_javascript', array()); $js_file = 'public://' . variable_get('locale_js_directory', 'languages') . '/fr_' . $locale_javascripts['fr'] . '.js'; diff --git a/core/modules/locale/locale.css b/core/modules/locale/locale.css index 6c03945..3e13e07 100644 --- a/core/modules/locale/locale.css +++ b/core/modules/locale/locale.css @@ -1,11 +1,6 @@ -.locale-untranslated { - font-style: normal; - text-decoration: line-through; -} - -#locale-translation-filter-form .form-item-language, -#locale-translation-filter-form .form-item-translation, -#locale-translation-filter-form .form-item-customized { +#locale-translate-filter-form .form-item-langcode, +#locale-translate-filter-form .form-item-translation, +#locale-translate-filter-form .form-item-customized { float: left; /* LTR */ padding-right: .8em; /* LTR */ margin: 0.1em; @@ -16,10 +11,10 @@ */ width: 15em; } -#locale-translation-filter-form .form-type-select select { +#locale-translate-filter-form .form-type-select select { width: 100%; } -#locale-translation-filter-form .form-actions { +#locale-translate-filter-form .form-actions { float: left; /* LTR */ padding: 3ex 0 0 1em; /* LTR */ } diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 35f8032..b3ad5e7 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -111,7 +111,7 @@ function locale_menu() { $items['admin/config/regional/translate'] = array( 'title' => 'User interface translation', 'description' => 'Translate the built-in user interface.', - 'page callback' => 'locale_translate_seek_screen', + 'page callback' => 'locale_translate_screen', 'access arguments' => array('translate interface'), 'file' => 'locale.pages.inc', 'weight' => -5, @@ -139,20 +139,6 @@ function locale_menu() { 'type' => MENU_LOCAL_TASK, 'file' => 'locale.bulk.inc', ); - $items['admin/config/regional/translate/edit/%'] = array( - 'title' => 'Edit string', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('locale_translate_edit_form', 5), - 'access arguments' => array('translate interface'), - 'file' => 'locale.pages.inc', - ); - $items['admin/config/regional/translate/delete/%'] = array( - 'title' => 'Delete string', - 'page callback' => 'locale_translate_delete_page', - 'page arguments' => array(5), - 'access arguments' => array('translate interface'), - 'file' => 'locale.pages.inc', - ); // Localize date formats. $items['admin/config/regional/date-time/locale'] = array( @@ -265,6 +251,10 @@ function locale_theme() { 'locale_date_format_form' => array( 'render element' => 'form', ), + 'locale_translate_edit_form_strings' => array( + 'render element' => 'form', + 'file' => 'locale.pages.inc', + ), ); } diff --git a/core/modules/locale/locale.pages.inc b/core/modules/locale/locale.pages.inc index aaabec7..082a2b4 100644 --- a/core/modules/locale/locale.pages.inc +++ b/core/modules/locale/locale.pages.inc @@ -10,152 +10,79 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * String search screen. */ -function locale_translate_seek_screen() { +function locale_translate_screen() { // Add CSS. drupal_add_css(drupal_get_path('module', 'locale') . '/locale.css'); - $elements = drupal_get_form('locale_translation_filter_form'); - $output = drupal_render($elements); - $output .= _locale_translate_seek(); - return $output; + return array( + 'filter' => drupal_get_form('locale_translate_filter_form'), + 'form' => drupal_get_form('locale_translate_edit_form'), + ); } /** - * Perform a string search and display results in a table + * Build a string search query. */ -function _locale_translate_seek() { - $output = ''; - - // We have at least one criterion to match - if (!($query = _locale_translate_seek_query())) { - $query = array( - 'translation' => 'all', - 'language' => 'all', - 'customized' => 'all', - 'string' => '', - ); - } +function locale_translate_query() { + $filter_values = locale_translate_filter_values(); $sql_query = db_select('locales_source', 's'); - $sql_query->leftJoin('locales_target', 't', 't.lid = s.lid'); + // Language is sanitized to be one of the possible options in + // locale_translate_filter_values(). + $sql_query->leftJoin('locales_target', 't', "t.lid = s.lid AND t.language = '" . $filter_values['langcode'] . "'"); $sql_query->fields('s', array('source', 'location', 'context', 'lid')); $sql_query->fields('t', array('translation', 'language', 'customized')); - // Compute LIKE section. - switch ($query['translation']) { + if (!empty($filter_values['string'])) { + $sql_query->condition(db_or() + ->condition('s.source', '%' . db_like($filter_values['string']) . '%', 'LIKE') + ->condition('t.translation', '%' . db_like($filter_values['string']) . '%', 'LIKE') + ); + } + + // Add translation status conditions. + switch ($filter_values['translation']) { case 'translated': - $sql_query->condition('t.translation', '%' . db_like($query['string']) . '%', 'LIKE'); - $sql_query->orderBy('t.translation', 'DESC'); - if ($query['customized'] != 'all') { - $sql_query->condition('t.customized', $query['customized']); + $sql_query->isNotNull('t.translation'); + if ($filter_values['customized'] != 'all') { + $sql_query->condition('t.customized', $filter_values['customized']); } break; case 'untranslated': - $sql_query->condition(db_and() - ->condition('s.source', '%' . db_like($query['string']) . '%', 'LIKE') - ->isNull('t.translation') - ); - $sql_query->orderBy('s.source'); - break; - case 'all' : - default: - $condition = db_or() - ->condition('s.source', '%' . db_like($query['string']) . '%', 'LIKE'); - if ($query['language'] != LANGUAGE_SYSTEM) { - // Only search in translations if the language is not forced to system language. - $condition->condition('t.translation', '%' . db_like($query['string']) . '%', 'LIKE'); - } - $sql_query->condition($condition); + $sql_query->isNull('t.translation'); break; } - $limit_language = NULL; - if ($query['language'] != LANGUAGE_SYSTEM && $query['language'] != 'all') { - $sql_query->condition('language', $query['language']); - $limit_language = $query['language']; - } - - $sql_query = $sql_query - ->extend('Drupal\Core\Database\Query\PagerSelectExtender') - ->limit(50); - $locales = $sql_query->execute(); - - $header = array(t('String'), t('Context'), ($limit_language) ? t('Language') : t('Languages'), array('data' => t('Operations'), 'colspan' => '2')); - - $strings = array(); - foreach ($locales as $locale) { - if (!isset($strings[$locale->lid])) { - $strings[$locale->lid] = array( - 'languages' => array(), - 'location' => $locale->location, - 'source' => $locale->source, - 'context' => $locale->context, - ); - } - if (isset($locale->language)) { - $strings[$locale->lid]['languages'][$locale->language] = $locale->translation; - } - } - - $rows = array(); - foreach ($strings as $lid => $string) { - $rows[] = array( - array('data' => check_plain(truncate_utf8(str_replace(LOCALE_PLURAL_DELIMITER, ', ', $string['source']), 150, FALSE, TRUE)) . '
' . $string['location'] . ''), - $string['context'], - array('data' => _locale_translate_language_list($string['languages'], $limit_language), 'align' => 'center'), - array('data' => l(t('edit'), "admin/config/regional/translate/edit/$lid", array('query' => drupal_get_destination())), 'class' => array('nowrap')), - array('data' => l(t('delete'), "admin/config/regional/translate/delete/$lid", array('query' => drupal_get_destination())), 'class' => array('nowrap')), - ); - } - - $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No strings available.'))); - $output .= theme('pager'); - - return $output; + $sql_query = $sql_query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(50); + return $sql_query->execute(); } /** - * List languages in search result table + * Build array out of search criteria specified in request variables. */ -function _locale_translate_language_list($translation, $limit_language) { - // Add CSS. - drupal_add_css(drupal_get_path('module', 'locale') . '/locale.css'); - - $languages = language_list(); - if (!locale_translate_english()) { - unset($languages['en']); - } - $output = ''; - foreach ($languages as $langcode => $language) { - if (!$limit_language || $limit_language == $langcode) { - $output .= (!empty($translation[$langcode])) ? $langcode . ' ' : "$langcode "; - } - } - - return $output; -} - -/** - * Build array out of search criteria specified in request variables - */ -function _locale_translate_seek_query() { - $query = &drupal_static(__FUNCTION__); - if (!isset($query)) { - $query = array(); - $fields = array('string', 'language', 'translation', 'customized'); - foreach ($fields as $field) { - if (isset($_SESSION['locale_translation_filter'][$field])) { - $query[$field] = $_SESSION['locale_translation_filter'][$field]; +function locale_translate_filter_values() { + $filter_values = &drupal_static(__FUNCTION__); + if (!isset($filter_values)) { + $filter_values = array(); + $filters = locale_translate_filters(); + foreach ($filters as $key => $filter) { + $filter_values[$key] = $filter['default']; + if (isset($_SESSION['locale_translate_filter'][$key])) { + // Only allow this value if it was among the options, or + // if there were no fixed options to filter for. + if (!isset($filter['options']) || isset($filter['options'][$_SESSION['locale_translate_filter'][$key]])) { + $filter_values[$key] = $_SESSION['locale_translate_filter'][$key]; + } } } } - return $query; + return $filter_values; } /** * List locale translation filters that can be applied. */ -function locale_translation_filters() { +function locale_translate_filters() { $filters = array(); // Get all languages, except English @@ -168,14 +95,23 @@ function locale_translation_filters() { } } + // Pick the default language code for the filter. + $default_langcode = language_default()->langcode; + if (!isset($language_options[$default_langcode])) { + $available_langcodes = array_keys($language_options); + $default_langcode = array_shift($available_langcodes); + } + $filters['string'] = array( 'title' => t('String contains'), 'description' => t('Leave blank to show all strings. The search is case sensitive.'), + 'default' => '', ); - $filters['language'] = array( + $filters['langcode'] = array( 'title' => t('Language'), - 'options' => array_merge(array('all' => t('All languages'), LANGUAGE_SYSTEM => t('System (English)')), $language_options), + 'options' => $language_options, + 'default' => $default_langcode, ); $filters['translation'] = array( @@ -185,6 +121,7 @@ function locale_translation_filters() { 'translated' => t('Only translated strings'), 'untranslated' => t('Untranslated strings') ), + 'default' => 'all', ); $filters['customized'] = array( @@ -199,6 +136,7 @@ function locale_translation_filters() { ':input[name=translation]' => array('value' => 'translated'), ) ), + 'default' => 'all', ); return $filters; @@ -209,8 +147,9 @@ function locale_translation_filters() { * * @ingroup forms */ -function locale_translation_filter_form() { - $filters = locale_translation_filters(); +function locale_translate_filter_form() { + $filters = locale_translate_filters(); + $filter_values = locale_translate_filter_values(); $form['filters'] = array( '#type' => 'fieldset', @@ -225,24 +164,25 @@ function locale_translation_filter_form() { '#type' => 'search', '#title' => $filter['title'], '#description' => $filter['description'], + '#default_value' => $filter_values[$key], ); } else { + + $empty_option = isset( $filter['options'][$filter['default']]) ? $filter['options'][$filter['default']] : array(); $form['filters']['status'][$key] = array( '#title' => $filter['title'], '#type' => 'select', - '#empty_value' => 'all', - '#empty_option' => $filter['options']['all'], + '#empty_value' => $filter['default'], + '#empty_option' => $empty_option, '#size' => 0, '#options' => $filter['options'], + '#default_value' => $filter_values[$key], ); if (isset($filter['states'])) { $form['filters']['status'][$key]['#states'] = $filter['states']; } } - if (!empty($_SESSION['locale_translation_filter'][$key])) { - $form['filters']['status'][$key]['#default_value'] = $_SESSION['locale_translation_filter'][$key]; - } } $form['filters']['actions'] = array( @@ -253,7 +193,7 @@ function locale_translation_filter_form() { '#type' => 'submit', '#value' => t('Filter'), ); - if (!empty($_SESSION['locale_translation_filter'])) { + if (!empty($_SESSION['locale_translate_filter'])) { $form['filters']['actions']['reset'] = array( '#type' => 'submit', '#value' => t('Reset') @@ -264,168 +204,137 @@ function locale_translation_filter_form() { } /** - * Validate result from locale translation filter form. - */ -function locale_translation_filter_form_validate($form, &$form_state) { - if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['language'])) { - form_set_error('type', t('You must select something to filter by.')); - } -} - -/** * Process result from locale translation filter form. */ -function locale_translation_filter_form_submit($form, &$form_state) { +function locale_translate_filter_form_submit($form, &$form_state) { $op = $form_state['values']['op']; - $filters = locale_translation_filters(); + $filters = locale_translate_filters(); switch ($op) { case t('Filter'): foreach ($filters as $name => $filter) { if (isset($form_state['values'][$name])) { - $_SESSION['locale_translation_filter'][$name] = $form_state['values'][$name]; + $_SESSION['locale_translate_filter'][$name] = $form_state['values'][$name]; } } break; case t('Reset'): - $_SESSION['locale_translation_filter'] = array(); + $_SESSION['locale_translate_filter'] = array(); break; } $form_state['redirect'] = 'admin/config/regional/translate/translate'; } - /** - * User interface for string editing. + * User interface for string editing as one table. * * @ingroup forms */ -function locale_translate_edit_form($form, &$form_state, $lid) { - // Fetch source string, if possible. - $source = db_query('SELECT source, context, location FROM {locales_source} WHERE lid = :lid', array(':lid' => $lid))->fetchObject(); - if (!$source) { - drupal_set_message(t('String not found.'), 'error'); - drupal_goto('admin/config/regional/translate/translate'); - } - // Split source to work with plural values. - $source_array = explode(LOCALE_PLURAL_DELIMITER, $source->source); - if (count($source_array) == 1) { - // Add original text value and mark as non-plural. - $form['plural'] = array( - '#type' => 'value', - '#value' => 0 - ); - $form['original'] = array( - '#type' => 'item', - '#title' => t('Original text'), - '#markup' => check_plain($source_array[0]), - ); - } - else { - // Add original text value and mark as plural. - $form['plural'] = array( - '#type' => 'value', - '#value' => 1 - ); - $form['original_singular'] = array( - '#type' => 'item', - '#title' => t('Original singular form'), - '#markup' => check_plain($source_array[0]), - ); - $form['original_plural'] = array( - '#type' => 'item', - '#title' => t('Original plural form'), - '#markup' => check_plain($source_array[1]), - ); - } - if (!empty($source->context)) { - $form['context'] = array( - '#type' => 'item', - '#title' => t('Context'), - '#markup' => check_plain($source->context), - ); - } - $form['lid'] = array( - '#type' => 'value', - '#value' => $lid - ); - $form['location'] = array( - '#type' => 'value', - '#value' => $source->location - ); +function locale_translate_edit_form($form, &$form_state) { + $strings = locale_translate_query(); - // 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(); - if (!locale_translate_english()) { - unset($languages['en']); - } - // Store languages to iterate for validation and submission of the form. - $form_state['langcodes'] = array_keys($languages); $plural_formulas = variable_get('locale_translation_plurals', array()); - $form['translations'] = array( - '#type' => 'vertical_tabs', - '#tree' => TRUE + $filter_values = locale_translate_filter_values(); + $form['langcode'] = array( + '#type' => 'value', + '#value' => $filter_values['langcode'], ); - // Approximate the number of rows to use in the default textarea. - $rows = min(ceil(str_word_count($source_array[0]) / 12), 10); - foreach ($languages as $langcode => $language) { - $form['translations'][$langcode] = array( - '#type' => 'fieldset', - '#title' => $language->name, + $form['strings'] = array( + '#type' => 'item', + '#tree' => TRUE, + '#theme' => 'locale_translate_edit_form_strings', + ); + foreach ($strings as $string) { + // Split source to work with plural values. + $source_array = explode(LOCALE_PLURAL_DELIMITER, $string->source); + $translation_array = explode(LOCALE_PLURAL_DELIMITER, $string->translation); + if (count($source_array) == 1) { + // Add original text value and mark as non-plural. + $form['strings'][$string->lid]['plural'] = array( + '#type' => 'value', + '#value' => 0 + ); + $form['strings'][$string->lid]['original'] = array( + '#type' => 'item', + '#title' => t('Original text'), + '#title_display' => 'invisible', + '#markup' => check_plain($source_array[0]), + ); + } + else { + // Add original text value and mark as plural. + $form['strings'][$string->lid]['plural'] = array( + '#type' => 'value', + '#value' => 1 + ); + $form['strings'][$string->lid]['original_singular'] = array( + '#type' => 'item', + '#title' => t('Singular form'), + '#markup' => check_plain($source_array[0]), + ); + $form['strings'][$string->lid]['original_plural'] = array( + '#type' => 'item', + '#title' => t('Plural form'), + '#markup' => check_plain($source_array[1]), + ); + } + if (!empty($string->context)) { + $form['strings'][$string->lid]['context'] = array( + '#type' => 'value', + '#value' => check_plain($string->context), + ); + } + $form['strings'][$string->lid]['location'] = array( + '#type' => 'value', + '#value' => $string->location, ); - if (empty($form['plural']['#value'])) { - $form['translations'][$langcode][0] = array( + + // Approximate the number of rows to use in the default textarea. + $rows = min(ceil(str_word_count($source_array[0]) / 12), 10); + if (empty($form['strings'][$string->lid]['plural']['#value'])) { + $form['strings'][$string->lid]['translations'][0] = array( '#type' => 'textarea', - '#title' => $language->name, + '#title_display' => 'invisible', '#rows' => $rows, - '#default_value' => '', + '#default_value' => $translation_array[0], ); } else { // Dealing with plural strings. - if (isset($plural_formulas[$langcode]['plurals']) && $plural_formulas[$langcode]['plurals'] > 1) { + if (isset($plural_formulas[$string->language]['plurals']) && $plural_formulas[$string->language]['plurals'] > 1) { // Add a textarea for each plural variant. - for ($i = 0; $i < $plural_formulas[$langcode]['plurals']; $i++) { - $form['translations'][$langcode][$i] = array( + for ($i = 0; $i < $plural_formulas[$string->language]['plurals']; $i++) { + $form['strings'][$string->lid]['translations'][$i] = array( '#type' => 'textarea', '#title' => ($i == 0 ? t('Singular form') : format_plural($i, 'First plural form', '@count. plural form')), '#rows' => $rows, - '#default_value' => '', + '#default_value' => $translation_array[$i], ); } } else { // Fallback for unknown number of plurals. - $form['translations'][$langcode][0] = array( + $form['strings'][$string->lid]['translations'][0] = array( '#type' => 'textarea', '#title' => t('Sigular form'), '#rows' => $rows, - '#default_value' => '', + '#default_value' => $translation_array[0], ); - $form['translations'][$langcode][1] = array( + $form['strings'][$string->lid]['translations'][1] = array( '#type' => 'textarea', '#title' => t('Plural form'), '#rows' => $rows, - '#default_value' => '', + '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '', ); } } } - - // Fetch translations and fill in default values in the form. - $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = :lid", array(':lid' => $lid)); - foreach ($result as $translation) { - $translation_array = explode(LOCALE_PLURAL_DELIMITER, $translation->translation); - for ($i = 0; $i < count($translation_array); $i++) { - $form['translations'][$translation->language][$i]['#default_value'] = $translation_array[$i]; - } + if (count(element_children($form['strings']))) { + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save translations')); } - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save translations')); return $form; } @@ -433,9 +342,11 @@ function locale_translate_edit_form($form, &$form_state, $lid) { * Validate string editing form submissions. */ function locale_translate_edit_form_validate($form, &$form_state) { - foreach ($form_state['langcodes'] as $langcode) { - foreach ($form_state['values']['translations'][$langcode] as $key => $value) { + $langcode = $form_state['values']['langcode']; + foreach ($form_state['values']['strings'] as $lid => $translations) { + foreach ($translations['translations'] as $key => $value) { if (!locale_string_is_safe($value)) { + form_set_error("strings][$lid][translations][$key", t('The submitted string contains disallowed HTML: %string', array('%string' => $value))); form_set_error("translations][$langcode][$key", t('The submitted string contains disallowed HTML: %string', array('%string' => $value))); watchdog('locale', 'Attempted submission of a translation string with disallowed HTML: %string', array('%string' => $value), WATCHDOG_WARNING); } @@ -445,18 +356,16 @@ function locale_translate_edit_form_validate($form, &$form_state) { /** * 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['langcodes'] as $langcode) { + $langcode = $form_state['values']['langcode']; + foreach ($form_state['values']['strings'] as $lid => $translations) { // Serialize plural variants in one string by LOCALE_PLURAL_DELIMITER. - $value = implode(LOCALE_PLURAL_DELIMITER, $form_state['values']['translations'][$langcode]); - $translation = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $langcode))->fetchField(); + $translation_new = implode(LOCALE_PLURAL_DELIMITER, $translations['translations']); + $translation_old = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $langcode))->fetchField(); // No translation when all strings are empty. $has_translation = FALSE; - foreach ($form_state['values']['translations'][$langcode] as $string) { + foreach ($translations['translations'] as $string) { if (!empty($string)) { $has_translation = TRUE; break; @@ -464,28 +373,28 @@ function locale_translate_edit_form_submit($form, &$form_state) { } if ($has_translation) { // Only update or insert if we have a value to use. - if (!empty($translation) && $translation != $value) { + if (!empty($translation_old) && $translation_old != $translation_new) { db_update('locales_target') ->fields(array( - 'translation' => $value, + 'translation' => $translation_new, 'customized' => LOCALE_CUSTOMIZED, )) ->condition('lid', $lid) ->condition('language', $langcode) ->execute(); } - if (empty($translation)) { + if (empty($translation_old)) { db_insert('locales_target') ->fields(array( 'lid' => $lid, - 'translation' => $value, + 'translation' => $translation_new, 'language' => $langcode, 'customized' => LOCALE_CUSTOMIZED, )) ->execute(); } } - elseif (!empty($translation)) { + elseif (!empty($translation_old)) { // Empty translation entered: remove existing entry from database. db_delete('locales_target') ->condition('lid', $lid) @@ -493,55 +402,39 @@ function locale_translate_edit_form_submit($form, &$form_state) { ->execute(); } - // Force JavaScript translation file recreation for this language. - _locale_invalidate_js($langcode); } - drupal_set_message(t('The string has been saved.')); + drupal_set_message(t('The strings have been saved.')); + // Force JavaScript translation file recreation for this language. + _locale_invalidate_js($langcode); // Clear locale cache. - _locale_invalidate_js(); cache()->deletePrefix('locale:'); - - $form_state['redirect'] = 'admin/config/regional/translate/translate'; - return; } /** - * String deletion confirmation page. + * Default theme function for translatione edit form. */ -function locale_translate_delete_page($lid) { - if ($source = db_query('SELECT lid, source FROM {locales_source} WHERE lid = :lid', array(':lid' => $lid))->fetchObject()) { - return drupal_get_form('locale_translate_delete_form', $source); - } - else { - throw new NotFoundHttpException(); +function theme_locale_translate_edit_form_strings($variables) { + $output = ''; + $header = array(t('Source string'), t('Translation')); + $rows = array(); + $form = $variables['form']; + foreach (element_children($form) as $lid) { + $string = $form[$lid]; + if ($string['plural']['#value']) { + $source = drupal_render($string['original_singular']) .'
'. drupal_render($string['original_plural']); + } + else { + $source = drupal_render($string['original']); + } + $source .= empty($string['context']) ? '' : '
'. t('In Context') .': '. $string['context']['#value'] .''; + $rows[] = array( + array('data' => $source), + array('data' => $string['translations']), + ); } -} - -/** - * User interface for the string deletion confirmation screen. - * - * @ingroup forms - */ -function locale_translate_delete_form($form, &$form_state, $source) { - $form['lid'] = array('#type' => 'value', '#value' => $source->lid); - return confirm_form($form, t('Are you sure you want to delete the string "%source"?', array('%source' => $source->source)), 'admin/config/regional/translate/translate', t('Deleting the string will remove all translations of this string in all languages. This action cannot be undone.'), t('Delete'), t('Cancel')); -} - -/** - * Process string deletion submissions. - */ -function locale_translate_delete_form_submit($form, &$form_state) { - db_delete('locales_source') - ->condition('lid', $form_state['values']['lid']) - ->execute(); - db_delete('locales_target') - ->condition('lid', $form_state['values']['lid']) - ->execute(); - // Force JavaScript translation file recreation for all languages. - _locale_invalidate_js(); - cache()->deletePrefix('locale:'); - drupal_set_message(t('The string has been removed.')); - $form_state['redirect'] = 'admin/config/regional/translate/translate'; + $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No strings available.'))); + $output .= theme('pager'); + return $output; } -- 1.7.8.2