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.
257 lines
8.2 KiB
PHP
257 lines
8.2 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';
|
|
|
|
/** @param list<string|array<string,mixed>>|array<string,mixed> $available */
|
|
public static function resolve(
|
|
array $query,
|
|
array $cookies,
|
|
string $acceptLanguage,
|
|
array $available = []
|
|
): string {
|
|
$supported = self::supportedLocales($available);
|
|
$requested = self::requestedLocale($query, $supported);
|
|
if ($requested !== null) {
|
|
return $requested;
|
|
}
|
|
$cookieLocale = self::matchLocale(
|
|
$cookies[self::COOKIE_NAME] ?? null,
|
|
$supported
|
|
);
|
|
if ($cookieLocale !== null) {
|
|
return $cookieLocale;
|
|
}
|
|
return self::browserLocale($acceptLanguage, $supported);
|
|
}
|
|
|
|
/** @param list<string|array<string,mixed>>|array<string,mixed> $available */
|
|
public static function requestedLocale(
|
|
array $query,
|
|
array $available = []
|
|
): ?string {
|
|
return self::matchLocale(
|
|
$query['lang'] ?? null,
|
|
self::supportedLocales($available)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Backward-compatible public normalizer. With no directory it resolves
|
|
* only the original Chinese and English locales.
|
|
*
|
|
* @param list<string|array<string,mixed>>|array<string,mixed> $available
|
|
*/
|
|
public static function normalizeLocale(
|
|
$value,
|
|
array $available = []
|
|
): ?string {
|
|
return self::matchLocale(
|
|
$value,
|
|
self::supportedLocales($available)
|
|
);
|
|
}
|
|
|
|
public static function canonicalLocale($value): ?string
|
|
{
|
|
return LocaleCode::canonicalize($value);
|
|
}
|
|
|
|
/** @param list<string|array<string,mixed>>|array<string,mixed> $available */
|
|
public static function browserLocale(
|
|
string $acceptLanguage,
|
|
array $available = []
|
|
): string {
|
|
$supported = self::supportedLocales($available);
|
|
$acceptLanguage = trim($acceptLanguage);
|
|
if ($acceptLanguage === '') {
|
|
return self::defaultLocale($supported, true);
|
|
}
|
|
|
|
$preferences = [];
|
|
foreach (explode(',', $acceptLanguage) as $position => $entry) {
|
|
$parts = array_map('trim', explode(';', $entry));
|
|
$requested = array_shift($parts);
|
|
$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;
|
|
}
|
|
$preferences[] = [
|
|
'requested' => $requested,
|
|
'quality' => $quality,
|
|
'position' => $position,
|
|
];
|
|
}
|
|
usort($preferences, static function (array $left, array $right): int {
|
|
if ($left['quality'] === $right['quality']) {
|
|
return $left['position'] <=> $right['position'];
|
|
}
|
|
return $left['quality'] < $right['quality'] ? 1 : -1;
|
|
});
|
|
foreach ($preferences as $preference) {
|
|
$matched = self::matchLocale(
|
|
$preference['requested'],
|
|
$supported
|
|
);
|
|
if ($matched !== null) {
|
|
return $matched;
|
|
}
|
|
}
|
|
return self::defaultLocale($supported, false);
|
|
}
|
|
|
|
public static function templateLanguage(string $locale): string
|
|
{
|
|
$locale = self::canonicalLocale($locale);
|
|
return $locale !== null && explode('-', $locale)[0] === 'en'
|
|
? 'true' : 'false';
|
|
}
|
|
|
|
public static function switchUrl(string $locale, string $requestUri): string
|
|
{
|
|
$locale = self::canonicalLocale($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
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param list<string|array<string,mixed>>|array<string,mixed> $available
|
|
* @return array<string,mixed>
|
|
*/
|
|
public static function viewData(
|
|
string $locale,
|
|
string $requestUri,
|
|
array $available = []
|
|
): array {
|
|
$supported = self::supportedLocales($available);
|
|
$locale = self::matchLocale($locale, $supported)
|
|
?? self::defaultLocale($supported, true);
|
|
return [
|
|
'public_locale' => $locale,
|
|
'language' => self::templateLanguage($locale),
|
|
'language_zh_url' => self::switchUrl(
|
|
self::CHINESE,
|
|
$requestUri
|
|
),
|
|
'language_en_url' => self::switchUrl(
|
|
self::ENGLISH,
|
|
$requestUri
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<string|array<string,mixed>>|array<string,mixed> $available
|
|
* @return list<string>
|
|
*/
|
|
private static function supportedLocales(array $available): array
|
|
{
|
|
if ($available === []) {
|
|
return [self::CHINESE, self::ENGLISH];
|
|
}
|
|
$supported = [];
|
|
foreach ($available as $key => $value) {
|
|
$candidate = null;
|
|
if (is_string($value)) {
|
|
$candidate = is_string($key) ? $key : $value;
|
|
} elseif (is_array($value)) {
|
|
$candidate = $value['locale'] ?? (
|
|
is_string($key) ? $key : null
|
|
);
|
|
} elseif (is_string($key)) {
|
|
$candidate = $key;
|
|
}
|
|
$canonical = self::canonicalLocale($candidate);
|
|
if ($canonical !== null && !isset($supported[$canonical])) {
|
|
$supported[$canonical] = true;
|
|
}
|
|
}
|
|
return $supported === []
|
|
? [self::CHINESE, self::ENGLISH]
|
|
: array_keys($supported);
|
|
}
|
|
|
|
/** @param list<string> $supported */
|
|
private static function matchLocale($value, array $supported): ?string
|
|
{
|
|
$requested = self::canonicalLocale($value);
|
|
if ($requested === null) {
|
|
return null;
|
|
}
|
|
$byLowercase = [];
|
|
foreach ($supported as $locale) {
|
|
$byLowercase[strtolower($locale)] = $locale;
|
|
}
|
|
$candidate = $requested;
|
|
while ($candidate !== '') {
|
|
$key = strtolower($candidate);
|
|
if (isset($byLowercase[$key])) {
|
|
return $byLowercase[$key];
|
|
}
|
|
$position = strrpos($candidate, '-');
|
|
if ($position === false) {
|
|
break;
|
|
}
|
|
$candidate = substr($candidate, 0, $position);
|
|
}
|
|
$language = strtolower(explode('-', $requested)[0]);
|
|
foreach ($supported as $locale) {
|
|
if (strtolower(explode('-', $locale)[0]) === $language) {
|
|
return $locale;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** @param list<string> $supported */
|
|
private static function defaultLocale(
|
|
array $supported,
|
|
bool $preferSource
|
|
): string {
|
|
if ($preferSource && in_array(self::CHINESE, $supported, true)) {
|
|
return self::CHINESE;
|
|
}
|
|
if (in_array(self::ENGLISH, $supported, true)) {
|
|
return self::ENGLISH;
|
|
}
|
|
if (in_array(self::CHINESE, $supported, true)) {
|
|
return self::CHINESE;
|
|
}
|
|
return $supported[0] ?? self::CHINESE;
|
|
}
|
|
}
|