LanguageRequestSubscriber.php 3.36 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\language\EventSubscriber\LanguageRequestSubscriber.
6
7
 */

8
namespace Drupal\language\EventSubscriber;
9

10
use Drupal\Core\Session\AccountInterface;
11
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
12
13
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\LanguageNegotiatorInterface;
14
15
16
17
18
19
20
21
22
23
24
25
26
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Sets the $request property on the language manager.
 */
class LanguageRequestSubscriber implements EventSubscriberInterface {

  /**
   * The language manager service.
   *
27
   * @var \Drupal\language\ConfigurableLanguageManagerInterface
28
29
30
   */
  protected $languageManager;

31
32
33
34
35
36
37
  /**
   * The language negotiator.
   *
   * @var \Drupal\language\LanguageNegotiatorInterface
   */
  protected $negotiator;

38
39
40
  /**
   * The translation service.
   *
41
   * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface;
42
43
44
   */
  protected $translation;

45
46
47
48
49
50
51
  /**
   * The current active user.
   *
   * @return \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

52
53
54
  /**
   * Constructs a LanguageRequestSubscriber object.
   *
55
   * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
56
   *   The language manager service.
57
58
   * @param \Drupal\language\LanguageNegotiatorInterface
   *   The language negotiator.
59
   * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translation;
60
   *   The translation service.
61
62
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current active user.
63
   */
64
  public function __construct(ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, TranslatorInterface $translation, AccountInterface $current_user) {
65
    $this->languageManager = $language_manager;
66
    $this->negotiator = $negotiator;
67
    $this->translation = $translation;
68
    $this->currentUser = $current_user;
69
70
71
72
73
74
75
76
77
78
  }

  /**
   * Sets the request on the language manager.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function onKernelRequestLanguage(GetResponseEvent $event) {
    if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
79
80
81
82
83
84
      $request = $event->getRequest();
      $this->negotiator->setCurrentUser($this->currentUser);
      $this->negotiator->setRequest($request);
      if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
        $this->languageManager->setNegotiator($this->negotiator);
        $this->languageManager->setRequest($request);
85
        $this->languageManager->setConfigOverrideLanguage($this->languageManager->getCurrentLanguage());
86
      }
87
88
      // After the language manager has initialized, set the default langcode
      // for the string translations.
89
      $langcode = $this->languageManager->getCurrentLanguage()->id;
90
      $this->translation->setDefaultLangcode($langcode);
91
92
93
94
95
96
97
98
99
100
    }
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
101
    $events[KernelEvents::REQUEST][] = array('onKernelRequestLanguage', 255);
102
103
104
105
106

    return $events;
  }

}