LocaleConfigSubscriber.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Drupal\locale;
  3. use Drupal\Core\Config\ConfigCrudEvent;
  4. use Drupal\Core\Config\ConfigEvents;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Drupal\Core\Config\StorableConfigBase;
  7. use Drupal\language\Config\LanguageConfigOverrideCrudEvent;
  8. use Drupal\language\Config\LanguageConfigOverrideEvents;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11. * Updates strings translation when configuration translations change.
  12. *
  13. * This reacts to the updates of translated active configuration and
  14. * configuration language overrides. When those updates involve configuration
  15. * which was available as default configuration, we need to feed back changes
  16. * to any item which was originally part of that configuration to the interface
  17. * translation storage. Those updated translations are saved as customized, so
  18. * further community translation updates will not undo user changes.
  19. *
  20. * This subscriber does not respond to deleting active configuration or deleting
  21. * configuration translations. The locale storage is additive and we cannot be
  22. * sure that only a given configuration translation used a source string. So
  23. * we should not remove the translations from locale storage in these cases. The
  24. * configuration or override would itself be deleted either way.
  25. *
  26. * By design locale module only deals with sources in English.
  27. *
  28. * @see \Drupal\locale\LocaleConfigManager
  29. */
  30. class LocaleConfigSubscriber implements EventSubscriberInterface {
  31. /**
  32. * The configuration factory.
  33. *
  34. * @var \Drupal\Core\Config\ConfigFactoryInterface
  35. */
  36. protected $configFactory;
  37. /**
  38. * The typed configuration manager.
  39. *
  40. * @var \Drupal\locale\LocaleConfigManager
  41. */
  42. protected $localeConfigManager;
  43. /**
  44. * Constructs a LocaleConfigSubscriber.
  45. *
  46. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  47. * The configuration factory.
  48. * @param \Drupal\locale\LocaleConfigManager $locale_config_manager
  49. * The typed configuration manager.
  50. */
  51. public function __construct(ConfigFactoryInterface $config_factory, LocaleConfigManager $locale_config_manager) {
  52. $this->configFactory = $config_factory;
  53. $this->localeConfigManager = $locale_config_manager;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public static function getSubscribedEvents() {
  59. $events[LanguageConfigOverrideEvents::SAVE_OVERRIDE] = 'onOverrideChange';
  60. $events[LanguageConfigOverrideEvents::DELETE_OVERRIDE] = 'onOverrideChange';
  61. $events[ConfigEvents::SAVE] = 'onConfigSave';
  62. return $events;
  63. }
  64. /**
  65. * Updates the locale strings when a translated active configuration is saved.
  66. *
  67. * @param \Drupal\Core\Config\ConfigCrudEvent $event
  68. * The configuration event.
  69. */
  70. public function onConfigSave(ConfigCrudEvent $event) {
  71. // Only attempt to feed back configuration translation changes to locale if
  72. // the update itself was not initiated by locale data changes.
  73. if (!drupal_installation_attempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
  74. $config = $event->getConfig();
  75. $langcode = $config->get('langcode') ?: 'en';
  76. $this->updateLocaleStorage($config, $langcode);
  77. }
  78. }
  79. /**
  80. * Updates the locale strings when a configuration override is saved/deleted.
  81. *
  82. * @param \Drupal\language\Config\LanguageConfigOverrideCrudEvent $event
  83. * The language configuration event.
  84. */
  85. public function onOverrideChange(LanguageConfigOverrideCrudEvent $event) {
  86. // Only attempt to feed back configuration override changes to locale if
  87. // the update itself was not initiated by locale data changes.
  88. if (!drupal_installation_attempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
  89. $translation_config = $event->getLanguageConfigOverride();
  90. $langcode = $translation_config->getLangcode();
  91. $reference_config = $this->configFactory->getEditable($translation_config->getName())->get();
  92. $this->updateLocaleStorage($translation_config, $langcode, $reference_config);
  93. }
  94. }
  95. /**
  96. * Update locale storage based on configuration translations.
  97. *
  98. * @param \Drupal\Core\Config\StorableConfigBase $config
  99. * Active configuration or configuration translation override.
  100. * @param string $langcode
  101. * The language code of $config.
  102. * @param array $reference_config
  103. * (Optional) Reference configuration to check against if $config was an
  104. * override. This allows us to update locale keys for data not in the
  105. * override but still in the active configuration.
  106. */
  107. protected function updateLocaleStorage(StorableConfigBase $config, $langcode, array $reference_config = []) {
  108. $name = $config->getName();
  109. if ($this->localeConfigManager->isSupported($name) && locale_is_translatable($langcode)) {
  110. $translatables = $this->localeConfigManager->getTranslatableDefaultConfig($name);
  111. $this->processTranslatableData($name, $config->get(), $translatables, $langcode, $reference_config);
  112. }
  113. }
  114. /**
  115. * Process the translatable data array with a given language.
  116. *
  117. * @param string $name
  118. * The configuration name.
  119. * @param array $config
  120. * The active configuration data or override data.
  121. * @param array|\Drupal\Core\StringTranslation\TranslatableMarkup[] $translatable
  122. * The translatable array structure.
  123. * @see \Drupal\locale\LocaleConfigManager::getTranslatableData()
  124. * @param string $langcode
  125. * The language code to process the array with.
  126. * @param array $reference_config
  127. * (Optional) Reference configuration to check against if $config was an
  128. * override. This allows us to update locale keys for data not in the
  129. * override but still in the active configuration.
  130. */
  131. protected function processTranslatableData($name, array $config, array $translatable, $langcode, array $reference_config = []) {
  132. foreach ($translatable as $key => $item) {
  133. if (!isset($config[$key])) {
  134. if (isset($reference_config[$key])) {
  135. $this->resetExistingTranslations($name, $translatable[$key], $reference_config[$key], $langcode);
  136. }
  137. continue;
  138. }
  139. if (is_array($item)) {
  140. $reference_config = isset($reference_config[$key]) ? $reference_config[$key] : [];
  141. $this->processTranslatableData($name, $config[$key], $item, $langcode, $reference_config);
  142. }
  143. else {
  144. $this->saveCustomizedTranslation($name, $item->getUntranslatedString(), $item->getOption('context'), $config[$key], $langcode);
  145. }
  146. }
  147. }
  148. /**
  149. * Reset existing locale translations to their source values.
  150. *
  151. * Goes through $translatable to reset any existing translations to the source
  152. * string, so prior translations would not reappear in the configuration.
  153. *
  154. * @param string $name
  155. * The configuration name.
  156. * @param array|\Drupal\Core\StringTranslation\TranslatableMarkup $translatable
  157. * Either a possibly nested array with TranslatableMarkup objects at the
  158. * leaf items or a TranslatableMarkup object directly.
  159. * @param array|string $reference_config
  160. * Either a possibly nested array with strings at the leaf items or a string
  161. * directly. Only those $translatable items that are also present in
  162. * $reference_config will get translations reset.
  163. * @param string $langcode
  164. * The language code of the translation being processed.
  165. */
  166. protected function resetExistingTranslations($name, $translatable, $reference_config, $langcode) {
  167. if (is_array($translatable)) {
  168. foreach ($translatable as $key => $item) {
  169. if (isset($reference_config[$key])) {
  170. // Process further if the key still exists in the reference active
  171. // configuration and the default translation but not the current
  172. // configuration override.
  173. $this->resetExistingTranslations($name, $item, $reference_config[$key], $langcode);
  174. }
  175. }
  176. }
  177. elseif (!is_array($reference_config)) {
  178. $this->saveCustomizedTranslation($name, $translatable->getUntranslatedString(), $translatable->getOption('context'), $reference_config, $langcode);
  179. }
  180. }
  181. /**
  182. * Saves a translation string and marks it as customized.
  183. *
  184. * @param string $name
  185. * The configuration name.
  186. * @param string $source
  187. * The source string value.
  188. * @param string $context
  189. * The source string context.
  190. * @param string $new_translation
  191. * The translation string.
  192. * @param string $langcode
  193. * The language code of the translation.
  194. */
  195. protected function saveCustomizedTranslation($name, $source, $context, $new_translation, $langcode) {
  196. $locale_translation = $this->localeConfigManager->getStringTranslation($name, $langcode, $source, $context);
  197. if (!empty($locale_translation)) {
  198. // Save this translation as custom if it was a new translation and not the
  199. // same as the source. (The interface prefills translation values with the
  200. // source). Or if there was an existing (non-empty) translation and the
  201. // user changed it (even if it was changed back to the original value).
  202. // Otherwise the translation file would be overwritten with the locale
  203. // copy again later.
  204. $existing_translation = $locale_translation->getString();
  205. if (($locale_translation->isNew() && $source != $new_translation) ||
  206. (!$locale_translation->isNew() && ((empty($existing_translation) && $source != $new_translation) || ((!empty($existing_translation) && $new_translation != $existing_translation))))) {
  207. $locale_translation
  208. ->setString($new_translation)
  209. ->setCustomized(TRUE)
  210. ->save();
  211. }
  212. }
  213. }
  214. }