| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\I18n; |
| 4 |
|
| 5 |
use ipl\Stdlib\Str; |
| 6 |
use stdClass; |
| 7 |
|
| 8 |
/** |
| 9 |
* Negotiate and parse locale codes |
| 10 |
*/ |
| 11 |
class Locale |
| 12 |
{ |
| 13 |
/** @var string Default locale code */ |
| 14 |
protected string $defaultLocale = 'en_US'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Get the default locale |
| 18 |
* |
| 19 |
* @return string |
| 20 |
*/ |
| 21 |
public function getDefaultLocale(): string |
| 22 |
{ |
| 23 |
return $this->defaultLocale; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Set the default locale |
| 28 |
* |
| 29 |
* @param string $defaultLocale |
| 30 |
* |
| 31 |
* @return $this |
| 32 |
*/ |
| 33 |
public function setDefaultLocale(string $defaultLocale): static |
| 34 |
{ |
| 35 |
$this->defaultLocale = $defaultLocale; |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Return the preferred locale based on the given HTTP header and the available translations |
| 42 |
* |
| 43 |
* @param string $header The HTTP "Accept-Language" header |
| 44 |
* @param array<string> $available Available translations |
| 45 |
* |
| 46 |
* @return string The browser's preferred locale code |
| 47 |
*/ |
| 48 |
public function getPreferred(string $header, array $available): string |
| 49 |
{ |
| 50 |
$headerValues = Str::trimSplit($header, ','); |
| 51 |
for ($i = 0; $i < count($headerValues); $i++) { |
| 52 |
// Include the original index to ensure a stable sort. |
| 53 |
$headerValues[$i] = [$headerValues[$i], $i]; |
| 54 |
} |
| 55 |
usort( // Sort DESC, keeping equal elements ASC. |
| 56 |
$headerValues, |
| 57 |
function ($a, $b) { |
| 58 |
$tagA = Str::trimSplit($a[0], ';', 2); |
| 59 |
$tagB = Str::trimSplit($b[0], ';', 2); |
| 60 |
$qValA = (float) (strpos($a[0], ';') > 0 ? substr(array_pop($tagA), 2) : 1); |
| 61 |
$qValB = (float) (strpos($b[0], ';') > 0 ? substr(array_pop($tagB), 2) : 1); |
| 62 |
|
| 63 |
return $qValA < $qValB ? 1 : ($qValA > $qValB ? -1 : ($a[1] > $b[1] ? 1 : ($a[1] < $b[1] ? -1 : 0))); |
| 64 |
} |
| 65 |
); |
| 66 |
for ($i = 0; $i < count($headerValues); $i++) { |
| 67 |
// Restore the original array structure after sorting. |
| 68 |
$headerValues[$i] = $headerValues[$i][0]; |
| 69 |
} |
| 70 |
$requestedLocales = []; |
| 71 |
foreach ($headerValues as $headerValue) { |
| 72 |
if (strpos($headerValue, ';') > 0) { |
| 73 |
$parts = Str::trimSplit($headerValue, ';', 2); |
| 74 |
$headerValue = $parts[0]; |
| 75 |
} |
| 76 |
$requestedLocales[] = str_replace('-', '_', $headerValue); |
| 77 |
} |
| 78 |
$requestedLocales = array_combine( |
| 79 |
array_map('strtolower', array_values($requestedLocales)), |
| 80 |
array_values($requestedLocales) |
| 81 |
); |
| 82 |
|
| 83 |
$available[] = $this->defaultLocale; |
| 84 |
$availableLocales = array_combine( |
| 85 |
array_map('strtolower', array_values($available)), |
| 86 |
array_values($available) |
| 87 |
); |
| 88 |
|
| 89 |
$similarMatch = null; |
| 90 |
|
| 91 |
foreach ($requestedLocales as $requestedLocaleLowered => $requestedLocale) { |
| 92 |
$localeObj = $this->parseLocale($requestedLocaleLowered); |
| 93 |
|
| 94 |
if ( |
| 95 |
isset($availableLocales[$requestedLocaleLowered]) |
| 96 |
&& (! $similarMatch || $this->parseLocale($similarMatch)->language === $localeObj->language) |
| 97 |
) { |
| 98 |
return $availableLocales[$requestedLocaleLowered]; |
| 99 |
} |
| 100 |
|
| 101 |
if (! $similarMatch) { |
| 102 |
foreach ($availableLocales as $availableLocaleLowered => $availableLocale) { |
| 103 |
if ($this->parseLocale($availableLocaleLowered)->language === $localeObj->language) { |
| 104 |
$similarMatch = $availableLocaleLowered; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $similarMatch ? $availableLocales[$similarMatch] : $this->defaultLocale; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Parse a locale into its subtags |
| 116 |
* |
| 117 |
* Convert the output of {@see \Locale::parseLocale()} to an object and return it. |
| 118 |
* |
| 119 |
* @param string $locale |
| 120 |
* |
| 121 |
* @return stdClass Output of {@see \Locale::parseLocale()} converted to an object |
| 122 |
*/ |
| 123 |
public function parseLocale(string $locale): stdClass |
| 124 |
{ |
| 125 |
return (object) \Locale::parseLocale($locale); |
| 126 |
} |
| 127 |
} |