diff options
| author | winter Sparkles | 2026-08-08 23:33:27 +0100 |
|---|---|---|
| committer | winter Sparkles | 2026-08-08 23:33:27 +0100 |
| commit | 4bb659e1f7bb5850db5695a536428a9b71e3ffe5 (patch) | |
| tree | 749e41db42f9f5a08e2342e65e660ac5e59c46f7 | |
| parent | b422b6ded608807c7d3bb8b965b3797ca513610b (diff) | |
tidy up the localisation stuff
| -rw-r--r-- | Psso/XMLResponse.php | 5 | ||||
| -rw-r--r-- | locale/en.ini | 15 | ||||
| -rw-r--r-- | localisation.php | 23 | ||||
| -rw-r--r-- | templates/index.xsl | 18 | ||||
| -rw-r--r-- | webroot/index.php | 21 |
5 files changed, 48 insertions, 34 deletions
diff --git a/Psso/XMLResponse.php b/Psso/XMLResponse.php index 1b17660..61e31b4 100644 --- a/Psso/XMLResponse.php +++ b/Psso/XMLResponse.php @@ -35,10 +35,11 @@ class XMLResponse { //header('Content-Type: application/xhtml+xml'); $processor = new \XSLTProcessor; + $processor->registerPHPFunctionNS('urn:psso:xsl', 'L', 'L'); foreach (self::$stylesheets as $sheet) { $processor->importStylesheet($sheet); } - $root = \Dom\import_simplexml($this->doc); - echo $processor->transformToXml($root); + //$root = \Dom\import_simplexml($this->doc); + echo $processor->transformToXml($this->doc); } } diff --git a/locale/en.ini b/locale/en.ini new file mode 100644 index 0000000..7809906 --- /dev/null +++ b/locale/en.ini @@ -0,0 +1,15 @@ +error = "Error" +error.not-found = "Page not found" +error.not-found.long = "The page you requested (%s) does not exist." +error.wrong-method = "Invalid method" +error.wrong-method.long = "Your request method (%s) is not valid for this path." +error.uncaught = "Uncaught exception" +ui.powered-by = "Powered by Pleasant SSO" +ui.source-code = "Source code" +login.title = "Log in" +challenge.input.username = "Username" +challenge.input.password = "Password" +challenge.input.otp = "One-time passcode" +challenge.message.wrong-password = "Incorrect username or password" +challenge.message.wrong-otp = "Invalid OTP code" +challenge.continue = "Continue"
\ No newline at end of file diff --git a/localisation.php b/localisation.php index 0e193b0..a1c7bac 100644 --- a/localisation.php +++ b/localisation.php @@ -1,14 +1,15 @@ <?php -function L(string $key) { - // me when im lazy - return [ - 'login.title' => 'Log in', - 'challenge.input.username' => 'Username', - 'challenge.input.password' => 'Password', - 'challenge.input.otp' => 'One-time passcode', - 'challenge.message.wrong-password' => 'Incorrect username or password', - 'challenge.message.wrong-otp' => 'Invalid OTP code', - 'challenge.continue' => 'Continue', - ][$key] ?? $key; +// TODO: actually determine this from request header (/ user info?) +const LANGUAGE = 'en'; + +$langData = parse_ini_file(__DIR__ . '/locale/' . LANGUAGE . '.ini'); + +function L(string $key, mixed ...$values) { + global $langData; + if (!isset($langData[$key])) { + error_log("warn: translation key $key does not exist"); + return $key; + } + return sprintf($langData[$key], ...$values); } diff --git a/templates/index.xsl b/templates/index.xsl index 65e84f6..35300ec 100644 --- a/templates/index.xsl +++ b/templates/index.xsl @@ -1,5 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> -<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> +<xsl:stylesheet version="1.0" + xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:psso="urn:psso:xsl"> <xsl:output method="xml" encoding="UTF-8" doctype-system="about:legacy-compat"/> @@ -46,10 +48,14 @@ <xsl:template name="page-footer"> <footer> - <a><xsl:attribute name="href"> - <xsl:value-of select="/page/@source"/> - </xsl:attribute> - Source code</a> + <xsl:value-of select="psso:L('ui.powered-by')"/> + ยท + <a> + <xsl:attribute name="href"> + <xsl:value-of select="/page/@source"/> + </xsl:attribute> + <xsl:value-of select="psso:L('ui.source-code')"/> + </a> </footer> </xsl:template> @@ -59,7 +65,7 @@ <xsl:template match="error"> <error-message> - <box-label>Error</box-label> + <box-label><xsl:value-of select="psso:L('error')"/></box-label> <xsl:value-of select="."/> </error-message> </xsl:template> diff --git a/webroot/index.php b/webroot/index.php index 729cdf2..dd8a6d9 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -30,32 +30,23 @@ $router = new Psso\Router('../routes', $config); $router->registerStatusHandler(404, function ($path) { $resp = new Psso\XMLResponse; - $resp->doc->addAttribute('title', 'Not found'); - $resp->doc->addChild( - 'error', - "The requested page ($path) does not exist." - ); + $resp->doc->addAttribute('title', L('error.not-found')); + $resp->doc->addChild('error', L('error.not-found.long', $path)); $resp->send(); }); $router->registerStatusHandler(405, function ($path) { $method = $_SERVER['REQUEST_METHOD']; $resp = new Psso\XMLResponse; - $resp->doc->addAttribute('title', 'Wrong method'); - $resp->doc->addChild( - 'error', - "Your request method ($method) is not valid for this path." - ); + $resp->doc->addAttribute('title', L('error.wrong-method')); + $resp->doc->addChild('error', L('error.wrong-method.long', $method)); $resp->send(); }); $router->setExceptionHandler(function (Throwable $e) { $resp = new Psso\XMLResponse; - $resp->doc->addAttribute('title', 'Uncaught exception'); - $resp->doc->addChild( - 'error', - $e::class . ': ' . $e->getMessage() - ); + $resp->doc->addAttribute('title', L('error.uncaught')); + $resp->doc->addChild('error', $e::class . ': ' . $e->getMessage()); $resp->send(); error_log($e); }); |
