Commit b7d1db55 authored by alexpott's avatar alexpott

Issue #2468767 by eiriksm, Gábor Hojtsy: English config source strings are...

Issue #2468767 by eiriksm, Gábor Hojtsy: English config source strings are saved as custom translations in locale in foreign install
parent a451002a
......@@ -82,7 +82,7 @@ public static function getSubscribedEvents() {
public function onConfigSave(ConfigCrudEvent $event) {
// Only attempt to feed back configuration translation changes to locale if
// the update itself was not initiated by locale data changes.
if (!$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
if (!drupal_installation_attempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
$config = $event->getConfig();
$langcode = $config->get('langcode') ?: 'en';
$this->updateLocaleStorage($config, $langcode);
......@@ -98,7 +98,7 @@ public function onConfigSave(ConfigCrudEvent $event) {
public function onOverrideChange(LanguageConfigOverrideCrudEvent $event) {
// Only attempt to feed back configuration override changes to locale if
// the update itself was not initiated by locale data changes.
if (!$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
if (!drupal_installation_attempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
$translation_config = $event->getLanguageConfigOverride();
$langcode = $translation_config->getLangcode();
$reference_config = $this->configFactory->getEditable($translation_config->getName())->get();
......@@ -204,23 +204,25 @@ protected function resetExistingTranslations($name, $translatable, $reference_co
* The source string value.
* @param string $context
* The source string context.
* @param string $translation
* @param string $new_translation
* The translation string.
* @param string $langcode
* The language code of the translation.
*/
protected function saveCustomizedTranslation($name, $source, $context, $translation, $langcode) {
protected function saveCustomizedTranslation($name, $source, $context, $new_translation, $langcode) {
$locale_translation = $this->localeConfigManager->getStringTranslation($name, $langcode, $source, $context);
if (!empty($locale_translation)) {
// Save this translation as custom if it was a new translation and not the
// same as the source. (The interface prefills translation values with the
// source). Or if there was an existing translation and the user changed
// it (even if it was changed back to the original value). Otherwise the
// translation file would be overwritten with the locale copy again later.
if (($locale_translation->isNew() && $source != $translation) ||
(!$locale_translation->isNew() && $translation != $locale_translation->getString())) {
// source). Or if there was an existing (non-empty) translation and the
// user changed it (even if it was changed back to the original value).
// Otherwise the translation file would be overwritten with the locale
// copy again later.
$existing_translation = $locale_translation->getString();
if (($locale_translation->isNew() && $source != $new_translation) ||
(!$locale_translation->isNew() && ((empty($existing_translation) && $source != $new_translation) || ((!empty($existing_translation) && $new_translation != $existing_translation))))) {
$locale_translation
->setString($translation)
->setString($new_translation)
->setCustomized(TRUE)
->save();
}
......
......@@ -53,6 +53,9 @@ protected function getPo($langcode) {
msgid "Anonymous"
msgstr "Anonymous $langcode"
msgid "Language"
msgstr "Language $langcode"
ENDPO;
}
......@@ -75,19 +78,7 @@ public function testTranslationsLoaded() {
}
// Verify the strings from the translation files were imported.
$test_samples = ['Save and continue', 'Anonymous'];
$langcodes = ['de', 'es'];
foreach($test_samples as $sample) {
foreach($langcodes as $langcode) {
$edit = array();
$edit['langcode'] = $langcode;
$edit['translation'] = 'translated';
$edit['string'] = $sample;
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
$this->assertText($sample . ' ' . $langcode);
}
}
$this->verifyImportedStringsTranslated();
/** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
$language_manager = \Drupal::languageManager();
......@@ -127,6 +118,17 @@ public function testTranslationsLoaded() {
$override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
$this->assertEqual($override_en->get('anonymous'), 'Anonymous');
}
// Activate a module, to make sure that config is not overridden by module
// installation.
$edit = array(
'modules[Core][views][enable]' => TRUE,
'modules[Core][filter][enable]' => TRUE,
);
$this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
// Verify the strings from the translation are still as expected.
$this->verifyImportedStringsTranslated();
}
else {
// Active configuration should be English.
......@@ -140,6 +142,26 @@ public function testTranslationsLoaded() {
// Spanish is always an override (never used as installation language).
$this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
}
/**
* Helper function to verify that the expected strings are translated.
*/
protected function verifyImportedStringsTranslated() {
$test_samples = ['Save and continue', 'Anonymous', 'Language'];
$langcodes = ['de', 'es'];
foreach($test_samples as $sample) {
foreach($langcodes as $langcode) {
$edit = array();
$edit['langcode'] = $langcode;
$edit['translation'] = 'translated';
$edit['string'] = $sample;
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
$this->assertText($sample . ' ' . $langcode);
}
}
}
}
......@@ -6,3 +6,4 @@ core: 8.x
hidden: true
dependencies:
- locale
- tour
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment