From b528b295e65e9fe53738d1ed031e0479489f46bc Mon Sep 17 00:00:00 2001
From: Hanne Moa <hanne.moa@uninett.no>
Date: Tue, 15 Nov 2016 10:28:13 +0100
Subject: [PATCH] Wash config 'language.available'

---
 lib/SimpleSAML/Locale/Language.php | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/lib/SimpleSAML/Locale/Language.php b/lib/SimpleSAML/Locale/Language.php
index 0a9e53d47..a4f32d67c 100644
--- a/lib/SimpleSAML/Locale/Language.php
+++ b/lib/SimpleSAML/Locale/Language.php
@@ -138,7 +138,7 @@ class Language
     public function __construct(\SimpleSAML_Configuration $configuration)
     {
         $this->configuration = $configuration;
-        $this->availableLanguages = $this->configuration->getArray('language.available', array('en'));
+        $this->availableLanguages = $this->getInstalledLanguages();
         $this->defaultLanguage = $this->configuration->getString('language.default', 'en');
         $this->languageParameterName = $this->configuration->getString('language.parameter.name', 'language');
         $this->customFunction = $this->configuration->getArray('language.get_language_function', null);
@@ -152,6 +152,26 @@ class Language
     }
 
 
+    /**
+     * Wash configured (available) languages against installed languages
+     *
+     * @return array The set of langauges both in 'language.available' and $this->language_names
+     */
+    private function getInstalledLanguages()
+    {
+        $configuredAvailableLanguages = $this->configuration->getArray('language.available', array('en'));
+        $availableLanguages = array();
+        foreach ($configuredAvailableLanguages as $code) {
+            if (array_key_exists($code, $this->language_names) && isset($this->language_names[$code])) {
+                $availableLanguages[] = $code;
+            } else {
+                \SimpleSAML\Logger::error("Language \"$code\" not installed. Check config.");
+            }
+        }
+        return $availableLanguages;
+    }
+
+
     /*
      * Rename to non-idiosyncratic language code
      *
@@ -234,7 +254,11 @@ class Language
      */
     public function getLanguageLocalizedName($code)
     {
-        return $this->language_names[$code];
+        if (array_key_exists($code, $this->language_names) && isset($this->language_names[$code])) {
+            return $this->language_names[$code];
+        }
+        \SimpleSAML\Logger::error("Name for language \"$code\" not found. Check config.");
+        return null;
     }
 
 
-- 
GitLab