Add lang_getfrombrowser and human_filesize
These functions are used in almost all md projects at one point or another.
This commit is contained in:
parent
a09eba6b84
commit
08c9582210
105
MD_STD.php
105
MD_STD.php
|
@ -196,4 +196,109 @@ class MD_STD {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function lang_getfrombrowser gets the browser language based on HTTP headers.
|
||||
*
|
||||
* @param array<string> $allowed_languages Array containing all the languages for which
|
||||
* there are translations.
|
||||
* @param string $default_language Default language of the instance of MD.
|
||||
* @param string $lang_variable Currently set language variable. Optional.
|
||||
* @param boolean $strict_mode Whether to demand "de-de" (true) or "de" (false) Optional.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function lang_getfrombrowser(array $allowed_languages, string $default_language, string $lang_variable = "", bool $strict_mode = true):string {
|
||||
|
||||
// $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde
|
||||
if ($lang_variable === "") {
|
||||
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
|
||||
}
|
||||
|
||||
// wurde irgendwelche Information mitgeschickt?
|
||||
if (empty($lang_variable)) {
|
||||
// Nein? => Standardsprache zurückgeben
|
||||
return $default_language;
|
||||
}
|
||||
|
||||
// Den Header auftrennen
|
||||
$accepted_languages = preg_split('/,\s*/', $lang_variable);
|
||||
if (!is_array($accepted_languages)) return $default_language;
|
||||
|
||||
// Die Standardwerte einstellen
|
||||
$current_lang = $default_language;
|
||||
$current_q = 0;
|
||||
|
||||
// Nun alle mitgegebenen Sprachen abarbeiten
|
||||
foreach ($accepted_languages as $accepted_language) {
|
||||
|
||||
// Alle Infos über diese Sprache rausholen
|
||||
// phpcs:disable Generic.Strings.UnnecessaryStringConcat
|
||||
$res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches);
|
||||
// phpcs:enable
|
||||
|
||||
// war die Syntax gültig?
|
||||
if (!$res) {
|
||||
// Nein? Dann ignorieren
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sprachcode holen und dann sofort in die Einzelteile trennen
|
||||
$lang_code = explode('-', $matches[1]);
|
||||
|
||||
// Wurde eine Qualität mitgegeben?
|
||||
if (isset($matches[2])) {
|
||||
// die Qualität benutzen
|
||||
$lang_quality = (float)$matches[2];
|
||||
} else {
|
||||
// Kompabilitätsmodus: Qualität 1 annehmen
|
||||
$lang_quality = 1.0;
|
||||
}
|
||||
|
||||
// Bis der Sprachcode leer ist...
|
||||
// phpcs:disable Squiz.PHP.DisallowSizeFunctionsInLoops
|
||||
while (!empty($lang_code)) {
|
||||
// phpcs:enable
|
||||
// mal sehen, ob der Sprachcode angeboten wird
|
||||
if (in_array(strtolower(join('-', $lang_code)), $allowed_languages)) {
|
||||
// Qualität anschauen
|
||||
if ($lang_quality > $current_q) {
|
||||
// diese Sprache verwenden
|
||||
$current_lang = strtolower(join('-', $lang_code));
|
||||
$current_q = $lang_quality;
|
||||
// Hier die innere while-Schleife verlassen
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Wenn wir im strengen Modus sind, die Sprache nicht versuchen zu minimalisieren
|
||||
if ($strict_mode) {
|
||||
// innere While-Schleife aufbrechen
|
||||
break;
|
||||
}
|
||||
// den rechtesten Teil des Sprachcodes abschneiden
|
||||
array_pop($lang_code);
|
||||
}
|
||||
}
|
||||
|
||||
// die gefundene Sprache zurückgeben
|
||||
return $current_lang;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function human_filesize translates byte-level filesizes to human readable ones.
|
||||
* Thanks to Jeffrey Sambells http://jeffreysambells.com/2012/10/25/human-readable-filesize-php
|
||||
*
|
||||
* @param string $bytes A file size, e.g. returned from filesize().
|
||||
* @param integer $decimals Number of decimal digits to allow.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function human_filesize(string $bytes, int $decimals = 2):string {
|
||||
|
||||
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . $size[$factor];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user