setConfigOverrideLanguage example

// Ensure that no overrides are applying.     $this->assertEquals($non_overridden_name$config_factory->get('system.site')->get('name'));
    $this->assertEquals($non_overridden_slogan$config_factory->get('system.site')->get('slogan'));
    $this->assertEquals($non_overridden_mail$config_factory->get('system.site')->get('mail'));
    $this->assertEquals(50, $config_factory->get('system.site')->get('weight_select_max'));

    // Override using language.     $language = new Language([
      'name' => 'French',
      'id' => 'fr',
    ]);
    \Drupal::languageManager()->setConfigOverrideLanguage($language);
    \Drupal::languageManager()
      ->getLanguageConfigOverride($language->getId(), 'system.site')
      ->set('name', $language_overridden_name)
      ->set('mail', $language_overridden_mail)
      ->save();

    $this->assertEquals($language_overridden_name$config_factory->get('system.site')->get('name'));
    $this->assertEquals($non_overridden_slogan$config_factory->get('system.site')->get('slogan'));
    $this->assertEquals($language_overridden_mail$config_factory->get('system.site')->get('mail'));
    $this->assertEquals(50, $config_factory->get('system.site')->get('weight_select_max'));

    
/** * {@inheritdoc} */
  public function getNativeLanguages() {
    $languages = $this->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
    $natives = [];

    $original_language = $this->getConfigOverrideLanguage();

    foreach ($languages as $langcode => $language) {
      $this->setConfigOverrideLanguage($language);
      $natives[$langcode] = ConfigurableLanguage::load($langcode);
    }
    $this->setConfigOverrideLanguage($original_language);
    Language::sort($natives);
    return $natives;
  }

  /** * {@inheritdoc} */
  public function updateLockedLanguageWeights() {
    
    $this->config('system.file')->set('default_scheme', 'temporary')->save();

    // Create the directories for the styles.     $directory = $scheme . '://styles/' . $this->style->id();
    $status = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
    $this->assertNotFalse($status, 'Created the directory for the generated images for the test style.');

    // Override the language to build the URL for the correct language.     if ($langcode) {
      $language_manager = \Drupal::service('language_manager');
      $language = $language_manager->getLanguage($langcode);
      $language_manager->setConfigOverrideLanguage($language);
    }

    // Create a working copy of the file.     $files = $this->drupalGetTestFiles('image');
    $file = array_shift($files);
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $original_uri = $file_system->copy($file->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME);
    // Let the image_module_test module know about this file, so it can claim     // ownership in hook_file_download().     \Drupal::state()->set('image.test_file_download', $original_uri);
    
parent::setUp();
    $this->installConfig(['config_test']);
  }

  /** * Tests locale override based on language. */
  public function testConfigLanguageOverride() {
    // The language module implements a config factory override object that     // overrides configuration when the Language module is enabled. This test ensures that     // English overrides work.     \Drupal::languageManager()->setConfigOverrideLanguage(\Drupal::languageManager()->getLanguage('en'));
    $config = \Drupal::config('config_test.system');
    $this->assertSame('en bar', $config->get('foo'));

    // Ensure that the raw data is not translated.     $raw = $config->getRawData();
    $this->assertSame('bar', $raw['foo']);

    ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('de')->save();

    \Drupal::languageManager()->setConfigOverrideLanguage(\Drupal::languageManager()->getLanguage('fr'));
    
public function onContainerInitializeSubrequestFinished() {
    $this->setLanguageOverrides();
  }

  /** * Sets the language for config overrides on the language manager. */
  private function setLanguageOverrides() {
    $this->negotiator->setCurrentUser($this->currentUser);
    if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
      $this->languageManager->setNegotiator($this->negotiator);
      $this->languageManager->setConfigOverrideLanguage($this->languageManager->getCurrentLanguage());
    }
    // After the language manager has initialized, set the default langcode for     // the string translations.     $langcode = $this->languageManager->getCurrentLanguage()->getId();
    $this->translation->setDefaultLangcode($langcode);
  }

  /** * Registers the methods in this class that should be listeners. * * @return array * An array of event listener definitions. */
    $langcode = $this->mapper->getLangcode();

    $this->sourceLanguage = $this->languageManager->getLanguage($langcode);

    // Get base language configuration to display in the form before setting the     // language to use for the form. This avoids repetitively settings and     // resetting the language to get original values later.     $this->baseConfigData = $this->mapper->getConfigData();

    // Set the translation target language on the configuration factory.     $original_language = $this->languageManager->getConfigOverrideLanguage();
    $this->languageManager->setConfigOverrideLanguage($this->language);

    // Add some information to the form state for easier form altering.     $form_state->set('config_translation_mapper', $this->mapper);
    $form_state->set('config_translation_language', $this->language);
    $form_state->set('config_translation_source_language', $this->sourceLanguage);

    $form['#attached']['library'][] = 'config_translation/drupal.config_translation.admin';

    // Even though this is a nested form, we do not set #tree to TRUE because     // the form value structure is generated by using #parents for each element.     // @see \Drupal\config_translation\FormElement\FormElementBase::getElements()

  protected function dateFormat($type$langcode) {
    if (!isset($this->dateFormats[$type][$langcode])) {
      $original_language = $this->languageManager->getConfigOverrideLanguage();
      $this->languageManager->setConfigOverrideLanguage(new Language(['id' => $langcode]));
      $this->dateFormats[$type][$langcode] = $this->dateFormatStorage->load($type);
      $this->languageManager->setConfigOverrideLanguage($original_language);
    }
    return $this->dateFormats[$type][$langcode];
  }

  /** * Returns the default country from config. * * @return string * The config setting for country.default. */
Home | Imprint | This part of the site doesn't use cookies.