-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated DetectLanguage code to be used for service-desk, white-pages…
… and self-service-password.
- Loading branch information
1 parent
6a55a57
commit 2469ffb
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php namespace Ltb; | ||
/** | ||
* detect the preferred language of the user agent | ||
* | ||
* @copyright Roy Kaldung <roy@kaldung.com> | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class LanguageTest extends \Mockery\Adapter\Phpunit\MockeryTestCase | ||
{ | ||
function test_accept_all_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"); | ||
$defaultLanguage = "en"; | ||
|
||
# Execute function | ||
$chosenLanguage = \Ltb\Language::detect_language($defaultLanguage, $availableLanguages); | ||
|
||
$this->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); | ||
} | ||
} |