commit 4652931a8e91a104f737ed096e534d2e1e0ae861 Author: Erik Stielstra Date: Tue Aug 4 09:15:41 2015 +0200 Code style, naming and other minor changes. diff --git a/l10n_update.drush.inc b/l10n_update.drush.inc index eec7ce6..a496167 100644 --- a/l10n_update.drush.inc +++ b/l10n_update.drush.inc @@ -269,13 +269,12 @@ function _drush_l10n_update_get_updates() { * Callback for l10n-update-d8-plural-fix command. */ function drush_l10n_update_d8_plural_fix() { - // Establish whether any D8 style translations have been imported. - if (l10n_update_d8_plural_get_strings() !== FALSE) { - // Confirm that the user indeed wishes to convert D8 style plurals and proceed - // accordingly. - if (drush_confirm('The conversion operation is permanent - Are you sure you want to continue?', $indent = 0)) { + + if (l10n_update_get_d8_plural_strings() !== FALSE) { + // Confirm that the user wishes to convert D8 style plurals. + if (drush_confirm('This conversion is permanent. Are you sure you want to continue?', $indent = 0)) { $mode = (drush_get_option('overwrite')) ? LOCALE_IMPORT_OVERWRITE : LOCALE_IMPORT_KEEP; - $drush_message = dt('Initialising: Converting imported D8 style plurals into D7 format'); + $drush_message = dt('Please stand by, this may take some time...'); drush_log($drush_message . PHP_EOL, 'status'); $logs = l10n_update_d8_plural_conversion($mode); if (!empty($logs)) { @@ -284,13 +283,13 @@ function drush_l10n_update_d8_plural_fix() { } } else { - drush_log(PHP_EOL . dt('Finished D8 => D7 translation conversion operations.') . PHP_EOL, 'ok'); + drush_log(PHP_EOL . dt('Finished. Drupal 8 plural style translation strings were converted.') . PHP_EOL, 'ok'); } } } else { - // No D8 style translation strings have been imported - $drush_message = dt('No D8 style translation strings could be found; Exiting.'); + // No D8 style translation strings have been imported. + $drush_message = dt('Finished. No Drupal 8 plural style translation strings were be found.'); drush_log($drush_message . PHP_EOL, 'status'); } } diff --git a/l10n_update.install b/l10n_update.install index 07773cb..f48591f 100644 --- a/l10n_update.install +++ b/l10n_update.install @@ -262,57 +262,58 @@ function l10n_update_requirements($phase) { * @param Int $mode * Specifies the mode used for importing strings into the database. * + * @return array + * Returns a keyed array of log messages. Available keys: + * - message: Translated message + * - status: Message status. See drush_log() for supported values. */ function l10n_update_d8_plural_conversion($mode = LOCALE_IMPORT_KEEP) { - module_load_include('translation.inc', 'l10n_update'); + module_load_include('inc', 'l10n_update', 'translation'); $warnings_encountered = FALSE; $logs = array(); - - // Retrieve the list of D8 style translation strings - $results = l10n_update_d8_plural_get_strings(); - - // Iterate through each string - foreach ($results as $result) { - // Extract the source & target strings and other relevant data. - $original = $result->source; - $sources = explode(L10N_UPDATE_PLURAL_DELIMITER, $result->source); - $translations = explode(L10N_UPDATE_PLURAL_DELIMITER, $result->translation); - $language = $result->language; - $text_group = $result->textgroup; - $lid = $result->lid; + $modified = array(); + + $plurals = l10n_update_get_d8_plural_strings(); + foreach ($plurals as $plural) { + + // Extract data from source and target strings. + $original = $plural->source; + $sources = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->source); + $translations = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->translation); + $language = $plural->language; + $text_group = $plural->textgroup; + $lid = $plural->lid; $success = FALSE; - // Iterate through each source string and convert if appropriate + // Iterate over source strings and convert if appropriate. for ($index = 0; $index < count($sources); $index++) { $source = $sources[$index]; $translation = $translations[$index]; - // Establish whether a correct translation already exists for the source strings + // Establish whether a correct translation already exists. $query = db_select('locales_source', 'ls'); $query->addField('ls', 'source'); $query->addField('lt', 'translation'); $query->innerJoin('locales_target', 'lt', 'ls.lid = lt.lid'); - $query->condition('source', $source, '='); - $query->condition('language', $language, '='); + $query->condition('source', $source); + $query->condition('language', $language); $result = $query->execute(); - // Translations already exist for the plural string components // @todo Only warn / skip if the translation is customized. - if ($result->rowCount() && $mode == LOCALE_IMPORT_KEEP) { + if ($plural->rowCount() && $mode == LOCALE_IMPORT_KEEP) { + // Custom translation exists, add a warning to the logs. $logs[] = array( - 'message' => t('Cannot convert string \'@original\': @language translation already exists for source string \'@source\'', array('@original' => $original, '@language' => $language, '@source' => $source)), + 'message' => t('Cannot convert string \'@original\': @language translation already exists for source string \'@source\'.', array('@original' => $original, '@language' => $language, '@source' => $source)), 'status' => 'warning', ); $warnings_encountered = TRUE; } else { - // Insert the new translations into the database + // Insert the new translations into the database. $report = array('skips' => 0, 'additions' => 0, 'updates' => 0, 'deletes' => 0); - // @todo: add actual context support. _l10n_update_locale_import_one_string_db($report, $language, '', $source, $translation, $text_group, NULL, LOCALE_IMPORT_OVERWRITE, L10N_UPDATE_STRING_CUSTOM); - cache_clear_all('locale:', 'cache', TRUE); - _locale_invalidate_js($language); + if (!empty($report['skips'])) { $logs = array( 'message' => t('Not saved locally due to invalid HTML content.'), @@ -321,7 +322,7 @@ function l10n_update_d8_plural_conversion($mode = LOCALE_IMPORT_KEEP) { } elseif (!empty($report['additions']) || !empty($report['updates'])) { $logs = array( - 'message' => t('Updated translations for source string \'@source\'', array('@source' => $source)), + 'message' => t('Updated translations for source string \'@source\'.', array('@source' => $source)), 'status' => 'ok', ); $success = TRUE; @@ -340,24 +341,37 @@ function l10n_update_d8_plural_conversion($mode = LOCALE_IMPORT_KEEP) { ); } - // Delete the existing D8 format translation string from the database + // Delete the existing D8 formated translation string. Flag the language + // as being modified. if ($success) { + $modified[$language] = TRUE; + db_delete('locales_source') - ->condition('lid', $lid, '=') + ->condition('lid', $lid) ->execute(); db_delete('locales_target') - ->condition('lid', $lid, '=') + ->condition('lid', $lid) ->execute(); } } } } - // Correct translations already exist - alert the user to the --overwrite option + // Clear caches if translations are modified. + if ($modified) { + cache_clear_all('locale:', 'cache', TRUE); + foreach(array_keys($modified) as $langcode) { + _locale_invalidate_js($langcode); + } + } + + // Existing plural translations were skipped. Add a warning to the log to + // inform the user how to fix this situation. + // @todo Is it possible to fix these string via the translation interface? if ($warnings_encountered) { $logs = array( - 'message' => t('Re-run the command with the --overwrite option to replace existing translations'), + 'message' => t('Use the drush l10n-update-d8-plural-fix command with the --overwrite option to replace existing translations.'), 'status' => 'warning', ); } @@ -366,16 +380,18 @@ function l10n_update_d8_plural_conversion($mode = LOCALE_IMPORT_KEEP) { } /** - * Returns a list of imported D8 style translation strings. + * Returns an array of D8 plural style strings. * * @return Array|Bool - * An array of imported strings if found, otherwise FALSE. - * + * An array of data from source and translation strings of which the source + * string contains a Drupal 8 style plural delimiter. Returns false if not + * found. */ -function l10n_update_d8_plural_get_strings() { +function l10n_update_get_d8_plural_strings() { $translation_strings = &drupal_static(__FUNCTION__); + if (!isset($translation_strings)) { - // Retrieve a list of all affected translation strings. + // Retrieve a translation strings. $query = db_select('locales_source', 'ls'); $query->fields('ls', array('lid', 'location', 'textgroup', 'source', 'context', 'version')); $query->fields('lt', array('translation', 'language', 'plid', 'plural', 'l10n_status')); @@ -383,21 +399,18 @@ function l10n_update_d8_plural_get_strings() { $query->condition('source','%' . db_like(L10N_UPDATE_PLURAL_DELIMITER) . '%', 'LIKE'); $results = $query->execute(); - // If we have found some results then return these accordingly. if ($results->rowCount()) { foreach ($results as $result) { $translation_strings[] = $result; } - return $translation_strings; } else { - // No valid results could be retrieved. - return FALSE; + // No valid results were found. + $translation_strings = FALSE; } } - else { - return $translation_strings; - } + + return $translation_strings; } /** @@ -513,7 +526,7 @@ function l10n_update_update_7203() { } function l10n_update_update_7204() { - if (l10n_update_d8_plural_get_strings() !== FALSE) { + if (l10n_update_get_d8_plural_strings() !== FALSE) { $mode = (drush_get_option('overwrite')) ? LOCALE_IMPORT_OVERWRITE : LOCALE_IMPORT_KEEP; $logs = l10n_update_d8_plural_conversion($mode); if (!empty($logs)) {