files reformatted

for better readability
This commit is contained in:
Tealk
2021-03-18 18:26:03 +01:00
parent e2e4a9620b
commit bf3048baf8
40 changed files with 2079 additions and 2205 deletions
+42 -41
View File
@@ -1,10 +1,11 @@
<?php
/**
* This class is used to negotiate language displayed to user.
* Reads browser preferences and chooses the best language from list
*/
* This class is used to negotiate language displayed to user.
* Reads browser preferences and chooses the best language from list
*/
class LocaleNegotiator
{
{
private $accepted_langs = [];
private $default_language;
private $all_locales = array(
@@ -220,15 +221,15 @@ class LocaleNegotiator
'zh_SG' => '中文',
'zh_TW' => '中文',
'zu_ZA' => 'Isizulu',
);
);
/**
* This method scans for languages and creates a list of language and its name (localized ofc.)
* @param String $default_language language displayed to user in case no suitable lang is found
*/
* This method scans for languages and creates a list of language and its name (localized ofc.)
* @param String $default_language language displayed to user in case no suitable lang is found
*/
function __construct($default_language)
{
$tmp = glob(__DIR__ . '/../locale/*' , GLOB_ONLYDIR);
$tmp = glob(__DIR__ . '/../locale/*', GLOB_ONLYDIR);
$this->default_language = $default_language;
//Works only if the server supports the locale
//This basically means $accepted_langs[<lang_code>] = "<lang name>";
@@ -239,41 +240,42 @@ class LocaleNegotiator
}
/**
* Returns list of accepted langs so it can be reused for rendering language list for switching...
*/
public function get_accepted_langs(){
* Returns list of accepted langs so it can be reused for rendering language list for switching...
*/
public function get_accepted_langs()
{
return $this->accepted_langs;
}
/**
* This method does the actual negotiation. It has override parameter in case user wants to switch
* languages.
* @param String $override adds language to list of preffered languages with highest priority
* @return String language code that matched best with browser preferences
*/
public function negotiate($override = null){
* This method does the actual negotiation. It has override parameter in case user wants to switch
* languages.
* @param String $override adds language to list of preffered languages with highest priority
* @return String language code that matched best with browser preferences
*/
public function negotiate($override = null)
{
$langs = [];
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$http_accept_language = str_replace("-", "_", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $http_accept_language, $lang_parse);
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $http_accept_language, $lang_parse);
if (count($lang_parse[1])) {
$langs = array_combine($lang_parse[1], $lang_parse[4]);
foreach ($langs as $lang => $val) {
//If browser didn't send quality of language, it is 1 by default
if ($val === '') $langs[$lang] = 1;
}
if (count($lang_parse[1])) {
$langs = array_combine($lang_parse[1], $lang_parse[4]);
if (isset($override))
{
//More important than the best lang of browser
$langs[$override] = 2;
}
foreach ($langs as $lang => $val) {
//If browser didn't send quality of language, it is 1 by default
if ($val === '') $langs[$lang] = 1;
}
arsort($langs, SORT_NUMERIC);
}
if (isset($override)) {
//More important than the best lang of browser
$langs[$override] = 2;
}
arsort($langs, SORT_NUMERIC);
}
}
//So we have lang code as value
@@ -285,18 +287,18 @@ class LocaleNegotiator
global $lang;
foreach ($langs as $lang) {
if (strlen($lang)>2){
if (in_array($lang, $accepted_langs)){
if (strlen($lang) > 2) {
if (in_array($lang, $accepted_langs)) {
$best_match = $lang;
break;
}
}else{
$possible = array_filter($accepted_langs, function($key) {
} else {
$possible = array_filter($accepted_langs, function ($key) {
global $lang;
return strpos($key, $lang) === 0;
return strpos($key, $lang) === 0;
});
if (count($possible)){
if (count($possible)) {
foreach ($possible as $value) {
$best_match = $value;
}
@@ -305,11 +307,10 @@ class LocaleNegotiator
}
}
if ($best_match === false){
if ($best_match === false) {
$best_match = $this->default_language;
}
return $best_match;
}
}