Commit 2095c89f authored by alexpott's avatar alexpott

Issue #365615 followup by attiks, YesCT, Gábor Hojtsy, mgifford, Albert...

Issue #365615 followup by attiks, YesCT, Gábor Hojtsy, mgifford, Albert Volkman, plach, webwarrior, David_Rothstein, penyaskito, smokris | yang_yi_cn: Fixed Followups: Language detection not working correctly for most Chinese readers (and add a user interface for all browser language mappings).
parent 1a222d4d
......@@ -115,9 +115,6 @@ function theme_language_negotiation_configure_browser_form_table($variables) {
$links['delete'] = array(
'title' => t('Delete'),
'url' => Url::fromRoute('language.negotiation_browser_delete', ['browser_langcode' => $key]),
'attributes' => array(
'class' => array('image-style-link'),
),
);
$row[] = array(
'data' => array(
......@@ -131,7 +128,7 @@ function theme_language_negotiation_configure_browser_form_table($variables) {
$header = array(
t('Browser language code'),
t('Drupal language'),
t('Site language'),
t('Operations'),
);
......@@ -139,7 +136,8 @@ function theme_language_negotiation_configure_browser_form_table($variables) {
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'lang-neg-browser'),
'#empty' => t('No browser language mappings available.'),
'#attributes' => array('id' => 'language-negotiation-browser'),
);
$output = drupal_render($table);
......
......@@ -63,7 +63,7 @@ function language_help($route_name, RouteMatchInterface $route_match) {
return $output;
case 'language.negotiation_browser':
$output = '<p>' . t('Browsers use different language codes to refer to the same languages. You can add and edit mappings from browser language codes to the <a href="!configure-languages">languages used</a>.', array('!configure-languages' => \Drupal::url('language.admin_overview'))) . '</p>';
$output = '<p>' . t('Browsers use different language codes to refer to the same languages. Internally, a best effort is made to determine the correct language based on the code that the browser sends. You can add and edit additional mappings from browser language codes to <a href="!configure-languages">site languages</a>.', array('!configure-languages' => \Drupal::url('language.admin_overview'))) . '</p>';
return $output;
case 'language.negotiation_selected':
......
......@@ -65,6 +65,14 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
if (array_key_exists($this->browserLangcode, $mappings)) {
unset($mappings[$this->browserLangcode]);
language_set_browser_drupal_langcode_mappings($mappings);
$args = array(
'%browser' => $this->browserLangcode,
);
$this->logger('language')->notice('The browser language detection mapping for the %browser browser language code has been deleted.', $args);
drupal_set_message($this->t('The mapping for the %browser browser language code has been deleted.', $args));
}
$form_state->setRedirect('language.negotiation_browser');
......
......@@ -89,12 +89,16 @@ public function buildForm(array $form, FormStateInterface $form_state) {
foreach ($mappings as $browser_langcode => $drupal_langcode) {
$form['mappings'][$browser_langcode] = array(
'browser_langcode' => array(
'#title' => $this->t('Browser language code'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#default_value' => $browser_langcode,
'#size' => 20,
'#required' => TRUE,
),
'drupal_langcode' => array(
'#title' => $this->t('Site language'),
'#title_display' => 'invisible',
'#type' => 'select',
'#options' => $language_options,
'#default_value' => $drupal_langcode,
......@@ -113,14 +117,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => $this->t('Browser language code'),
'#description' => $this->t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
'#default_value' => '',
'#size' => 20,
);
$form['new_mapping']['drupal_langcode'] = array(
'#type' => 'select',
'#title' => $this->t('Drupal language'),
'#title' => $this->t('Site language'),
'#options' => $language_options,
'#default_value' => '',
);
return parent::buildForm($form, $form_state);
......@@ -134,16 +136,15 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$unique_values = array();
// Check all mappings.
$mappings = array();
if ($form_state->hasValue('mappings')) {
$mappings = $form_state->getValue('mappings');
foreach ($mappings as $key => $data) {
// Make sure browser_langcode is unique.
if (array_key_exists($data['browser_langcode'], $unique_values)) {
$form_state->setErrorByName('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes must be unique.'));
$form_state->setErrorByName('mappings][new_mapping][browser_langcode', $this->t('Browser language codes must be unique.'));
}
elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
$form_state->setErrorByName('mappings][' . $key . '][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
$form_state->setErrorByName('mappings][new_mapping][browser_langcode', $this->t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
}
$unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
}
......@@ -175,7 +176,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$config->setData($mappings);
$config->save();
}
$form_state->setRedirect('language.negotiation');
parent::submitForm($form, $form_state);
}
......
......@@ -186,6 +186,14 @@ function testUIBrowserLanguageMappings() {
$edit = array();
$this->drupalPostForm('admin/config/regional/language/detection/browser/delete/' . $browser_langcode, $edit, t('Confirm'));
// We need raw here because %browser will add HTML.
$t_args = array(
'%browser' => $browser_langcode,
);
$this->assertRaw(t('The mapping for the %browser browser language code has been deleted.', $t_args), 'The test browser language code has been deleted.');
// Check we went back to the browser negotiation mapping overview.
$this->assertUrl(\Drupal::url('language.negotiation_browser', [], ['absolute' => TRUE]));
// Check that ch-zn no longer exists.
$this->assertNoField('edit-mappings-zh-cn-browser-langcode', 'Chinese browser language code no longer exists.');
......@@ -195,7 +203,7 @@ function testUIBrowserLanguageMappings() {
'new_mapping[drupal_langcode]' => 'en',
);
$this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->assertUrl(\Drupal::url('language.negotiation_browser', [], ['absolute' => TRUE]));
$this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
$this->assertField('edit-mappings-xx-drupal-langcode', 'en', 'Drupal language code found.');
......@@ -217,7 +225,7 @@ function testUIBrowserLanguageMappings() {
'mappings[xx][drupal_langcode]' => 'zh-hans',
);
$this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->assertUrl(\Drupal::url('language.negotiation_browser', [], ['absolute' => TRUE]));
$this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
$this->assertField('edit-mappings-xx-drupal-langcode', 'zh-hans', 'Drupal language code found.');
}
......
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