You are here

trait CurrentLanguageResetTrait in GraphQL 8.4

Sets the current language for the current request.

Hierarchy

File

src/EventSubscriber/CurrentLanguageResetTrait.php, line 10

Namespace

Drupal\graphql\EventSubscriber
View source
trait CurrentLanguageResetTrait {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The language negotiator.
   *
   * @var \Drupal\language\LanguageNegotiatorInterface
   */
  protected $languageNegotiator;

  /**
   * The translator.
   *
   * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface
   */
  protected $translator;

  /**
   * The current user service.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Resets the global language context across different services.
   */
  protected function resetLanguageContext() : void {
    if (!isset($this->languageNegotiator)) {
      return;
    }
    if (!$this->languageManager
      ->isMultilingual()) {
      return;
    }
    $this->languageNegotiator
      ->setCurrentUser($this->currentUser);
    if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
      $this->languageManager
        ->setNegotiator($this->languageNegotiator);
      $this->languageManager
        ->setConfigOverrideLanguage($this->languageManager
        ->getCurrentLanguage());
    }

    // After the language manager has initialized, set the default langcode for
    // the string translations.
    if (method_exists($this->translator, 'setDefaultLangcode')) {
      $language = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
      $this->translator
        ->setDefaultLangcode($language);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentLanguageResetTrait::$currentUser protected property The current user service.
CurrentLanguageResetTrait::$languageManager protected property The language manager.
CurrentLanguageResetTrait::$languageNegotiator protected property The language negotiator.
CurrentLanguageResetTrait::$translator protected property The translator.
CurrentLanguageResetTrait::resetLanguageContext protected function Resets the global language context across different services.