function LocaleConfigSubscriber::saveCustomizedTranslation

Same name in other branches
  1. 9 core/modules/locale/src/LocaleConfigSubscriber.php \Drupal\locale\LocaleConfigSubscriber::saveCustomizedTranslation()
  2. 10 core/modules/locale/src/LocaleConfigSubscriber.php \Drupal\locale\LocaleConfigSubscriber::saveCustomizedTranslation()
  3. 11.x core/modules/locale/src/LocaleConfigSubscriber.php \Drupal\locale\LocaleConfigSubscriber::saveCustomizedTranslation()

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.

2 calls to LocaleConfigSubscriber::saveCustomizedTranslation()
LocaleConfigSubscriber::processTranslatableData in core/modules/locale/src/LocaleConfigSubscriber.php
Process the translatable data array with a given language.
LocaleConfigSubscriber::resetExistingTranslations in core/modules/locale/src/LocaleConfigSubscriber.php
Reset existing locale translations to their source values.

File

core/modules/locale/src/LocaleConfigSubscriber.php, line 209

Class

LocaleConfigSubscriber
Updates strings translation when configuration translations change.

Namespace

Drupal\locale

Code

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 (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($new_translation)
                ->setCustomized(TRUE)
                ->save();
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.