From 4bb659e1f7bb5850db5695a536428a9b71e3ffe5 Mon Sep 17 00:00:00 2001 From: winter Sparkles Date: Sat, 8 Aug 2026 23:33:27 +0100 Subject: tidy up the localisation stuff --- Psso/XMLResponse.php | 5 +++-- locale/en.ini | 15 +++++++++++++++ localisation.php | 23 ++++++++++++----------- templates/index.xsl | 18 ++++++++++++------ webroot/index.php | 21 ++++++--------------- 5 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 locale/en.ini 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 @@ '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 @@ - + @@ -46,10 +48,14 @@ @@ -59,7 +65,7 @@ - Error + 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); }); -- cgit v1.3