Locale Config helper
$config is always a DrupalConfig object.
Expanded class hierarchy of LocaleConfigSubscriber
class LocaleConfigSubscriber implements EventSubscriberInterface {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Default configuration context.
*
* @var \Drupal\Core\Config\Context\ContextInterface
*/
protected $defaultConfigContext;
/**
* Constructs a LocaleConfigSubscriber object.
*
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager service.
* @param \Drupal\Core\Config\Context\ConfigContext $config_context
* The configuration context service.
*/
public function __construct(LanguageManager $language_manager, ContextInterface $config_context) {
$this->languageManager = $language_manager;
$this->defaultConfigContext = $config_context;
}
/**
* Initializes configuration context with language.
*
* @param \Drupal\Core\Config\ConfigEvent $event
* The Event to process.
*/
public function configContext(ConfigEvent $event) {
$context = $event
->getContext();
// If there is a user set in the current context, set the language based on
// the preferred language of the user. Otherwise set it based on the
// negotiated interface language.
if ($account = $context
->get('user.account')) {
$context
->set('locale.language', language_load(user_preferred_langcode($account)));
}
elseif ($language = $this->languageManager
->getLanguage(Language::TYPE_INTERFACE)) {
$context
->set('locale.language', $language);
}
}
/**
* Override configuration values with localized data.
*
* @param \Drupal\Core\Config\ConfigEvent $event
* The Event to process.
*/
public function configLoad(ConfigEvent $event) {
$context = $event
->getContext();
if ($language = $context
->get('locale.language')) {
$config = $event
->getConfig();
$locale_name = $this
->getLocaleConfigName($config
->getName(), $language);
// Check to see if the config storage has an appropriately named file
// containing override data.
if ($override = $event
->getConfig()
->getStorage()
->read($locale_name)) {
$config
->setOverride($override);
}
}
}
/**
* Sets the negotiated interface language on the default configuration context.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* Kernel event to respond to.
*/
public function onKernelRequestSetDefaultConfigContextLocale(GetResponseEvent $event) {
// Re-initialize the default configuration context to ensure any cached
// configuration object are reset and can be translated. This will invoke
// the config context event which will retrieve the negotiated language
// from the language manager in configContext().
$this->defaultConfigContext
->init();
}
/**
* Get configuration name for this language.
*
* It will be the same name with a prefix depending on language code:
* locale.config.LANGCODE.NAME
*
* @param string $name
* The name of the config object.
* @param \Drupal\Core\Language\Language $language
* The language object.
*
* @return string
* The localized config name.
*/
public function getLocaleConfigName($name, Language $language) {
return 'locale.config.' . $language->langcode . '.' . $name;
}
/**
* Implements EventSubscriberInterface::getSubscribedEvents().
*/
static function getSubscribedEvents() {
$events['config.context'][] = array(
'configContext',
20,
);
$events['config.load'][] = array(
'configLoad',
20,
);
$events[KernelEvents::REQUEST][] = array(
'onKernelRequestSetDefaultConfigContextLocale',
20,
);
return $events;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LocaleConfigSubscriber:: |
protected | property | Default configuration context. | |
LocaleConfigSubscriber:: |
protected | property | The language manager. | |
LocaleConfigSubscriber:: |
public | function | Initializes configuration context with language. | |
LocaleConfigSubscriber:: |
public | function | Override configuration values with localized data. | |
LocaleConfigSubscriber:: |
public | function | Get configuration name for this language. | |
LocaleConfigSubscriber:: |
static | function |
Implements EventSubscriberInterface::getSubscribedEvents(). Overrides EventSubscriberInterface:: |
|
LocaleConfigSubscriber:: |
public | function | Sets the negotiated interface language on the default configuration context. | |
LocaleConfigSubscriber:: |
public | function | Constructs a LocaleConfigSubscriber object. |