From 38f03c375eafdb6b95190729479c6fa0d721b400 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Mon, 16 Sep 2024 11:31:53 +0200 Subject: More i18n --- src/application/i18n.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/application/i18n.php') diff --git a/src/application/i18n.php b/src/application/i18n.php index 964439f..2fcadab 100644 --- a/src/application/i18n.php +++ b/src/application/i18n.php @@ -6,8 +6,10 @@ $__i18n_msg_store = []; $__i18n_current_locale = null; function i18n_parse(string $contents, ?string $filename = null): array { - $syntax_error = fn(string $msg, ?int $line = null): never => - throw new Exception("i18n syntax error: $msg (in " . ($filename ?? "unknown") . ":" . ($line ?? 0) . ")"); + $syntax_error = fn(string $msg, int $line): never => + throw new Exception("i18n syntax error: $msg (in " . ($filename ?? "unknown") . ":" . $line . ")"); + $other_error = fn(string $msg, int $line): never => + throw new Exception("i18n error: $msg (in " . ($filename ?? "unknown") . ":" . $line . ")"); $msgs = []; $lines = explode("\n", $contents); @@ -27,7 +29,9 @@ function i18n_parse(string $contents, ?string $filename = null): array { if ($currentId !== "") { if ($currentContext !== "") $currentId = $currentContext . "\004" . $currentId; - $msgs[$currentId] ??= $currentMessage; + if (isset($msgs[$currentId])) + $other_error("duplicate message id '$currentId'", $lnNum); + $msgs[$currentId] = $currentMessage; } $currentId = json_decode(substr($ln, 2)); $currentContext = ""; @@ -58,7 +62,9 @@ function i18n_parse(string $contents, ?string $filename = null): array { if ($currentId !== "") { if ($currentContext !== "") $currentId = $currentContext . "\x7F" . $currentId; - $msgs[$currentId] ??= $currentMessage; + if (isset($msgs[$currentId])) + $other_error("duplicate message id '$currentId'", count($lines)); + $msgs[$currentId] = $currentMessage; } return $msgs; } -- cgit v1.2.3