From ad554b48e7dc355d9b33eede98f4e3e829fd867e Mon Sep 17 00:00:00 2001 From: winter Date: Tue, 31 Dec 2024 14:30:16 +0000 Subject: switch to my own translations system i really don't like gettext --- Digitigrade/Text.php | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Digitigrade/Text.php (limited to 'Digitigrade') diff --git a/Digitigrade/Text.php b/Digitigrade/Text.php new file mode 100644 index 0000000..952818b --- /dev/null +++ b/Digitigrade/Text.php @@ -0,0 +1,49 @@ +translationKey = $translationKey; + } + + public function __tostring(): string { + return $this->getInLanguage(self::$defaultLanguage); + } + + public function getInLanguage(string $language): string { + $dict = self::getDict($language); + if ($dict == null) { + Logger::getInstance()->warning("tried to use language '$language', which does not exist"); + return $this->translationKey; + } + if (isset($dict[$this->translationKey])) { + return $dict[$this->translationKey]; + } + if (isset($dict['FALLBACK'])) { + return $this->getInLanguage($dict['FALLBACK']); + } + Logger::getInstance()->warning("looking up translation key '$this->translationKey' in language '$language' failed"); + return $this->translationKey; + } + + private static function getDict(string $language): ?array { + if (isset(self::$dictionary[$language])) { + return self::$dictionary[$language]; + } + $filename = __DIR__ . "/../locale/$language.json"; + if (!is_file($filename)) { + return null; + } + $dict = json_decode(file_get_contents($filename), associative: true); + self::$dictionary[$language] = $dict; + return $dict; + } + + public static function setDefaultLanguage(string $language) { + self::$defaultLanguage = $language; + } +} \ No newline at end of file -- cgit v1.3