You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.1 KiB
PHP
133 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
final class PublicLocale
|
|
{
|
|
public const COOKIE_NAME = 'venix_public_locale';
|
|
public const COOKIE_TTL = 31536000;
|
|
public const ENGLISH = 'en';
|
|
public const CHINESE = 'zh-CN';
|
|
|
|
public static function resolve(array $query, array $cookies, string $acceptLanguage): string
|
|
{
|
|
$requested = self::requestedLocale($query);
|
|
if ($requested !== null) {
|
|
return $requested;
|
|
}
|
|
|
|
$cookieLocale = self::normalizeLocale($cookies[self::COOKIE_NAME] ?? null);
|
|
if ($cookieLocale !== null) {
|
|
return $cookieLocale;
|
|
}
|
|
|
|
return self::browserLocale($acceptLanguage);
|
|
}
|
|
|
|
public static function requestedLocale(array $query): ?string
|
|
{
|
|
return self::normalizeLocale($query['lang'] ?? null);
|
|
}
|
|
|
|
public static function normalizeLocale($value): ?string
|
|
{
|
|
if (!is_string($value)) {
|
|
return null;
|
|
}
|
|
|
|
$locale = strtolower(str_replace('_', '-', trim($value)));
|
|
if ($locale === 'zh' || str_starts_with($locale, 'zh-')) {
|
|
return self::CHINESE;
|
|
}
|
|
if ($locale === 'en' || str_starts_with($locale, 'en-')) {
|
|
return self::ENGLISH;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function browserLocale(string $acceptLanguage): string
|
|
{
|
|
$acceptLanguage = trim($acceptLanguage);
|
|
if ($acceptLanguage === '') {
|
|
return self::CHINESE;
|
|
}
|
|
|
|
$supported = [];
|
|
foreach (explode(',', $acceptLanguage) as $position => $entry) {
|
|
$parts = array_map('trim', explode(';', $entry));
|
|
$locale = self::normalizeLocale(array_shift($parts));
|
|
if ($locale === null) {
|
|
continue;
|
|
}
|
|
|
|
$quality = 1.0;
|
|
foreach ($parts as $parameter) {
|
|
if (stripos($parameter, 'q=') !== 0) {
|
|
continue;
|
|
}
|
|
$qualityValue = substr($parameter, 2);
|
|
$quality = is_numeric($qualityValue) ? (float) $qualityValue : 0.0;
|
|
break;
|
|
}
|
|
if ($quality <= 0.0 || $quality > 1.0) {
|
|
continue;
|
|
}
|
|
|
|
$supported[] = [
|
|
'locale' => $locale,
|
|
'quality' => $quality,
|
|
'position' => $position,
|
|
];
|
|
}
|
|
|
|
usort($supported, static function (array $left, array $right): int {
|
|
if ($left['quality'] === $right['quality']) {
|
|
return $left['position'] <=> $right['position'];
|
|
}
|
|
return $left['quality'] < $right['quality'] ? 1 : -1;
|
|
});
|
|
|
|
return $supported[0]['locale'] ?? self::ENGLISH;
|
|
}
|
|
|
|
public static function templateLanguage(string $locale): string
|
|
{
|
|
return self::normalizeLocale($locale) === self::ENGLISH ? 'true' : 'false';
|
|
}
|
|
|
|
public static function switchUrl(string $locale, string $requestUri): string
|
|
{
|
|
$locale = self::normalizeLocale($locale) ?? self::CHINESE;
|
|
$parts = parse_url($requestUri);
|
|
if (!is_array($parts) || isset($parts['scheme']) || isset($parts['host'])) {
|
|
$parts = ['path' => '/'];
|
|
}
|
|
$path = is_array($parts) && isset($parts['path']) ? (string) $parts['path'] : '/';
|
|
if ($path === '' || $path[0] !== '/' || str_starts_with($path, '//')
|
|
|| preg_match('/[\x00-\x1F\x7F]/', $path)) {
|
|
$path = '/';
|
|
}
|
|
|
|
$query = [];
|
|
if (is_array($parts) && isset($parts['query'])) {
|
|
parse_str((string) $parts['query'], $query);
|
|
}
|
|
$query['lang'] = $locale;
|
|
|
|
return $path . '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
|
}
|
|
|
|
public static function viewData(string $locale, string $requestUri): array
|
|
{
|
|
$locale = self::normalizeLocale($locale) ?? self::CHINESE;
|
|
|
|
return [
|
|
'public_locale' => $locale,
|
|
'language' => self::templateLanguage($locale),
|
|
'language_zh_url' => self::switchUrl(self::CHINESE, $requestUri),
|
|
'language_en_url' => self::switchUrl(self::ENGLISH, $requestUri),
|
|
];
|
|
}
|
|
}
|