diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index 557faad..eafcd5d 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -27,13 +27,6 @@ class LanguageManager implements LanguageManagerInterface { protected $translation; /** - * An array of all the available languages keyed by language code. - * - * @var \Drupal\Core\Language\LanguageInterface[] - */ - protected $languages; - - /** * The default language object. * * @var \Drupal\Core\Language\LanguageDefault @@ -130,37 +123,17 @@ public function getDefaultLanguage() { * {@inheritdoc} */ public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) { - // Initialize master language list. - if (!isset($this->languages)) { - // No language module, so use the default language only. - $default = $this->getDefaultLanguage(); - $this->languages = array($default->getId() => $default); - // Add the special languages, they will be filtered later if needed. - $this->languages += $this->getDefaultLockedLanguages($default->getWeight()); - } + + // No language module, so use the default language only. + $default = $this->getDefaultLanguage(); + $languages = array($default->getId() => $default); + // Add the special languages, they will be filtered later if needed. + $languages += $this->getDefaultLockedLanguages($default->getWeight()); // Filter the full list of languages based on the value of the $all flag. By // default we remove the locked languages, but the caller may request for // those languages to be added as well. - $filtered_languages = array(); - - // Add the site's default language if flagged as allowed value. - if ($flags & LanguageInterface::STATE_SITE_DEFAULT) { - $default = isset($default) ? $default : $this->getDefaultLanguage(); - // Rename the default language. But we do not want to do this globally, - // if we're acting on a global object, so clone the object first. - $default = clone $default; - $default->name = $this->t("Site's default language (@lang_name)", array('@lang_name' => $default->name)); - $filtered_languages[LanguageInterface::LANGCODE_SITE_DEFAULT] = $default; - } - - foreach ($this->languages as $id => $language) { - if (($language->isLocked() && ($flags & LanguageInterface::STATE_LOCKED)) || (!$language->isLocked() && ($flags & LanguageInterface::STATE_CONFIGURABLE))) { - $filtered_languages[$id] = $language; - } - } - - return $filtered_languages; + return $this->languageFilter($languages, $flags); } /** @@ -384,4 +357,42 @@ public function getConfigOverrideLanguage() { return $this->getCurrentLanguage(); } + + /** + * Filters the full list of languages based on the value of the flag. + * + * By default removes the locked languages. + * + * @param \Drupal\Core\Language\LanguageInterface[] $languages + * Array with languages to be filtered. + * + * @param int $flags + * (optional) Specifies the state of the languages that have to be returned. + * It can be: LanguageInterface::STATE_CONFIGURABLE, + * LanguageInterface::STATE_LOCKED, LanguageInterface::STATE_ALL. + * + * @return \Drupal\Core\Language\LanguageInterface[] + * An associative array of languages, keyed by the language code. + */ + protected function languageFilter(array $languages, $flags = LanguageInterface::STATE_CONFIGURABLE) { + $filtered_languages = array(); + // Add the site's default language if flagged as allowed value. + if ($flags & LanguageInterface::STATE_SITE_DEFAULT) { + $default = $this->getDefaultLanguage(); + // Rename the default language. But we do not want to do this globally, + // if we're acting on a global object, so clone the object first. + $default = clone $default; + $default->name = $this->t("Site's default language (@lang_name)", array('@lang_name' => $default->name)); + $filtered_languages[LanguageInterface::STATE_SITE_DEFAULT] = $default; + } + + foreach ($languages as $id => $language) { + if (($language->isLocked() && ($flags & LanguageInterface::STATE_LOCKED)) || (!$language->isLocked() && ($flags & LanguageInterface::STATE_CONFIGURABLE))) { + $filtered_languages[$id] = $language; + } + } + + return $filtered_languages; + } + } diff --git a/core/modules/language/src/ConfigurableLanguageManager.php b/core/modules/language/src/ConfigurableLanguageManager.php index d6d5075..4d1dec2 100644 --- a/core/modules/language/src/ConfigurableLanguageManager.php +++ b/core/modules/language/src/ConfigurableLanguageManager.php @@ -247,7 +247,6 @@ public function reset($type = NULL) { $this->negotiatedMethods = array(); $this->languageTypes = NULL; $this->languageTypesInfo = NULL; - $this->languages = NULL; if ($this->negotiator) { $this->negotiator->reset(); } @@ -279,37 +278,35 @@ public function setNegotiator(LanguageNegotiatorInterface $negotiator) { * {@inheritdoc} */ public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) { - if (!isset($this->languages)) { - // Prepopulate the language list with the default language to keep things - // working even if we have no configuration. - $default = $this->getDefaultLanguage(); - $this->languages = array($default->getId() => $default); - - // Retrieve the list of languages defined in configuration. - $prefix = 'language.entity.'; - $config_ids = $this->configFactory->listAll($prefix); - - // Instantiate languages from config objects. - $weight = 0; - foreach ($this->configFactory->loadMultiple($config_ids) as $config) { - $data = $config->get(); - $langcode = $data['id']; - // Initialize default property so callers have an easy reference and can - // save the same object without data loss. - $data['default'] = ($langcode == $default->getId()); - $data['name'] = $data['label']; - $this->languages[$langcode] = new Language($data); - $weight = max(array($weight, $this->languages[$langcode]->getWeight())); - } + // Prepopulate the language list with the default language to keep things + // working even if we have no configuration. + $default = $this->getDefaultLanguage(); + $languages = array($default->getId() => $default); - // Add locked languages, they will be filtered later if needed. - $this->languages += $this->getDefaultLockedLanguages($weight); + // Retrieve the list of languages defined in configuration. + $prefix = 'language.entity.'; + $config_ids = $this->configFactory->listAll($prefix); - // Sort the language list by weight then title. - Language::sort($this->languages); + // Instantiate languages from config objects. + $weight = 0; + foreach ($this->configFactory->loadMultiple($config_ids) as $config) { + $data = $config->get(); + $langcode = $data['id']; + // Initialize default property so callers have an easy reference and can + // save the same object without data loss. + $data['default'] = ($langcode == $default->getId()); + $data['name'] = $data['label']; + $languages[$langcode] = new Language($data); + $weight = max(array($weight, $languages[$langcode]->getWeight())); } - return parent::getLanguages($flags); + // Add locked languages, they will be filtered later if needed. + $languages += $this->getDefaultLockedLanguages($weight); + + // Sort the language list by weight then title. + Language::sort($languages); + + return $this->languageFilter($languages, $flags); } /** diff --git a/core/modules/language/src/Tests/LanguageSelectorTranslatableTest.php b/core/modules/language/src/Tests/LanguageSelectorTranslatableTest.php new file mode 100644 index 0000000..9fff59e --- /dev/null +++ b/core/modules/language/src/Tests/LanguageSelectorTranslatableTest.php @@ -0,0 +1,90 @@ +administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator'); + $this->drupalLogin($this->administrator); + } + + /** + * Returns an array of permissions needed for the translator. + */ + protected function getAdministratorPermissions() { + return array_filter( + array('translate interface', + 'administer content translation', + 'create content translations', + 'update content translations', + 'delete content translations', + 'administer languages' + ) + ); + } + + /** + * Tests Content Translation language selectors are correctly translatable. + */ + public function testLanguageStringSelector() { + // Add another language. + $edit = array('predefined_langcode' => 'es'); + $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); + + // Translate the string English in Spanish (Inglés). Override config entity. + $name_translation = 'Inglés'; + \Drupal::languageManager()->getLanguageConfigOverride('es', 'language.entity.en')->set('label', $name_translation)->save(); + + // Check content translation overview selector. + $path = 'es/admin/config/regional/content-language'; + $this->drupalGet($path); + + // Get en language from selector. + $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => 'edit-settings-node-node-settings-language-langcode', ':option' => 'en')); + + // Check that the language text is translated. + $this->assertEqual((string) $elements[0], $name_translation, 'Checking the option string English is translated to Spanish.'); + } + +}