diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/get_ui_language.php | 29 |
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 |
