Verified Commit b74670f1 authored by catch's avatar catch Committed by Lee Rowlands
Browse files

Issue #2806009 by alexpott, JvE, Berdir, Dmitriy.trt, jhodgdon, lokapujya,...

Issue #2806009 by alexpott, JvE, Berdir, Dmitriy.trt, jhodgdon, lokapujya, VladimirAus, Gábor Hojtsy, Jose Reyero, Anybody, kristiaanvandeneynde, Sutharsan, casey, smustgrave, nod_: Installing a module causes translations to be overwritten

(cherry picked from commit e029e6cd)
parent 7e4d997d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1841,7 +1841,7 @@ function install_finish_translations(&$install_state) {
  }

  // Creates configuration translations.
  $batches[] = locale_config_batch_update_components([], array_keys($languages));
  $batches[] = locale_config_batch_update_components([], array_keys($languages), [], TRUE);
  return $batches;
}

+30 −3
Original line number Diff line number Diff line
@@ -538,14 +538,19 @@ function locale_translate_delete_translation_files(array $projects = [], array $
 * @param array $components
 *   (optional) Array of component lists indexed by type. If not present or it
 *   is an empty array, it will update all components.
 * @param bool $update_default_config_langcodes
 *   Determines whether default configuration langcodes should be updated. This
 *   Should only happen during site and extension install.
 *
 * @return array
 *   The batch definition.
 */
function locale_config_batch_update_components(array $options, array $langcodes = [], array $components = []) {
function locale_config_batch_update_components(array $options, array $langcodes = [], array $components = [], bool $update_default_config_langcodes = FALSE) {
  $langcodes = $langcodes ? $langcodes : array_keys(\Drupal::languageManager()->getLanguages());
  if ($langcodes && $names = Locale::config()->getComponentNames($components)) {
    return locale_config_batch_build($names, $langcodes, $options);
    // If the component list is empty we need to ensure that all configuration
    // in the default collection is using the site's default langcode.
    return locale_config_batch_build($names, $langcodes, $options, $update_default_config_langcodes);
  }
}

@@ -560,19 +565,27 @@ function locale_config_batch_update_components(array $options, array $langcodes
 *   (optional) An array with options that can have the following elements:
 *   - 'finish_feedback': Whether or not to give feedback to the user when the
 *     batch is finished. Defaults to TRUE.
 * @param bool $update_default_config_langcodes
 *   Determines whether default configuration langcodes should be updated. This
 *   Should only happen during site and extension install.
 *
 * @return array
 *   The batch definition.
 *
 * @see locale_config_batch_refresh_name()
 */
function locale_config_batch_build(array $names, array $langcodes, array $options = []) {
function locale_config_batch_build(array $names, array $langcodes, array $options = [], bool $update_default_config_langcodes = FALSE) {
  $options += ['finish_feedback' => TRUE];
  $batch_builder = (new BatchBuilder())
    ->setFile(\Drupal::service('extension.list.module')->getPath('locale') . '/locale.bulk.inc')
    ->setTitle(t('Updating configuration translations'))
    ->setInitMessage(t('Starting configuration update'))
    ->setErrorMessage(t('Error updating configuration translations'));

  if ($update_default_config_langcodes && \Drupal::languageManager()->getDefaultLanguage()->getId() !== 'en') {
    $batch_builder->addOperation('locale_config_batch_set_config_langcodes');
  }

  $i = 0;
  $batch_names = [];
  foreach ($names as $name) {
@@ -596,6 +609,20 @@ function locale_config_batch_build(array $names, array $langcodes, array $option
  return $batch_builder->toArray();
}

/**
 * Implements callback_batch_operation().
 *
 * Updates default configuration when new modules or themes are installed.
 *
 * @param array|\ArrayAccess $context
 *   The batch context.
 */
function locale_config_batch_set_config_langcodes(&$context) {
  Locale::config()->updateDefaultConfigLangcodes();
  $context['finished'] = 1;
  $context['message'] = t('Updated default configuration to %langcode', ['%langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId()]);
}

/**
 * Implements callback_batch_operation().
 *
+2 −27
Original line number Diff line number Diff line
@@ -319,8 +319,6 @@ function locale_get_plural($count, $langcode = NULL) {
 * Implements hook_modules_installed().
 */
function locale_modules_installed($modules) {
  locale_system_set_config_langcodes();

  $components['module'] = $modules;
  locale_system_update($components);
}
@@ -337,8 +335,6 @@ function locale_module_preuninstall($module) {
 * Implements hook_themes_installed().
 */
function locale_themes_installed($themes) {
  locale_system_set_config_langcodes();

  $components['theme'] = $themes;
  locale_system_update($components);
}
@@ -370,28 +366,7 @@ function locale_cron() {
 * Updates default configuration when new modules or themes are installed.
 */
function locale_system_set_config_langcodes() {
  // Need to rewrite some default configuration language codes if the default
  // site language is not English.
  $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
  if ($default_langcode != 'en') {
    // Update active configuration copies of all prior shipped configuration if
    // they are still English. It is not enough to change configuration shipped
    // with the components just installed, because installing a component such
    // as views or tour module may bring in default configuration from prior
    // components.
    $names = Locale::config()->getComponentNames();
    foreach ($names as $name) {
      $config = \Drupal::configFactory()->reset($name)->getEditable($name);
      // Should only update if still exists in active configuration. If locale
      // module is enabled later, then some configuration may not exist anymore.
      if (!$config->isNew()) {
        $langcode = $config->get('langcode');
        if (empty($langcode) || $langcode == 'en') {
          $config->set('langcode', $default_langcode)->save();
        }
      }
    }
  }
  Locale::config()->updateDefaultConfigLangcodes();
}

/**
@@ -435,7 +410,7 @@ function locale_system_update(array $components) {
    // components. Do this even if import is not enabled because parsing new
    // configuration may expose new source strings.
    $module_handler->loadInclude('locale', 'inc', 'locale.bulk');
    if ($batch = locale_config_batch_update_components([])) {
    if ($batch = locale_config_batch_update_components([], [], [], TRUE)) {
      batch_set($batch);
    }
  }
+30 −0
Original line number Diff line number Diff line
@@ -649,4 +649,34 @@ protected function filterOverride(array $override_data, array $translatable) {
    return $filtered_data;
  }

  /**
   * Updates default configuration when new modules or themes are installed.
   */
  public function updateDefaultConfigLangcodes() {
    $this->isUpdatingFromLocale = TRUE;
    // Need to rewrite some default configuration language codes if the default
    // site language is not English.
    $default_langcode = $this->languageManager->getDefaultLanguage()->getId();
    if ($default_langcode != 'en') {
      // Update active configuration copies of all prior shipped configuration if
      // they are still English. It is not enough to change configuration shipped
      // with the components just installed, because installing a component such
      // as views or tour module may bring in default configuration from prior
      // components.
      $names = $this->getComponentNames();
      foreach ($names as $name) {
        $config = $this->configFactory->reset($name)->getEditable($name);
        // Should only update if still exists in active configuration. If locale
        // module is enabled later, then some configuration may not exist anymore.
        if (!$config->isNew()) {
          $langcode = $config->get('langcode');
          if (empty($langcode) || $langcode == 'en') {
            $config->set('langcode', $default_langcode)->save();
          }
        }
      }
    }
    $this->isUpdatingFromLocale = FALSE;
  }

}
+2 −0
Original line number Diff line number Diff line
@@ -8,3 +8,5 @@ locale_test_translate.settings:
      type: label
    translatable_default_with_no_translation:
      type: label
    key_set_during_install:
      type: boolean
Loading