You are here

function l10n_update_d8_plural_conversion in Localization update 7.2

Converts D8 style translations strings into D7 format.

Drupal 8 stores plurals (both for source and translation strings) as one string, where the plural forms are separated by a special character. By mistake this plural handling was also back ported. This function converts any D8 style plural into a D7 style plural. It used the following strategy.

  • Any locales_source record that contains the separator character is converted.
  • All these records are deleted, except those of which the translation was edited.
  • Existing translations will only be overwritten by converted translations if the import behaviour setting (admin/config/regional/language/update) allows that.

Parameters

int $start: The number of start.

int $length: The length number.

array $report: Array containing import results.

Return value

bool Returns true conversion a keyed array of log messages. Available keys:

  • message: Translated message
  • status: Message status. See drush_log() for supported values.
1 call to l10n_update_d8_plural_conversion()
l10n_update_update_7207 in ./l10n_update.install
Migrate D8 style plurals to D7 style.

File

./l10n_update.install, line 313
Install file for l10n remote updates.

Code

function l10n_update_d8_plural_conversion($start = 0, $length = 0, array &$report) {
  module_load_include('inc', 'l10n_update', 'l10n_update.translation');
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $processed = array();
  $plurals = l10n_update_get_d8_plural_strings($start, $length);
  $finished = count($plurals) < $length;
  if (empty($plurals)) {
    return $finished;
  }
  $mode = variable_get('l10n_update_import_mode', LOCALE_IMPORT_OVERWRITE);
  $writer = new PoDatabaseWriter();
  $writer
    ->setOptions(array(
    'overwrite_options' => array(
      'not_customized' => $mode != LOCALE_IMPORT_KEEP,
      'customized' => $mode == LOCALE_IMPORT_OVERWRITE,
    ),
    'customized' => L10N_UPDATE_NOT_CUSTOMIZED,
  ));
  $writer_report = isset($report['writer']) ? $report['writer'] : array();
  $writer
    ->setReport($writer_report);
  foreach ($plurals as $plural) {

    // Extract data from source and target strings.
    $sources = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->source);
    $translations = explode(L10N_UPDATE_PLURAL_DELIMITER, $plural->translation);
    $writer
      ->setLangcode($plural->language);
    $item = new PoItem();
    $item
      ->setContext($plural->context);
    $item
      ->setSource($sources);
    $item
      ->setTranslation($translations);
    $item
      ->setPlural(TRUE);
    $item
      ->setLangcode($plural->language);
    $item
      ->setTextgroup($plural->textgroup);
    $writer
      ->writeItem($item);

    // Collect plurals to be deleted. In the rare case that a translation was
    // modified, we will convert it but not delete the custom translation.
    if (!$plural->l10n_status) {
      $report['results']['lids'][] = $plural->lid;
    }
    $report['results']['languages'][$plural->language] = $plural->language;
  }
  $report['writer'] = $writer
    ->getReport();
  unset($report['writer']['strings']);
  return $finished;
}