From 2095c89ff3e4dd4cbebe5e22b70d7ab61215814f Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 6 Nov 2014 09:41:24 +0000
Subject: [PATCH] =?UTF-8?q?Issue=20#365615=20followup=20by=20attiks,=20Yes?=
 =?UTF-8?q?CT,=20G=C3=A1bor=20Hojtsy,=20mgifford,=20Albert=20Volkman,=20pl?=
 =?UTF-8?q?ach,=20webwarrior,=20David=5FRothstein,=20penyaskito,=20smokris?=
 =?UTF-8?q?=20|=20yang=5Fyi=5Fcn:=20Fixed=20Followups:=20Language=20detect?=
 =?UTF-8?q?ion=20not=20working=20correctly=20for=20most=20Chinese=20reader?=
 =?UTF-8?q?s=20(and=20add=20a=20user=20interface=20for=20all=20browser=20l?=
 =?UTF-8?q?anguage=20mappings).?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 core/modules/language/language.admin.inc           |  8 +++-----
 core/modules/language/language.module              |  2 +-
 .../src/Form/NegotiationBrowserDeleteForm.php      |  8 ++++++++
 .../language/src/Form/NegotiationBrowserForm.php   | 14 +++++++-------
 .../src/Tests/LanguageBrowserDetectionUnitTest.php | 12 ++++++++++--
 5 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/core/modules/language/language.admin.inc b/core/modules/language/language.admin.inc
index d7a041514c23..ebae05c684ce 100644
--- a/core/modules/language/language.admin.inc
+++ b/core/modules/language/language.admin.inc
@@ -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);
 
diff --git a/core/modules/language/language.module b/core/modules/language/language.module
index 68126f6ce137..6901c91c579c 100644
--- a/core/modules/language/language.module
+++ b/core/modules/language/language.module
@@ -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':
diff --git a/core/modules/language/src/Form/NegotiationBrowserDeleteForm.php b/core/modules/language/src/Form/NegotiationBrowserDeleteForm.php
index 0c0a4474f181..aa8fbeade009 100644
--- a/core/modules/language/src/Form/NegotiationBrowserDeleteForm.php
+++ b/core/modules/language/src/Form/NegotiationBrowserDeleteForm.php
@@ -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');
diff --git a/core/modules/language/src/Form/NegotiationBrowserForm.php b/core/modules/language/src/Form/NegotiationBrowserForm.php
index aea7ad19d9f0..8cbac111e8e7 100644
--- a/core/modules/language/src/Form/NegotiationBrowserForm.php
+++ b/core/modules/language/src/Form/NegotiationBrowserForm.php
@@ -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);
   }
diff --git a/core/modules/language/src/Tests/LanguageBrowserDetectionUnitTest.php b/core/modules/language/src/Tests/LanguageBrowserDetectionUnitTest.php
index 62ee8e30b9c2..f724338edd33 100644
--- a/core/modules/language/src/Tests/LanguageBrowserDetectionUnitTest.php
+++ b/core/modules/language/src/Tests/LanguageBrowserDetectionUnitTest.php
@@ -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.');
   }
-- 
GitLab