From 2469ffbdcd07a0a55cb952d748994a5ffea07782 Mon Sep 17 00:00:00 2001 From: Abhishek Pai Date: Wed, 17 Jul 2024 08:56:48 +0200 Subject: [PATCH] Migrated DetectLanguage code to be used for service-desk, white-pages and self-service-password. --- src/Ltb/Language.php | 36 ++++++++++++++++++++++++++++ tests/Ltb/LanguageTest.php | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/Ltb/Language.php create mode 100644 tests/Ltb/LanguageTest.php diff --git a/src/Ltb/Language.php b/src/Ltb/Language.php new file mode 100644 index 0000000..ed70417 --- /dev/null +++ b/src/Ltb/Language.php @@ -0,0 +1,36 @@ + + * @license http://www.php.net/license/3_01.txt PHP license + */ + +final class Language { + + /** + * split request header Accept-Language to determine the UserAgent's + * prefered language + * + * @param string $defaultLanguage preselected default language + * @return string returns the default language or a match from $availableLanguages + */ + static function detect_language($defaultLanguage, $availableLanguages): string + { + $acceptedLanguages = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ? htmlspecialchars($_SERVER['HTTP_ACCEPT_LANGUAGE']) : ""; + $languageList = explode(',', $acceptedLanguages); + $choosenLanguage= $defaultLanguage; + foreach($languageList as $currentLanguage) { + $currentLanguage = explode(';', $currentLanguage); + if (preg_match('/(..)-?.*/', $currentLanguage[0], $reg)) { + foreach($reg as $checkLang) { + if ($match = preg_grep('/'.$checkLang.'/i', $availableLanguages)) { + $choosenLanguage= $match[key($match)]; + break 2; + } + } + } + } + return $choosenLanguage; + } +} \ No newline at end of file diff --git a/tests/Ltb/LanguageTest.php b/tests/Ltb/LanguageTest.php new file mode 100644 index 0000000..0ebf60b --- /dev/null +++ b/tests/Ltb/LanguageTest.php @@ -0,0 +1,48 @@ +assertEquals("en", $chosenLanguage); + } + + function test_restrict_language() { + # User-Agent Language + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en"; + + $availableLanguages = array("ar","cn","de","el","es","fr","it","nb-NO","pl","pt-PT","ru","sl","tr","zh-CN","ca","cs","ee","en","eu","hu","ja","nl","pt-BR","rs","sk","sv","uk","zh-TW"); + $allowedLanguages = array("fr"); + $defaultLanguage = "fr"; + + # Execute function + $chosenLanguage = \Ltb\Language::detect_language($defaultLanguage, $allowedLanguages ? array_intersect($availableLanguages, $allowedLanguages) : $availableLanguages); + + $this->assertEquals("fr", $chosenLanguage); + } + + function test_default_language() { + # User-Agent Language + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "ar, en"; + + $availableLanguages = array("ar","cn","de","el","es","fr","it","nb-NO","pl","pt-PT","ru","sl","tr","zh-CN","ca","cs","ee","en","eu","hu","ja","nl","pt-BR","rs","sk","sv","uk","zh-TW"); + $allowedLanguages = array("fr"); + $defaultLanguage = "en"; + + # Execute function + $chosenLanguage = \Ltb\Language::detect_language($defaultLanguage, $allowedLanguages ? array_intersect($availableLanguages, $allowedLanguages) : $availableLanguages); + + $this->assertEquals("en", $chosenLanguage); + } +}