Commit 67c8a34c authored by alexpott's avatar alexpott

Issue #2107427 by penyaskito, dclavain, Gábor Hojtsy, mashermike, Aron Novak,...

Issue #2107427 by penyaskito, dclavain, Gábor Hojtsy, mashermike, Aron Novak, YesCT, szato, Wim Leers, mikispeed, vijaycs85: Fixed Regression: Language names should display in their native names in the language switcher block.
parent 2132a56e
...@@ -162,6 +162,14 @@ public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) { ...@@ -162,6 +162,14 @@ public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) {
return $filtered_languages; return $filtered_languages;
} }
/**
* {@inheritdoc}
*/
public function getNativeLanguages() {
// In a language unaware site we don't have translated languages.
return $this->getLanguages();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -97,6 +97,15 @@ public function getDefaultLanguage(); ...@@ -97,6 +97,15 @@ public function getDefaultLanguage();
*/ */
public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE); public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE);
/**
* Returns a list of languages set up on the site in their native form.
*
* @return \Drupal\Core\Language\LanguageInterface[]
* An associative array of languages, keyed by the language code, ordered
* by weight ascending and name ascending.
*/
public function getNativeLanguages();
/** /**
* Returns a language object from the given language code. * Returns a language object from the given language code.
* *
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
use Drupal\Core\Language\LanguageDefault; use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageManager; use Drupal\Core\Language\LanguageManager;
use Drupal\language\Config\LanguageConfigFactoryOverrideInterface; use Drupal\language\Config\LanguageConfigFactoryOverrideInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
...@@ -272,7 +273,7 @@ public function getLanguages($flags = BaseLanguageInterface::STATE_CONFIGURABLE) ...@@ -272,7 +273,7 @@ public function getLanguages($flags = BaseLanguageInterface::STATE_CONFIGURABLE)
$default = $this->getDefaultLanguage(); $default = $this->getDefaultLanguage();
$this->languages = array($default->id => $default); $this->languages = array($default->id => $default);
// Retrieve the config storage to list available languages. // Retrieve the list of languages defined in configuration.
$prefix = 'language.entity.'; $prefix = 'language.entity.';
$config_ids = $this->configFactory->listAll($prefix); $config_ids = $this->configFactory->listAll($prefix);
...@@ -299,6 +300,24 @@ public function getLanguages($flags = BaseLanguageInterface::STATE_CONFIGURABLE) ...@@ -299,6 +300,24 @@ public function getLanguages($flags = BaseLanguageInterface::STATE_CONFIGURABLE)
return parent::getLanguages($flags); return parent::getLanguages($flags);
} }
/**
* {@inheritdoc}
*/
public function getNativeLanguages() {
$languages = $this->getLanguages(BaseLanguageInterface::STATE_CONFIGURABLE);
$natives = array();
$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} * {@inheritdoc}
*/ */
......
...@@ -131,11 +131,11 @@ function getLanguageSwitchLinks(Request $request, $type, $path) { ...@@ -131,11 +131,11 @@ function getLanguageSwitchLinks(Request $request, $type, $path) {
$query = array(); $query = array();
parse_str($request->getQueryString(), $query); parse_str($request->getQueryString(), $query);
foreach ($this->languageManager->getLanguages() as $language) { foreach ($this->languageManager->getNativeLanguages() as $language) {
$langcode = $language->id; $langcode = $language->id;
$links[$langcode] = array( $links[$langcode] = array(
'href' => $path, 'href' => $path,
'title' => $language->name, 'title' => $language->getName(),
'attributes' => array('class' => array('language-link')), 'attributes' => array('class' => array('language-link')),
'query' => $query, 'query' => $query,
); );
......
...@@ -190,10 +190,10 @@ public function processOutbound($path, &$options = array(), Request $request = N ...@@ -190,10 +190,10 @@ public function processOutbound($path, &$options = array(), Request $request = N
function getLanguageSwitchLinks(Request $request, $type, $path) { function getLanguageSwitchLinks(Request $request, $type, $path) {
$links = array(); $links = array();
foreach ($this->languageManager->getLanguages() as $language) { foreach ($this->languageManager->getNativeLanguages() as $language) {
$links[$language->id] = array( $links[$language->id] = array(
'href' => $path, 'href' => $path,
'title' => $language->name, 'title' => $language->getName(),
'language' => $language, 'language' => $language,
'attributes' => array('class' => array('language-link')), 'attributes' => array('class' => array('language-link')),
); );
......
...@@ -22,7 +22,7 @@ class LanguageSwitchingTest extends WebTestBase { ...@@ -22,7 +22,7 @@ class LanguageSwitchingTest extends WebTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('language', 'block', 'language_test'); public static $modules = array('locale', 'language', 'block', 'language_test');
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
...@@ -36,23 +36,26 @@ protected function setUp() { ...@@ -36,23 +36,26 @@ protected function setUp() {
* Functional tests for the language switcher block. * Functional tests for the language switcher block.
*/ */
function testLanguageBlock() { function testLanguageBlock() {
// Enable the language switching block..
$block = $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, array(
'id' => 'test_language_block',
// Ensure a 2-byte UTF-8 sequence is in the tested output.
'label' => $this->randomMachineName(8) . '×',
));
// Add language. // Add language.
$edit = array( $edit = array(
'predefined_langcode' => 'fr', 'predefined_langcode' => 'fr',
); );
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
// Set the native language name.
$this->saveNativeLanguageName('fr', 'français');
// Enable URL language detection and selection. // Enable URL language detection and selection.
$edit = array('language_interface[enabled][language-url]' => '1'); $edit = array('language_interface[enabled][language-url]' => '1');
$this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
// Enable the language switching block.
$block = $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, array(
'id' => 'test_language_block',
// Ensure a 2-byte UTF-8 sequence is in the tested output.
'label' => $this->randomMachineName(8) . '×',
));
$this->doTestLanguageBlockAuthenticated($block->label()); $this->doTestLanguageBlockAuthenticated($block->label());
$this->doTestLanguageBlockAnonymous($block->label()); $this->doTestLanguageBlockAnonymous($block->label());
} }
...@@ -75,6 +78,7 @@ protected function doTestLanguageBlockAuthenticated($block_label) { ...@@ -75,6 +78,7 @@ protected function doTestLanguageBlockAuthenticated($block_label) {
list($language_switcher) = $this->xpath('//div[@id=:id]/div[contains(@class, "content")]', array(':id' => 'block-test-language-block')); list($language_switcher) = $this->xpath('//div[@id=:id]/div[contains(@class, "content")]', array(':id' => 'block-test-language-block'));
$list_items = array(); $list_items = array();
$anchors = array(); $anchors = array();
$labels = array();
foreach ($language_switcher->ul->li as $list_item) { foreach ($language_switcher->ul->li as $list_item) {
$classes = explode(" ", (string) $list_item['class']); $classes = explode(" ", (string) $list_item['class']);
list($langcode) = array_intersect($classes, array('en', 'fr')); list($langcode) = array_intersect($classes, array('en', 'fr'));
...@@ -86,6 +90,7 @@ protected function doTestLanguageBlockAuthenticated($block_label) { ...@@ -86,6 +90,7 @@ protected function doTestLanguageBlockAuthenticated($block_label) {
'hreflang' => (string) $list_item->a['hreflang'], 'hreflang' => (string) $list_item->a['hreflang'],
'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'], 'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'],
); );
$labels[] = (string) $list_item->a;
} }
$expected_list_items = array( $expected_list_items = array(
0 => array('langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'), 0 => array('langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'),
...@@ -101,6 +106,7 @@ protected function doTestLanguageBlockAuthenticated($block_label) { ...@@ -101,6 +106,7 @@ protected function doTestLanguageBlockAuthenticated($block_label) {
$this->assertIdentical($settings['path']['currentPath'], 'user/2', 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.'); $this->assertIdentical($settings['path']['currentPath'], 'user/2', 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.');
$this->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.'); $this->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.');
$this->assertIdentical($settings['path']['currentLanguage'], 'en', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.'); $this->assertIdentical($settings['path']['currentLanguage'], 'en', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
$this->assertIdentical($labels, array('English', 'français'), 'The language links labels are in their own language on the language switcher block.');
} }
/** /**
...@@ -128,6 +134,7 @@ protected function doTestLanguageBlockAnonymous($block_label) { ...@@ -128,6 +134,7 @@ protected function doTestLanguageBlockAnonymous($block_label) {
'active' => array(), 'active' => array(),
'inactive' => array(), 'inactive' => array(),
); );
$labels = array();
foreach ($language_switcher->ul->li as $link) { foreach ($language_switcher->ul->li as $link) {
$classes = explode(" ", (string) $link['class']); $classes = explode(" ", (string) $link['class']);
list($langcode) = array_intersect($classes, array('en', 'fr')); list($langcode) = array_intersect($classes, array('en', 'fr'));
...@@ -144,9 +151,11 @@ protected function doTestLanguageBlockAnonymous($block_label) { ...@@ -144,9 +151,11 @@ protected function doTestLanguageBlockAnonymous($block_label) {
else { else {
$anchors['inactive'][] = $langcode; $anchors['inactive'][] = $langcode;
} }
$labels[] = (string) $link->a;
} }
$this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language list item is marked as active on the language switcher block.'); $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language list item is marked as active on the language switcher block.');
$this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language anchor is marked as active on the language switcher block.'); $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language anchor is marked as active on the language switcher block.');
$this->assertIdentical($labels, array('English', 'français'), 'The language links labels are in their own language on the language switcher block.');
} }
/** /**
...@@ -276,4 +285,17 @@ protected function doTestLanguageLinkActiveClassAnonymous() { ...@@ -276,4 +285,17 @@ protected function doTestLanguageLinkActiveClassAnonymous() {
$this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is marked active.', array(':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode))); $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is marked active.', array(':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode)));
} }
/**
* Saves the native name of a language entity in configuration as a label.
*
* @param string $langcode
* The language code of the language.
* @param string $label
* The native name of the language.
*/
protected function saveNativeLanguageName($langcode, $label) {
\Drupal::service('language.config_factory_override')
->getOverride($langcode, 'language.entity.' . $langcode)->set('label', $label)->save();
}
} }
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