You are here

protected function MailHandler::changeActiveLanguage in Commerce Core 8.2

Changes the active language for translations.

Parameters

string $langcode: The langcode.

1 call to MailHandler::changeActiveLanguage()
MailHandler::sendMail in src/MailHandler.php
Sends an email to a user.

File

src/MailHandler.php, line 121

Class

MailHandler

Namespace

Drupal\commerce

Code

protected function changeActiveLanguage($langcode) {
  if (!$this->languageManager
    ->isMultilingual()) {
    return;
  }
  $language = $this->languageManager
    ->getLanguage($langcode);
  if (!$language) {
    return;
  }

  // The language manager has no method for overriding the default
  // language, like it does for config overrides. We have to change the
  // default language service's current language.
  // @see https://www.drupal.org/project/drupal/issues/3029010
  $this->languageDefault
    ->set($language);
  $this->languageManager
    ->setConfigOverrideLanguage($language);
  $this->languageManager
    ->reset();

  // The default string_translation service, TranslationManager, has a
  // setDefaultLangcode method. However, this method is not present on
  // either of its interfaces. Therefore we check for the concrete class
  // here so that any swapped service does not break the application.
  // @see https://www.drupal.org/project/drupal/issues/3029003
  $string_translation = $this
    ->getStringTranslation();
  if ($string_translation instanceof TranslationManager) {
    $string_translation
      ->setDefaultLangcode($language
      ->getId());
    $string_translation
      ->reset();
  }
}