Skip to content

Commit

Permalink
Migrated DetectLanguage code to be used for service-desk, white-pages…
Browse files Browse the repository at this point in the history
… and self-service-password.
  • Loading branch information
abpai94 authored and davidcoutadeur committed Sep 18, 2024
1 parent 6a55a57 commit 2469ffb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Ltb/Language.php
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 <[email protected]>
* @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;
}
}
48 changes: 48 additions & 0 deletions tests/Ltb/LanguageTest.php
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);
}
}

0 comments on commit 2469ffb

Please sign in to comment.