You are here

public function LocaleConfigManager::updateConfigTranslations in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/src/LocaleConfigManager.php \Drupal\locale\LocaleConfigManager::updateConfigTranslations()
  2. 9 core/modules/locale/src/LocaleConfigManager.php \Drupal\locale\LocaleConfigManager::updateConfigTranslations()

Updates all configuration translations for the names / languages provided.

To be used when interface translation changes result in the need to update configuration translations to keep them in sync.

Parameters

array $names: Array of names of configuration objects to update.

array $langcodes: (optional) Array of language codes to update. Defaults to all configurable languages.

Return value

int Total number of configuration override and active configuration objects updated (saved or removed).

File

core/modules/locale/src/LocaleConfigManager.php, line 565

Class

LocaleConfigManager
Manages configuration supported in part by interface translation.

Namespace

Drupal\locale

Code

public function updateConfigTranslations(array $names, array $langcodes = []) {
  $langcodes = $langcodes ? $langcodes : array_keys($this->languageManager
    ->getLanguages());
  $count = 0;
  foreach ($names as $name) {
    $translatable = $this
      ->getTranslatableDefaultConfig($name);
    if (empty($translatable)) {

      // If there is nothing translatable in this configuration or not
      // supported, skip it.
      continue;
    }
    $active_langcode = $this
      ->getActiveConfigLangcode($name);
    $active = $this->configStorage
      ->read($name);
    foreach ($langcodes as $langcode) {
      $processed = $this
        ->processTranslatableData($name, $active, $translatable, $langcode);

      // If the language code is not the same as the active storage
      // language, we should update the configuration override.
      if ($langcode != $active_langcode) {
        $override = $this->languageManager
          ->getLanguageConfigOverride($langcode, $name);

        // Filter out locale managed configuration keys so that translations
        // removed from Locale will be reflected in the config override.
        $data = $this
          ->filterOverride($override
          ->get(), $translatable);
        if (!empty($processed)) {

          // Merge in the Locale managed translations with existing data.
          $data = NestedArray::mergeDeepArray([
            $data,
            $processed,
          ], TRUE);
        }
        if (empty($data) && !$override
          ->isNew()) {

          // The configuration override contains Locale overrides that no
          // longer exist.
          $this
            ->deleteTranslationOverride($name, $langcode);
          $count++;
        }
        elseif (!empty($data)) {

          // Update translation data in configuration override.
          $this
            ->saveTranslationOverride($name, $langcode, $data);
          $count++;
        }
      }
      elseif (locale_is_translatable($langcode)) {

        // If the language code is the active storage language, we should
        // update. If it is English, we should only update if English is also
        // translatable.
        $active = NestedArray::mergeDeepArray([
          $active,
          $processed,
        ], TRUE);
        $this
          ->saveTranslationActive($name, $active);
        $count++;
      }
    }
  }
  return $count;
}