summaryrefslogtreecommitdiffstats
path: root/core/modules/locale/lib/Drupal/locale/LocaleConfigSubscriber.php
blob: 3d2fd4ae0e3f0f510cece3639f88aa35eb1ddbf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
 * @file
 * Definition of Drupal\locale\LocaleConfigSubscriber.
 */

namespace Drupal\locale;

use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigEvent;
use Drupal\Core\Config\StorageDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Locale Config helper
 *
 * $config is always a DrupalConfig object.
 */
class LocaleConfigSubscriber implements EventSubscriberInterface {
  /**
   * Override configuration values with localized data.
   *
   * @param Drupal\Core\Config\ConfigEvent $event
   *   The Event to process.
   */
  public function configLoad(ConfigEvent $event) {
    $config = $event->getConfig();
    $language = language(LANGUAGE_TYPE_INTERFACE);
    $locale_name = $this->getLocaleConfigName($config->getName(), $language);
    if ($override = $config->getStorage()->read($locale_name)) {
      $config->setOverride($override);
    }
  }

  /**
   * Get configuration name for this language.
   *
   * It will be the same name with a prefix depending on language code:
   * locale.config.LANGCODE.NAME
   */
  public function getLocaleConfigName($name, $language) {
    return 'locale.config.' . $language->langcode . '.' . $name;
  }

  /**
   * Implements EventSubscriberInterface::getSubscribedEvents().
   */
  static function getSubscribedEvents() {
    $events['config.load'][] = array('configLoad', 20);
    return $events;
  }
}