ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilHttpRequestsLanguageDetector.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
28 {
29  protected string $header_value;
30 
31  public function __construct(string $header_value)
32  {
33  $this->header_value = $header_value;
34  }
35 
40  public function getIso2LanguageCode(): string
41  {
42  if (strlen($this->header_value)) {
43  $matches = array();
44  // Format: de,de-DE;q=0.8,en-US;q=0.6,en;q=0.4
45  preg_match_all("/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i", $this->header_value, $matches);
46  if (count($matches[1])) {
47  $langs = array_combine($matches[1], $matches[4]);
48  foreach ($langs as $lang => $val) {
49  if ($val === '') {
50  $langs[$lang] = 1;
51  }
52  }
53  arsort($langs, SORT_NUMERIC);
54 
55  $keys = array_keys($langs);
56  if (isset($keys[0])) {
57  return substr($keys[0], 0, 2);
58  }
59  }
60  }
61 
62  throw new ilLanguageException("Could not extract any language information from request.");
63  }
64 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getIso2LanguageCode()
Returns the detected ISO2 language code.