From e8c7f1c59de7fc6a68e741c1c7b750cc9cd6b1f1 Mon Sep 17 00:00:00 2001
From: Hanne Moa <hanne.moa@uninett.no>
Date: Wed, 2 Mar 2016 12:19:32 +0100
Subject: [PATCH] Hook up new localization with twig.i18n

---
 lib/SimpleSAML/Locale/Language.php | 25 +++++++++++++++++++++++++
 lib/SimpleSAML/XHTML/Template.php  | 16 +++++++++++++++-
 templates/sandbox.twig             |  2 ++
 3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/lib/SimpleSAML/Locale/Language.php b/lib/SimpleSAML/Locale/Language.php
index 412fc2ac9..b0ea40120 100644
--- a/lib/SimpleSAML/Locale/Language.php
+++ b/lib/SimpleSAML/Locale/Language.php
@@ -115,6 +115,17 @@ class Language
         'eu'    => 'Euskara', // Basque
     );
 
+    /**
+     * A mapping of SSP languages to locales
+     *
+     * @var array
+     */
+    private $languagePosixMapping = array(
+        'no' => 'nb_NO',
+        'en' => 'en_US',
+        'nn' => 'nn_NO',
+    );
+
 
     /**
      * Constructor
@@ -138,6 +149,20 @@ class Language
     }
 
 
+    /*
+     * Rename to non-idiosyncratic language code
+     *
+     * @param string $language Language code for the language to rename, if neccesary.
+     */
+    public function getPosixLanguage($language)
+    {
+        if (isset($this->languagePosixMapping[$language])) {
+            return $this->languagePosixMapping[$language];
+        }
+        return $language;
+    }
+
+
     /**
      * This method will set a cookie for the user's browser to remember what language was selected.
      *
diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index b2fa78042..cc76a3e94 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -24,6 +24,13 @@ class SimpleSAML_XHTML_Template
      */
     private $translator;
 
+    /**
+     * The localization backend
+     *
+     * @var \SimpleSAML\Locale\Localization
+     */
+    private $localization;
+
     /**
      * The configuration to use in this template.
      *
@@ -65,6 +72,8 @@ class SimpleSAML_XHTML_Template
         // TODO: do not remove the slash from the beginning, change the templates instead!
         $this->data['baseurlpath'] = ltrim($this->configuration->getBasePath(), '/');
         $this->translator = new SimpleSAML\Locale\Translate($configuration, $defaultDictionary);
+        $this->localization = new \SimpleSAML\Locale\Localization($configuration);
+        $this->useTwig =  $this->setupTwig();
         $this->twig = $this->setupTwig();
     }
 
@@ -146,7 +155,12 @@ class SimpleSAML_XHTML_Template
             return null;
         }
 
-        return new \Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => $auto_reload));
+        $twig = new \Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => $auto_reload));
+        if ($this->localization->i18nBackend == 'twig.i18n') {
+            $this->localization->activateDomain('ssp');
+            $twig->addExtension(new \Twig_Extensions_Extension_I18n());
+        }
+        return $twig;
     }
 
     /*
diff --git a/templates/sandbox.twig b/templates/sandbox.twig
index 0d42e5e16..97f0b64fd 100644
--- a/templates/sandbox.twig
+++ b/templates/sandbox.twig
@@ -2,5 +2,7 @@
 {% block content %}
     <p>This page exists as a sandbox to play with twig without affecting anything else. The template is in ./templates.</p>
     <p>{{ sometext }}</p>
+    <p>Original: Hello, Untranslated World!</p>
+    <p>Translated: {% trans 'Hello, Untranslated World!' %}</p>
     <p>Current locale set: {{ currentLanguage }}</p>
 {% endblock content %}
-- 
GitLab