Chris@0: configStorage = $config_storage; Chris@0: $this->localeStorage = $locale_storage; Chris@0: $this->configFactory = $config_factory; Chris@0: $this->typedConfigManager = $typed_config; Chris@0: $this->languageManager = $language_manager; Chris@0: $this->defaultConfigStorage = $default_config_storage; Chris@0: $this->configManager = $config_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets array of translated strings for Locale translatable configuration. Chris@0: * Chris@0: * @param string $name Chris@0: * Configuration object name. Chris@0: * Chris@0: * @return array Chris@0: * Array of Locale translatable elements of the default configuration in Chris@0: * $name. Chris@0: */ Chris@0: public function getTranslatableDefaultConfig($name) { Chris@0: if ($this->isSupported($name)) { Chris@0: // Create typed configuration wrapper based on install storage data. Chris@0: $data = $this->defaultConfigStorage->read($name); Chris@0: $typed_config = $this->typedConfigManager->createFromNameAndData($name, $data); Chris@0: if ($typed_config instanceof TraversableTypedDataInterface) { Chris@0: return $this->getTranslatableData($typed_config); Chris@0: } Chris@0: } Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets translatable configuration data for a typed configuration element. Chris@0: * Chris@0: * @param \Drupal\Core\TypedData\TypedDataInterface $element Chris@0: * Typed configuration element. Chris@0: * Chris@0: * @return array|\Drupal\Core\StringTranslation\TranslatableMarkup Chris@0: * A nested array matching the exact structure under $element with only the Chris@0: * elements that are translatable wrapped into a TranslatableMarkup. If the Chris@0: * provided $element is not traversable, the return value is a single Chris@0: * TranslatableMarkup. Chris@0: */ Chris@0: protected function getTranslatableData(TypedDataInterface $element) { Chris@0: $translatable = []; Chris@0: if ($element instanceof TraversableTypedDataInterface) { Chris@0: foreach ($element as $key => $property) { Chris@0: $value = $this->getTranslatableData($property); Chris@0: if (!empty($value)) { Chris@0: $translatable[$key] = $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: else { Chris@0: // Something is only translatable by Locale if there is a string in the Chris@0: // first place. Chris@0: $value = $element->getValue(); Chris@0: $definition = $element->getDataDefinition(); Chris@0: if (!empty($definition['translatable']) && $value !== '' && $value !== NULL) { Chris@0: $options = []; Chris@0: if (isset($definition['translation context'])) { Chris@0: $options['context'] = $definition['translation context']; Chris@0: } Chris@0: return new TranslatableMarkup($value, [], $options); Chris@0: } Chris@0: } Chris@0: return $translatable; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Process the translatable data array with a given language. Chris@0: * Chris@0: * If the given language is translatable, will return the translated copy Chris@0: * which will only contain strings that had translations. If the given Chris@0: * language is English and is not translatable, will return a simplified Chris@0: * array of the English source strings only. Chris@0: * Chris@0: * @param string $name Chris@0: * The configuration name. Chris@0: * @param array $active Chris@0: * The active configuration data. Chris@0: * @param array|\Drupal\Core\StringTranslation\TranslatableMarkup[] $translatable Chris@0: * The translatable array structure. A nested array matching the exact Chris@0: * structure under of the default configuration for $name with only the Chris@0: * elements that are translatable wrapped into a TranslatableMarkup. Chris@0: * @param string $langcode Chris@0: * The language code to process the array with. Chris@0: * Chris@0: * @return array Chris@0: * Processed translatable data array. Will only contain translations Chris@0: * different from source strings or in case of untranslatable English, the Chris@0: * source strings themselves. Chris@0: * Chris@0: * @see self::getTranslatableData() Chris@0: */ Chris@0: protected function processTranslatableData($name, array $active, array $translatable, $langcode) { Chris@0: $translated = []; Chris@0: foreach ($translatable as $key => $item) { Chris@0: if (!isset($active[$key])) { Chris@0: continue; Chris@0: } Chris@0: if (is_array($item)) { Chris@0: // Only add this key if there was a translated value underneath. Chris@0: $value = $this->processTranslatableData($name, $active[$key], $item, $langcode); Chris@0: if (!empty($value)) { Chris@0: $translated[$key] = $value; Chris@0: } Chris@0: } Chris@0: else { Chris@0: if (locale_is_translatable($langcode)) { Chris@0: $value = $this->translateString($name, $langcode, $item->getUntranslatedString(), $item->getOption('context')); Chris@0: } Chris@0: else { Chris@0: $value = $item->getUntranslatedString(); Chris@0: } Chris@0: if (!empty($value)) { Chris@0: $translated[$key] = $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $translated; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Saves translated configuration override. Chris@0: * Chris@0: * @param string $name Chris@0: * Configuration object name. Chris@0: * @param string $langcode Chris@0: * Language code. Chris@0: * @param array $data Chris@0: * Configuration data to be saved, that will be only the translated values. Chris@0: */ Chris@0: protected function saveTranslationOverride($name, $langcode, array $data) { Chris@0: $this->isUpdatingFromLocale = TRUE; Chris@0: $this->languageManager->getLanguageConfigOverride($langcode, $name)->setData($data)->save(); Chris@0: $this->isUpdatingFromLocale = FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Saves translated configuration data. Chris@0: * Chris@0: * @param string $name Chris@0: * Configuration object name. Chris@0: * @param array $data Chris@0: * Configuration data to be saved with translations merged in. Chris@0: */ Chris@0: protected function saveTranslationActive($name, array $data) { Chris@0: $this->isUpdatingFromLocale = TRUE; Chris@0: $this->configFactory->getEditable($name)->setData($data)->save(); Chris@0: $this->isUpdatingFromLocale = FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes translated configuration data. Chris@0: * Chris@0: * @param string $name Chris@0: * Configuration object name. Chris@0: * @param string $langcode Chris@0: * Language code. Chris@0: */ Chris@0: protected function deleteTranslationOverride($name, $langcode) { Chris@0: $this->isUpdatingFromLocale = TRUE; Chris@0: $this->languageManager->getLanguageConfigOverride($langcode, $name)->delete(); Chris@0: $this->isUpdatingFromLocale = FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets configuration names associated with components. Chris@0: * Chris@0: * @param array $components Chris@0: * (optional) Array of component lists indexed by type. If not present or it Chris@0: * is an empty array, it will update all components. Chris@0: * Chris@0: * @return array Chris@0: * Array of configuration object names. Chris@0: */ Chris@0: public function getComponentNames(array $components = []) { Chris@0: $components = array_filter($components); Chris@0: if ($components) { Chris@0: $names = []; Chris@0: foreach ($components as $type => $list) { Chris@0: // InstallStorage::getComponentNames returns a list of folders keyed by Chris@0: // config name. Chris@0: $names = array_merge($names, $this->defaultConfigStorage->getComponentNames($type, $list)); Chris@0: } Chris@0: return $names; Chris@0: } Chris@0: else { Chris@0: return $this->defaultConfigStorage->listAll(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets configuration names associated with strings. Chris@0: * Chris@0: * @param array $lids Chris@0: * Array with string identifiers. Chris@0: * Chris@0: * @return array Chris@0: * Array of configuration object names. Chris@0: */ Chris@0: public function getStringNames(array $lids) { Chris@0: $names = []; Chris@0: $locations = $this->localeStorage->getLocations(['sid' => $lids, 'type' => 'configuration']); Chris@0: foreach ($locations as $location) { Chris@0: $names[$location->name] = $location->name; Chris@0: } Chris@0: return $names; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes configuration for language. Chris@0: * Chris@0: * @param string $langcode Chris@0: * Language code to delete. Chris@0: */ Chris@0: public function deleteLanguageTranslations($langcode) { Chris@0: $this->isUpdatingFromLocale = TRUE; Chris@0: $storage = $this->languageManager->getLanguageConfigOverrideStorage($langcode); Chris@0: foreach ($storage->listAll() as $name) { Chris@0: $this->languageManager->getLanguageConfigOverride($langcode, $name)->delete(); Chris@0: } Chris@0: $this->isUpdatingFromLocale = FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Translates string using the localization system. Chris@0: * Chris@0: * So far we only know how to translate strings from English so the source Chris@0: * string should be in English. Chris@0: * Unlike regular t() translations, strings will be added to the source Chris@0: * tables only if this is marked as default data. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of the configuration location. Chris@0: * @param string $langcode Chris@0: * Language code to translate to. Chris@0: * @param string $source Chris@0: * The source string, should be English. Chris@0: * @param string $context Chris@0: * The string context. Chris@0: * Chris@0: * @return string|false Chris@0: * Translated string if there is a translation, FALSE if not. Chris@0: */ Chris@0: public function translateString($name, $langcode, $source, $context) { Chris@0: if ($source) { Chris@0: // If translations for a language have not been loaded yet. Chris@0: if (!isset($this->translations[$name][$langcode])) { Chris@0: // Preload all translations for this configuration name and language. Chris@0: $this->translations[$name][$langcode] = []; Chris@0: foreach ($this->localeStorage->getTranslations(['language' => $langcode, 'type' => 'configuration', 'name' => $name]) as $string) { Chris@0: $this->translations[$name][$langcode][$string->context][$string->source] = $string; Chris@0: } Chris@0: } Chris@0: if (!isset($this->translations[$name][$langcode][$context][$source])) { Chris@0: // There is no translation of the source string in this config location Chris@0: // to this language for this context. Chris@0: if ($translation = $this->localeStorage->findTranslation(['source' => $source, 'context' => $context, 'language' => $langcode])) { Chris@0: // Look for a translation of the string. It might have one, but not Chris@0: // be saved in this configuration location yet. Chris@0: // If the string has a translation for this context to this language, Chris@0: // save it in the configuration location so it can be looked up faster Chris@0: // next time. Chris@0: $this->localeStorage->createString((array) $translation) Chris@0: ->addLocation('configuration', $name) Chris@0: ->save(); Chris@0: } Chris@0: else { Chris@0: // No translation was found. Add the source to the configuration Chris@0: // location so it can be translated, and the string is faster to look Chris@0: // for next time. Chris@0: $translation = $this->localeStorage Chris@0: ->createString(['source' => $source, 'context' => $context]) Chris@0: ->addLocation('configuration', $name) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: // Add an entry, either the translation found, or a blank string object Chris@0: // to track the source string, to this configuration location, language, Chris@0: // and context. Chris@0: $this->translations[$name][$langcode][$context][$source] = $translation; Chris@0: } Chris@0: Chris@0: // Return the string only when the string object had a translation. Chris@0: if ($this->translations[$name][$langcode][$context][$source]->isTranslation()) { Chris@0: return $this->translations[$name][$langcode][$context][$source]->getString(); Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reset static cache of configuration string translations. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function reset() { Chris@0: $this->translations = []; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the translation object for the given source/context and language. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of the configuration location. Chris@0: * @param string $langcode Chris@0: * Language code to translate to. Chris@0: * @param string $source Chris@0: * The source string, should be English. Chris@0: * @param string $context Chris@0: * The string context. Chris@0: * Chris@0: * @return \Drupal\locale\TranslationString|false Chris@0: * The translation object if the string was not empty or FALSE otherwise. Chris@0: */ Chris@0: public function getStringTranslation($name, $langcode, $source, $context) { Chris@0: if ($source) { Chris@0: $this->translateString($name, $langcode, $source, $context); Chris@0: if ($string = $this->translations[$name][$langcode][$context][$source]) { Chris@0: if (!$string->isTranslation()) { Chris@0: $conditions = ['lid' => $string->lid, 'language' => $langcode]; Chris@0: $translation = $this->localeStorage->createTranslation($conditions); Chris@0: $this->translations[$name][$langcode][$context][$source] = $translation; Chris@0: return $translation; Chris@0: } Chris@0: else { Chris@0: return $string; Chris@0: } Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether a language has configuration translation. Chris@0: * Chris@0: * @param string $name Chris@0: * Configuration name. Chris@0: * @param string $langcode Chris@0: * A language code. Chris@0: * Chris@0: * @return bool Chris@0: * A boolean indicating if a language has configuration translations. Chris@0: */ Chris@0: public function hasTranslation($name, $langcode) { Chris@0: $translation = $this->languageManager->getLanguageConfigOverride($langcode, $name); Chris@0: return !$translation->isNew(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the original language code for this shipped configuration. Chris@0: * Chris@0: * @param string $name Chris@0: * The configuration name. Chris@0: * Chris@0: * @return null|string Chris@0: * Language code of the default configuration for $name. If the default Chris@0: * configuration data for $name did not contain a language code, it is Chris@0: * assumed to be English. The return value is NULL if no such default Chris@0: * configuration exists. Chris@0: */ Chris@0: public function getDefaultConfigLangcode($name) { Chris@0: // Config entities that do not have the 'default_config_hash' cannot be Chris@0: // shipped configuration regardless of whether there is a name match. Chris@0: // configurable_language entities are a special case since they can be Chris@0: // translated regardless of whether they are shipped if they in the standard Chris@0: // language list. Chris@0: $config_entity_type = $this->configManager->getEntityTypeIdByName($name); Chris@0: if (!$config_entity_type || $config_entity_type === 'configurable_language' Chris@0: || !empty($this->configFactory->get($name)->get('_core.default_config_hash')) Chris@0: ) { Chris@0: $shipped = $this->defaultConfigStorage->read($name); Chris@0: if (!empty($shipped)) { Chris@0: return !empty($shipped['langcode']) ? $shipped['langcode'] : 'en'; Chris@0: } Chris@0: } Chris@0: return NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the current language code for this active configuration. Chris@0: * Chris@0: * @param string $name Chris@0: * The configuration name. Chris@0: * Chris@0: * @return null|string Chris@0: * Language code of the current active configuration for $name. If the Chris@0: * configuration data for $name did not contain a language code, it is Chris@0: * assumed to be English. The return value is NULL if no such active Chris@0: * configuration exists. Chris@0: */ Chris@0: public function getActiveConfigLangcode($name) { Chris@0: $active = $this->configStorage->read($name); Chris@0: if (!empty($active)) { Chris@0: return !empty($active['langcode']) ? $active['langcode'] : 'en'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Whether the given configuration is supported for interface translation. Chris@0: * Chris@0: * @param string $name Chris@0: * The configuration name. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if interface translation is supported. Chris@0: */ Chris@0: public function isSupported($name) { Chris@0: return $this->getDefaultConfigLangcode($name) == 'en' && $this->configStorage->read($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Indicates whether configuration translations are being updated from locale. Chris@0: * Chris@0: * @return bool Chris@0: * Whether or not configuration translations are currently being updated. Chris@0: * If TRUE, LocaleConfigManager is in control of the process and the Chris@0: * reference data is locale's storage. Changes made to active configuration Chris@0: * and overrides in this case should not feed back to locale storage. Chris@0: * On the other hand, when not updating from locale and configuration Chris@0: * translations change, we need to feed back to the locale storage. Chris@0: */ Chris@0: public function isUpdatingTranslationsFromLocale() { Chris@0: return $this->isUpdatingFromLocale; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates all configuration translations for the names / languages provided. Chris@0: * Chris@0: * To be used when interface translation changes result in the need to update Chris@0: * configuration translations to keep them in sync. Chris@0: * Chris@0: * @param array $names Chris@0: * Array of names of configuration objects to update. Chris@0: * @param array $langcodes Chris@0: * (optional) Array of language codes to update. Defaults to all Chris@0: * configurable languages. Chris@0: * Chris@0: * @return int Chris@0: * Total number of configuration override and active configuration objects Chris@0: * updated (saved or removed). Chris@0: */ Chris@0: public function updateConfigTranslations(array $names, array $langcodes = []) { Chris@0: $langcodes = $langcodes ? $langcodes : array_keys($this->languageManager->getLanguages()); Chris@0: $count = 0; Chris@0: foreach ($names as $name) { Chris@0: $translatable = $this->getTranslatableDefaultConfig($name); Chris@0: if (empty($translatable)) { Chris@0: // If there is nothing translatable in this configuration or not Chris@0: // supported, skip it. Chris@0: continue; Chris@0: } Chris@0: Chris@0: $active_langcode = $this->getActiveConfigLangcode($name); Chris@0: $active = $this->configStorage->read($name); Chris@0: Chris@0: foreach ($langcodes as $langcode) { Chris@0: $processed = $this->processTranslatableData($name, $active, $translatable, $langcode); Chris@0: // If the language code is not the same as the active storage Chris@0: // language, we should update the configuration override. Chris@0: if ($langcode != $active_langcode) { Chris@0: $override = $this->languageManager->getLanguageConfigOverride($langcode, $name); Chris@0: // Filter out locale managed configuration keys so that translations Chris@0: // removed from Locale will be reflected in the config override. Chris@0: $data = $this->filterOverride($override->get(), $translatable); Chris@0: if (!empty($processed)) { Chris@0: // Merge in the Locale managed translations with existing data. Chris@0: $data = NestedArray::mergeDeepArray([$data, $processed], TRUE); Chris@0: } Chris@0: if (empty($data) && !$override->isNew()) { Chris@0: // The configuration override contains Locale overrides that no Chris@0: // longer exist. Chris@0: $this->deleteTranslationOverride($name, $langcode); Chris@0: $count++; Chris@0: } Chris@0: elseif (!empty($data)) { Chris@0: // Update translation data in configuration override. Chris@0: $this->saveTranslationOverride($name, $langcode, $data); Chris@0: $count++; Chris@0: } Chris@0: } Chris@0: elseif (locale_is_translatable($langcode)) { Chris@0: // If the language code is the active storage language, we should Chris@0: // update. If it is English, we should only update if English is also Chris@0: // translatable. Chris@0: $active = NestedArray::mergeDeepArray([$active, $processed], TRUE); Chris@0: $this->saveTranslationActive($name, $active); Chris@0: $count++; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $count; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Filters override data based on default translatable items. Chris@0: * Chris@0: * @param array $override_data Chris@0: * Configuration override data. Chris@0: * @param array $translatable Chris@0: * Translatable data array. @see self::getTranslatableData() Chris@0: * @return array Chris@0: * Nested array of any items of $override_data which did not have keys in Chris@0: * $translatable. May be empty if $override_data only had items which were Chris@0: * also in $translatable. Chris@0: */ Chris@0: protected function filterOverride(array $override_data, array $translatable) { Chris@0: $filtered_data = []; Chris@0: foreach ($override_data as $key => $value) { Chris@0: if (isset($translatable[$key])) { Chris@0: // If the translatable default configuration has this key, look further Chris@0: // for subkeys or ignore this element for scalar values. Chris@0: if (is_array($value)) { Chris@0: $value = $this->filterOverride($value, $translatable[$key]); Chris@0: if (!empty($value)) { Chris@0: $filtered_data[$key] = $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: else { Chris@0: // If this key was not in the translatable default configuration, Chris@0: // keep it. Chris@0: $filtered_data[$key] = $value; Chris@0: } Chris@0: } Chris@0: return $filtered_data; Chris@0: } Chris@0: Chris@0: }