From 39f520474de9b10952b4dcff85e09f6a7b34ffe7 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Mon, 29 Feb 2016 16:33:01 +0100
Subject: [PATCH] Move the list of language localized names to the
 SimpleSAML\Locale\Language class, and make the array of languages generated
 by SimpleSAML_XHTML_Template::generateLanguageBar() a bit more useful.

---
 lib/SimpleSAML/Locale/Language.php | 59 ++++++++++++++++++++++++++++++
 lib/SimpleSAML/XHTML/Template.php  | 59 +++++++-----------------------
 2 files changed, 72 insertions(+), 46 deletions(-)

diff --git a/lib/SimpleSAML/Locale/Language.php b/lib/SimpleSAML/Locale/Language.php
index f843da056..412fc2ac9 100644
--- a/lib/SimpleSAML/Locale/Language.php
+++ b/lib/SimpleSAML/Locale/Language.php
@@ -69,6 +69,52 @@ class Language
      */
     private $customFunction;
 
+    /**
+     * A list of languages supported with their names localized, indexed by ISO 639-2 code.
+     *
+     * @var array
+     */
+    private $language_names = array(
+        'no'    => 'BokmĂĄl', // Norwegian BokmĂĄl
+        'nn'    => 'Nynorsk', // Norwegian Nynorsk
+        'se'    => 'Sámegiella', // Northern Sami
+        'sam'   => 'Ă…arjelh-saemien giele', // Southern Sami
+        'da'    => 'Dansk', // Danish
+        'en'    => 'English',
+        'de'    => 'Deutsch', // German
+        'sv'    => 'Svenska', // Swedish
+        'fi'    => 'Suomeksi', // Finnish
+        'es'    => 'Español', // Spanish
+        'fr'    => 'Français', // French
+        'it'    => 'Italiano', // Italian
+        'nl'    => 'Nederlands', // Dutch
+        'lb'    => 'LĂ«tzebuergesch', // Luxembourgish
+        'cs'    => 'Čeština', // Czech
+        'sl'    => 'Slovenščina', // Slovensk
+        'lt'    => 'LietuviĹł kalba', // Lithuanian
+        'hr'    => 'Hrvatski', // Croatian
+        'hu'    => 'Magyar', // Hungarian
+        'pl'    => 'Język polski', // Polish
+        'pt'    => 'PortuguĂŞs', // Portuguese
+        'pt-br' => 'PortuguĂŞs brasileiro', // Portuguese
+        'ru'    => 'русский язык', // Russian
+        'et'    => 'eesti keel', // Estonian
+        'tr'    => 'Türkçe', // Turkish
+        'el'    => 'ελληνικά', // Greek
+        'ja'    => '日本語', // Japanese
+        'zh'    => '简体中文', // Chinese (simplified)
+        'zh-tw' => '繁體中文', // Chinese (traditional)
+        'ar'    => 'العربية', // Arabic
+        'fa'    => 'پارسی', // Persian
+        'ur'    => 'اردو', // Urdu
+        'he'    => 'עִבְרִית', // Hebrew
+        'id'    => 'Bahasa Indonesia', // Indonesian
+        'sr'    => 'Srpski', // Serbian
+        'lv'    => 'Latviešu', // Latvian
+        'ro'    => 'Românește', // Romanian
+        'eu'    => 'Euskara', // Basque
+    );
+
 
     /**
      * Constructor
@@ -151,6 +197,19 @@ class Language
     }
 
 
+    /**
+     * Get the localized name of a language, by ISO 639-2 code.
+     *
+     * @param string $code The ISO 639-2 code of the language.
+     *
+     * @return string The localized name of the language.
+     */
+    public function getLanguageLocalizedName($code)
+    {
+        return $this->language_names[$code];
+    }
+
+
     /**
      * Get the language parameter name.
      *
diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 6289aa6fe..730fadff1 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -157,65 +157,32 @@ class SimpleSAML_XHTML_Template
 
 
     /**
-     * Generate languagebar
+     * Generate an array for its use in the language bar, indexed by the ISO 639-2 codes of the languages available,
+     * containing their localized names and the URL that should be used in order to change to that language.
+     *
+     * @return array The array containing information of all available languages.
      */
     private function generateLanguageBar()
     {
         $languages = $this->translator->getLanguage()->getLanguageList();
         $langmap = null;
         if (count($languages) > 1) {
-            // TODO: this array should not be defined here
-            $langnames = array(
-                'no'    => 'BokmĂĄl', // Norwegian BokmĂĄl
-                'nn'    => 'Nynorsk', // Norwegian Nynorsk
-                'se'    => 'Sámegiella', // Northern Sami
-                'sam'   => 'Ă…arjelh-saemien giele', // Southern Sami
-                'da'    => 'Dansk', // Danish
-                'en'    => 'English',
-                'de'    => 'Deutsch', // German
-                'sv'    => 'Svenska', // Swedish
-                'fi'    => 'Suomeksi', // Finnish
-                'es'    => 'Español', // Spanish
-                'fr'    => 'Français', // French
-                'it'    => 'Italiano', // Italian
-                'nl'    => 'Nederlands', // Dutch
-                'lb'    => 'LĂ«tzebuergesch', // Luxembourgish
-                'cs'    => 'Čeština', // Czech
-                'sl'    => 'Slovenščina', // Slovensk
-                'lt'    => 'LietuviĹł kalba', // Lithuanian
-                'hr'    => 'Hrvatski', // Croatian
-                'hu'    => 'Magyar', // Hungarian
-                'pl'    => 'Język polski', // Polish
-                'pt'    => 'PortuguĂŞs', // Portuguese
-                'pt-br' => 'PortuguĂŞs brasileiro', // Portuguese
-                'ru'    => 'русский язык', // Russian
-                'et'    => 'eesti keel', // Estonian
-                'tr'    => 'Türkçe', // Turkish
-                'el'    => 'ελληνικά', // Greek
-                'ja'    => '日本語', // Japanese
-                'zh'    => '简体中文', // Chinese (simplified)
-                'zh-tw' => '繁體中文', // Chinese (traditional)
-                'ar'    => 'العربية', // Arabic
-                'fa'    => 'پارسی', // Persian
-                'ur'    => 'اردو', // Urdu
-                'he'    => 'עִבְרִית', // Hebrew
-                'id'    => 'Bahasa Indonesia', // Indonesian
-                'sr'    => 'Srpski', // Serbian
-                'lv'    => 'Latviešu', // Latvian
-                'ro'    => 'Românește', // Romanian
-                'eu'    => 'Euskara', // Basque
-            );
             $parameterName = $this->getTranslator()->getLanguage()->getLanguageParameterName();
             $langmap = array();
             foreach ($languages as $lang => $current) {
                 $lang = strtolower($lang);
-                $langname = $langnames[$lang];
+                $langname = $this->translator->getLanguage()->getLanguageLocalizedName($lang);
                 $url = false;
                 if (!$current) {
-                    $url = htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters('',
-                        array($parameterName => $lang)));
+                    $url = htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters(
+                        '',
+                        array($parameterName => $lang)
+                    ));
                 }
-                $langmap[$langname] = $url;
+                $langmap[$lang] = array(
+                    'name' => $langname,
+                    'url' => $url,
+                );
             }
         }
         return $langmap;
-- 
GitLab