Commit 5fe8034b authored by alexpott's avatar alexpott

Issue #2020361 by vijaycs85: Create LanguageConfigContext to activate...

Issue #2020361 by vijaycs85: Create LanguageConfigContext to activate configuration overrides based on language.
parent be586a99
...@@ -149,6 +149,75 @@ function testConfigLocaleUserOverride() { ...@@ -149,6 +149,75 @@ function testConfigLocaleUserOverride() {
$this->assertIdentical($config->get('foo'), 'en bar'); $this->assertIdentical($config->get('foo'), 'en bar');
} }
/**
* Tests locale override based on language.
*/
function testConfigLocaleLanguageOverride() {
$this->installSchema('system', 'variable');
$this->installSchema('language', 'language');
language_save(new Language(array(
'name' => 'French',
'langcode' => 'fr',
)));
language_save(new Language(array(
'name' => 'English',
'langcode' => 'en',
)));
language_save(new Language(array(
'name' => 'German',
'langcode' => 'de',
)));
$language = language_load('fr');
$language_config_context = config_context_enter('Drupal\language\LanguageConfigContext');
$language_config_context->setLanguage($language);
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'fr bar');
// Ensure the non-overridden value is still the same.
$this->assertIdentical($config->get('404'), 'herp');
// Ensure that we get the expected value when we leave the language context. The
// locale overrides contain an English override too, so although we are not
// in a language override context, the English language override
// applies due to the negotiated language for the page.
config_context_leave();
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'en bar');
$config_factory = \Drupal::service('config.factory');
$language = language_load('de');
$config_factory->enterContext($language_config_context->setLanguage($language));
// Should not have to re-initialize the configuration object to get new
// overrides as the new context will have a different uuid.
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'de bar');
// Enter an english context on top of the german context.
$language = language_load('en');
// Create a new language config context to stack on top of the existing one.
$en_language_config_context = config_context_enter('Drupal\language\LanguageConfigContext');
$en_language_config_context->setLanguage($language);
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'en bar');
// Ensure that we get the expected value when we leave the english
// language context.
config_context_leave();
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'de bar');
// Ensure that we get the expected value when we leave the german
// language context.
config_context_leave();
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'en bar');
// Ensure that we cannot leave the default context.
config_context_leave();
$config = config('config_test.system');
$this->assertIdentical($config->get('foo'), 'en bar');
}
/** /**
* Tests locale override in combination with global overrides. * Tests locale override in combination with global overrides.
*/ */
......
<?php
/**
* @file
* Contains \Drupal\language\LanguageConfigContext.
*/
namespace Drupal\language;
use Drupal\Core\Config\Context\ConfigContext;
use Drupal\Core\Language\Language;
/**
* Defines a configuration context object for a language.
*
* This should be used when configuration objects need a context for a language
* other than the current language.
*
*/
class LanguageConfigContext extends ConfigContext {
/**
* Predefined key for language object.
*/
const LANGUAGE_KEY = 'language';
/**
* Creates the configuration context for language.
*
* @param \Drupal\Core\Language\Language $language
* The language to add to the config context.
*
* @return \Drupal\Core\Language\Language
* The language config context object.
*/
public function setLanguage(Language $language) {
$this->set(self::LANGUAGE_KEY, $language);
// Re-initialize since the language change changes the context fundamentally.
$this->init();
return $this;
}
}
...@@ -62,10 +62,14 @@ public function __construct(LanguageManager $language_manager, ContextInterface ...@@ -62,10 +62,14 @@ public function __construct(LanguageManager $language_manager, ContextInterface
public function configContext(ConfigEvent $event) { public function configContext(ConfigEvent $event) {
$context = $event->getContext(); $context = $event->getContext();
// If there is a user set in the current context, set the language based on // If there is a language set explicitly in current context, use it.
// the preferred language of the user. Otherwise set it based on the // otherwise check if there is a user set in the current context,
// negotiated interface language. // to set the language based on the preferred language of the user.
if ($account = $context->get('user.account')) { // Otherwise set it based on the negotiated interface language.
if ($language = $context->get('language')) {
$context->set('locale.language', $language);
}
elseif ($account = $context->get('user.account')) {
$context->set('locale.language', language_load(user_preferred_langcode($account))); $context->set('locale.language', language_load(user_preferred_langcode($account)));
} }
elseif ($language = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)) { elseif ($language = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment