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.

8.0.x
Alex Pott 2014-09-10 12:02:58 +01:00
parent 2132a56eb3
commit 67c8a34c07
6 changed files with 71 additions and 13 deletions

View File

@ -162,6 +162,14 @@ class LanguageManager implements LanguageManagerInterface {
return $filtered_languages;
}
/**
* {@inheritdoc}
*/
public function getNativeLanguages() {
// In a language unaware site we don't have translated languages.
return $this->getLanguages();
}
/**
* {@inheritdoc}
*/

View File

@ -97,6 +97,15 @@ interface LanguageManagerInterface {
*/
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.
*

View File

@ -15,6 +15,7 @@ use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageManager;
use Drupal\language\Config\LanguageConfigFactoryOverrideInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
@ -272,7 +273,7 @@ class ConfigurableLanguageManager extends LanguageManager implements Configurabl
$default = $this->getDefaultLanguage();
$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.';
$config_ids = $this->configFactory->listAll($prefix);
@ -299,6 +300,24 @@ class ConfigurableLanguageManager extends LanguageManager implements Configurabl
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}
*/

View File

@ -131,11 +131,11 @@ class LanguageNegotiationSession extends LanguageNegotiationMethodBase implement
$query = array();
parse_str($request->getQueryString(), $query);
foreach ($this->languageManager->getLanguages() as $language) {
foreach ($this->languageManager->getNativeLanguages() as $language) {
$langcode = $language->id;
$links[$langcode] = array(
'href' => $path,
'title' => $language->name,
'title' => $language->getName(),
'attributes' => array('class' => array('language-link')),
'query' => $query,
);

View File

@ -190,10 +190,10 @@ class LanguageNegotiationUrl extends LanguageNegotiationMethodBase implements In
function getLanguageSwitchLinks(Request $request, $type, $path) {
$links = array();
foreach ($this->languageManager->getLanguages() as $language) {
foreach ($this->languageManager->getNativeLanguages() as $language) {
$links[$language->id] = array(
'href' => $path,
'title' => $language->name,
'title' => $language->getName(),
'language' => $language,
'attributes' => array('class' => array('language-link')),
);

View File

@ -22,7 +22,7 @@ class LanguageSwitchingTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('language', 'block', 'language_test');
public static $modules = array('locale', 'language', 'block', 'language_test');
protected function setUp() {
parent::setUp();
@ -36,23 +36,26 @@ class LanguageSwitchingTest extends WebTestBase {
* Functional tests for the language switcher block.
*/
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.
$edit = array(
'predefined_langcode' => 'fr',
);
$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.
$edit = array('language_interface[enabled][language-url]' => '1');
$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->doTestLanguageBlockAnonymous($block->label());
}
@ -75,6 +78,7 @@ class LanguageSwitchingTest extends WebTestBase {
list($language_switcher) = $this->xpath('//div[@id=:id]/div[contains(@class, "content")]', array(':id' => 'block-test-language-block'));
$list_items = array();
$anchors = array();
$labels = array();
foreach ($language_switcher->ul->li as $list_item) {
$classes = explode(" ", (string) $list_item['class']);
list($langcode) = array_intersect($classes, array('en', 'fr'));
@ -86,6 +90,7 @@ class LanguageSwitchingTest extends WebTestBase {
'hreflang' => (string) $list_item->a['hreflang'],
'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'],
);
$labels[] = (string) $list_item->a;
}
$expected_list_items = array(
0 => array('langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'),
@ -101,6 +106,7 @@ class LanguageSwitchingTest extends WebTestBase {
$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']['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 @@ class LanguageSwitchingTest extends WebTestBase {
'active' => array(),
'inactive' => array(),
);
$labels = array();
foreach ($language_switcher->ul->li as $link) {
$classes = explode(" ", (string) $link['class']);
list($langcode) = array_intersect($classes, array('en', 'fr'));
@ -144,9 +151,11 @@ class LanguageSwitchingTest extends WebTestBase {
else {
$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($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 @@ class LanguageSwitchingTest extends WebTestBase {
$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();
}
}