From 4238c9b998edd4ba4d8b64d05503ec2d0d73eb3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaime=20Pe=CC=81rez=20Crespo?= <jaime.perez@uninett.no>
Date: Mon, 3 Dec 2018 09:53:32 +0100
Subject: [PATCH] bugfix: New translations are trying to be loaded when
 "usenewui" is disabled.

This resolves #1009.
---
 lib/SimpleSAML/XHTML/Template.php | 36 +++++++++++++------------------
 www/admin/sandbox.php             | 10 ++++-----
 2 files changed, 19 insertions(+), 27 deletions(-)

diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 9178f251c..13be9e395 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -56,7 +56,7 @@ class Template extends Response
      *
      * @var false|Twig_Environment
      */
-    private $twig;
+    private $twig = false;
 
     /**
      * The template name.
@@ -130,15 +130,17 @@ class Template extends Response
         // check if we are supposed to use the new UI
         $this->useNewUI = $this->configuration->getBoolean('usenewui', false);
 
-        // check if we need to attach a theme controller
-        $controller = $this->configuration->getString('theme.controller', false);
-        if ($controller && class_exists($controller) &&
-            in_array('\SimpleSAML\XHTML\TemplateControllerInterface', class_implements($controller))
-        ) {
-            $this->controller = new $controller();
-        }
+        if ($this->useNewUI) {
+            // check if we need to attach a theme controller
+            $controller = $this->configuration->getString('theme.controller', false);
+            if ($controller && class_exists($controller) &&
+                in_array('\SimpleSAML\XHTML\TemplateControllerInterface', class_implements($controller))
+            ) {
+                $this->controller = new $controller();
+            }
 
-        $this->twig = $this->setupTwig();
+            $this->twig = $this->setupTwig();
+        }
         parent::__construct();
     }
 
@@ -268,29 +270,21 @@ class Template extends Response
             $this->localization->addModuleDomain($this->theme['module']);
         }
 
+        // set up translation
         $options = [
             'cache' => $cache,
             'auto_reload' => $auto_reload,
-            'translation_function' => ['\SimpleSAML\Locale\Translate', 'translateSingularNativeGettext'],
-            'translation_function_plural' => ['\SimpleSAML\Locale\Translate', 'translatePluralNativeGettext'],
+            'translation_function' => ['\SimpleSAML\Locale\Translate', 'translateSingularGettext'],
+            'translation_function_plural' => ['\SimpleSAML\Locale\Translate', 'translatePluralGettext'],
         ];
 
-        // set up translation
-        if ($this->localization->i18nBackend === \SimpleSAML\Locale\Localization::GETTEXT_I18N_BACKEND) {
-            $options['translation_function'] = ['\SimpleSAML\Locale\Translate', 'translateSingularGettext'];
-            $options['translation_function_plural'] = [
-                '\SimpleSAML\Locale\Translate',
-                'translatePluralGettext'
-            ];
-        } // TODO: add a branch for the old SimpleSAMLphp backend
-
         $twig = new Twig_Environment($loader, $options);
         $twig->addExtension(new Twig_Extensions_Extension_I18n());
 
         // initialize some basic context
         $langParam = $this->configuration->getString('language.parameter.name', 'language');
         $twig->addGlobal('languageParameterName', $langParam);
-        $twig->addGlobal('localeBackend', $this->useNewUI ? Localization::GETTEXT_I18N_BACKEND : Localization::SSP_I18N_BACKEND);
+        $twig->addGlobal('localeBackend', Localization::GETTEXT_I18N_BACKEND);
         $twig->addGlobal('currentLanguage', $this->translator->getLanguage()->getLanguage());
         $twig->addGlobal('isRTL', false); // language RTL configuration
         if ($this->translator->getLanguage()->isLanguageRTL()) {
diff --git a/www/admin/sandbox.php b/www/admin/sandbox.php
index d7cf29365..e2318bd18 100644
--- a/www/admin/sandbox.php
+++ b/www/admin/sandbox.php
@@ -3,17 +3,15 @@
 require_once('../_include.php');
 
 // Load SimpleSAMLphp configuration
-$config = \SimpleSAML\Configuration::getInstance();
+$config = \SimpleSAML\Configuration::getInstance()->toArray();
+$config['usenewui'] = true;
+$config = \SimpleSAML\Configuration::loadFromArray($config, '[ARRAY]', 'simplesaml');
 $session = \SimpleSAML\Session::getSessionFromRequest();
 
-// Check if valid local session exists..
-//SimpleSAML\Utils\Auth::requireAdmin();
-
 $template = new \SimpleSAML\XHTML\Template($config, 'sandbox.php');
-
 $template->data['pagetitle'] = 'Sandbox';
 $template->data['sometext'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur.';
 $template->data['remaining'] = $session->getAuthData('admin', 'Expire') - time();
 $template->data['logout'] = null;
 
-$template->show();
+$template->send();
-- 
GitLab