class LocaleConfigSubscriber implements EventSubscriberInterface (View source)

Updates strings translation when configuration translations change.

This reacts to the updates of translated active configuration and configuration language overrides. When those updates involve configuration which was available as default configuration, we need to feed back changes to any item which was originally part of that configuration to the interface translation storage. Those updated translations are saved as customized, so further community translation updates will not undo user changes.

This subscriber does not respond to deleting active configuration or deleting configuration translations. The locale storage is additive and we cannot be sure that only a given configuration translation used a source string. So we should not remove the translations from locale storage in these cases. The configuration or override would itself be deleted either way.

By design locale module only deals with sources in English.

Properties

protected ConfigFactoryInterface $configFactory

The configuration factory.

protected LocaleConfigManager $localeConfigManager

The typed configuration manager.

protected LanguageManagerInterface $languageManager

The language manager.

Methods

__construct(ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager)

Constructs a LocaleConfigSubscriber.

static 
getSubscribedEvents()

{@inheritdoc}

onConfigSave(ConfigCrudEvent $event)

Updates the locale strings when a translated active configuration is saved.

onOverrideChange(LanguageConfigOverrideCrudEvent $event)

Updates the locale strings when a configuration override is saved/deleted.

updateLocaleStorage(StorableConfigBase $config, string $langcode, array $reference_config = [])

Update locale storage based on configuration translations.

processTranslatableData(string $name, array $config, array $translatable, string $langcode, array $reference_config = [])

Process the translatable data array with a given language.

resetExistingTranslations(string $name, array|TranslatableMarkup $translatable, array|string $reference_config, string $langcode)

Reset existing locale translations to their source values.

saveCustomizedTranslation(string $name, string $source, string $context, string $new_translation, string $langcode)

Saves a translation string and marks it as customized.

Details

__construct(ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager)

Constructs a LocaleConfigSubscriber.

Parameters

ConfigFactoryInterface $config_factory

The configuration factory.

LocaleConfigManager $locale_config_manager

The typed configuration manager.

static getSubscribedEvents()

{@inheritdoc}

onConfigSave(ConfigCrudEvent $event)

Updates the locale strings when a translated active configuration is saved.

Parameters

ConfigCrudEvent $event

The configuration event.

onOverrideChange(LanguageConfigOverrideCrudEvent $event)

Updates the locale strings when a configuration override is saved/deleted.

Parameters

LanguageConfigOverrideCrudEvent $event

The language configuration event.

updateLocaleStorage(StorableConfigBase $config, string $langcode, array $reference_config = [])

Update locale storage based on configuration translations.

Parameters

StorableConfigBase $config

Active configuration or configuration translation override.

string $langcode

The language code of $config.

array $reference_config

(Optional) Reference configuration to check against if $config was an override. This allows us to update locale keys for data not in the override but still in the active configuration.

protected processTranslatableData(string $name, array $config, array $translatable, string $langcode, array $reference_config = [])

Process the translatable data array with a given language.

Parameters

string $name

The configuration name.

array $config

The active configuration data or override data.

array $translatable

The translatable array structure. \Drupal\locale\LocaleConfigManager::getTranslatableData()

string $langcode

The language code to process the array with.

array $reference_config

(Optional) Reference configuration to check against if $config was an override. This allows us to update locale keys for data not in the override but still in the active configuration.

protected resetExistingTranslations(string $name, array|TranslatableMarkup $translatable, array|string $reference_config, string $langcode)

Reset existing locale translations to their source values.

Goes through $translatable to reset any existing translations to the source string, so prior translations would not reappear in the configuration.

Parameters

string $name

The configuration name.

array|TranslatableMarkup $translatable

Either a possibly nested array with TranslatableMarkup objects at the leaf items or a TranslatableMarkup object directly.

array|string $reference_config

Either a possibly nested array with strings at the leaf items or a string directly. Only those $translatable items that are also present in $reference_config will get translations reset.

string $langcode

The language code of the translation being processed.

protected saveCustomizedTranslation(string $name, string $source, string $context, string $new_translation, string $langcode)

Saves a translation string and marks it as customized.

Parameters

string $name

The configuration name.

string $source

The source string value.

string $context

The source string context.

string $new_translation

The translation string.

string $langcode

The language code of the translation.