pupupu

simple static CMS for crappy servers
git clone https://git.ce9e.org/pupupu.git

commit
3b8dbeae7537cca49250112d4798553f0253a3b5
parent
5aeb97fcfd5da704ea68973c0174430169a9ebb2
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-01-24 18:58
provide own language negotiation

Diffstat

M utils.php 39 +++++++++++++++++++++++++++++++--------

1 files changed, 31 insertions, 8 deletions


diff --git a/utils.php b/utils.php

@@ -6,16 +6,39 @@ use Symfony\Component\Yaml\Exception\ParseException;
    6     6 
    7     7 class WriteException extends Exception {}
    8     8 
    9    -1 function get_translation()
   -1     9 // https://gist.github.com/ch-gilbert/a376704763629691a828
   -1    10 // https://www.rfc-editor.org/rfc/rfc9110#name-accept-language
   -1    11 function negotiate_language($available_languages)
   10    12 {
   11    -1     if (!include('HTTP2.php')) {
   12    -1         return array();
   13    -1     };
   -1    13     $bestlang = $available_languages[0];
   -1    14     $bestqval = 0;
   -1    15 
   -1    16     if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
   -1    17         return $bestlang;
   -1    18     }
   -1    19 
   -1    20     $regexp = "/(([a-z]+)(-[a-z-]+)*)(\s*;\s*q=([01]\.[0-9]+))?\s*[,$]/i";
   -1    21     preg_match_all($regexp, $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches, PREG_SET_ORDER);
   -1    22 
   -1    23     foreach ($matches as $match) {
   -1    24         $language = strtolower($match[0]);
   -1    25         $langprefix = strtolower($match[1]);
   -1    26         $qvalue = !empty($match[4]) ? floatval($match[4]) : 1.0;
   14    27 
   15    -1     $http = new HTTP2();
   16    -1     $LANG = $http->negotiateLanguage(array(
   17    -1         'de' => true,
   18    -1     ));
   -1    28         if (in_array($language, $available_languages) && ($qvalue > $bestqval)) {
   -1    29             $bestlang = $language;
   -1    30             $bestqval = $qvalue;
   -1    31         } else if (in_array($langprefix, $available_languages) && ($qvalue * 0.9 > $bestqval)) {
   -1    32             $bestlang = $langprefix;
   -1    33             $bestqval = $qvalue * 0.9;
   -1    34         }
   -1    35     }
   -1    36     return $bestlang;
   -1    37 }
   -1    38 
   -1    39 function get_translation()
   -1    40 {
   -1    41     $LANG = negotiate_language(['en', 'de']);
   19    42 
   20    43     try {
   21    44         return Yaml::parseFile("trans/$LANG.yml");