From 3e9be61b4b8a88c333ab62a9a10db99466713ce2 Mon Sep 17 00:00:00 2001
From: Thijs Kinkhorst <thijs@kinkhorst.com>
Date: Mon, 10 Jan 2022 14:23:50 +0000
Subject: [PATCH] Initial version of tool to extract translatable strings from
 templates.

---
 bin/get-translatable-strings       | 29 +++++++++++++++++++++
 lib/SimpleSAML/Utils/Translate.php | 42 ++++++++++++++++++++++++++++++
 lib/SimpleSAML/XHTML/Template.php  |  9 +++++++
 3 files changed, 80 insertions(+)
 create mode 100755 bin/get-translatable-strings
 create mode 100644 lib/SimpleSAML/Utils/Translate.php

diff --git a/bin/get-translatable-strings b/bin/get-translatable-strings
new file mode 100755
index 000000000..2b0e7452e
--- /dev/null
+++ b/bin/get-translatable-strings
@@ -0,0 +1,29 @@
+#!/usr/bin/php -q
+<?php
+use SimpleSAML\Module;
+use SimpleSAML\Utils;
+
+// This is the base directory of the SimpleSAMLphp installation
+$baseDir = dirname(dirname(__FILE__));
+
+// Add library autoloader
+require_once($baseDir . '/lib/_autoload.php');
+
+$transUtils = new Utils\Translate();
+$sysUtils = new Utils\System();
+$tempDirBase = $sysUtils->getTempDir() . "/temptemplatestemp";
+$outputSuffix = '/locales/en/LC_MESSAGES';
+
+$modules = ['', 'core', 'admin', 'cron', 'exampleauth', 'multiauth', 'saml'];
+
+foreach($modules as $module) {
+    $tempDir = $tempDirBase . "/" . $module;
+    $transUtils->compileAllTemplates($module, $tempDir);
+    $domain = $module ?: 'messages';
+
+    $outputDir = $baseDir . ($module === '' ? '' : '/modules/' . $module) . $outputSuffix;
+    print `xgettext --default-domain=$domain -p $outputDir --from-code=UTF-8 -j --no-location -ktrans -L PHP $tempDir/*/*.php`;
+}
+
+// TODO: clean up workdir before/after run
+// TODO: merge new strings into translated languages catalogs
diff --git a/lib/SimpleSAML/Utils/Translate.php b/lib/SimpleSAML/Utils/Translate.php
new file mode 100644
index 000000000..7d6b72707
--- /dev/null
+++ b/lib/SimpleSAML/Utils/Translate.php
@@ -0,0 +1,42 @@
+<?php
+
+declare(strict_types=1);
+
+namespace SimpleSAML\Utils;
+
+use SimpleSAML\Configuration;
+use SimpleSAML\XHTML\Template;
+
+/**
+ * @package SimpleSAMLphp
+ */
+class Translate
+{
+    /**
+     * Compile all Twig templates for the given $module into the given $outputDir.
+     * This is used by the translation extraction tool to find the translatable
+     * strings for this module in the compiled templates.
+     * $module can be '' for the main SimpleSAMLphp templates.
+     */
+    public function compileAllTemplates(string $module, string $outputDir): void
+    {
+        $config = Configuration::loadFromArray(['template.cache' => $outputDir, 'module.enable' => [$module => true]]);
+        $baseDir = $config->getBaseDir();
+        $tplSuffix = '/templates/';
+
+        $tplDir = $baseDir . ($module === '' ? '' : 'modules/' . $module) . $tplSuffix;
+        $templateprefix = ($module === '' ? '' : $module . ":");
+
+        foreach (
+            new \RecursiveIteratorIterator(
+                new \RecursiveDirectoryIterator($tplDir),
+                \RecursiveIteratorIterator::LEAVES_ONLY
+            ) as $file
+        ) {
+            if ($file->isFile()) {
+                $p = new Template($config, $templateprefix . str_replace($tplDir, '', $file->getPathname()));
+                $p->compile();
+            }
+        }
+    }
+}
diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index bb18d8f2c..eca755310 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -491,6 +491,15 @@ class Template extends Response
         $this->data['header'] = $this->configuration->getValue('theme.header', 'SimpleSAMLphp');
     }
 
+    /**
+     * Helper function for locale extraction: just compile but not display
+     * this template. This is not generally useful, getContents() will normally
+     * compile and display the template in one step.
+     */
+    public function compile(): void
+    {
+        $this->twig->load($this->twig_template);
+    }
 
     /**
      * Get the contents produced by this template.
-- 
GitLab