aboutsummaryrefslogtreecommitdiffhomepage
path: root/misc/get_ui_language.php
diff options
context:
space:
mode:
authorwinter2024-12-24 17:46:51 +0000
committerwinter2024-12-24 17:46:51 +0000
commit721674973ca0a4d98cd081e9ff130c3356ddac03 (patch)
tree5caa082baa4f23bd8a88f4f268fea6d9233f6553 /misc/get_ui_language.php
parentd5a57276eb27e215dd0b1bd5eb67ac26acc9575b (diff)
implement ui translations using gettext
this was horrible i don't like gettext anymore
Diffstat (limited to 'misc/get_ui_language.php')
-rw-r--r--misc/get_ui_language.php29
1 files changed, 29 insertions, 0 deletions
diff --git a/misc/get_ui_language.php b/misc/get_ui_language.php
new file mode 100644
index 0000000..b1305b3
--- /dev/null
+++ b/misc/get_ui_language.php
@@ -0,0 +1,29 @@
+<?php
+
+function get_ui_language(?array $possibleLanguages = null): string {
+ if ($possibleLanguages == null) {
+ $possibleLanguages = array_map(function ($path) {
+ $parts = explode('/', $path);
+ return $parts[count($parts) - 2];
+ }, glob(__DIR__ . '/../locale/*/LC_MESSAGES'));
+ }
+
+ if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+ return $possibleLanguages[0];
+ }
+
+ $accepts = array_map(
+ fn($spec) => str_replace('-', '_', explode(';', $spec)[0]),
+ explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])
+ );
+
+ foreach ($accepts as $alang) {
+ foreach ($possibleLanguages as $candidate) {
+ if (str_starts_with(strtolower($candidate), strtolower($alang))) {
+ return $candidate;
+ }
+ }
+ }
+
+ return $possibleLanguages[0];
+} \ No newline at end of file