$supported */ $supported = array_values(array_unique(config('lottery.locales.supported', ['en', 'zh', 'ne']))); $fallback = (string) config('lottery.locales.fallback', 'en'); $header = strtolower(trim((string) $request->header('X-Locale', ''))); if ($header !== '' && in_array($header, $supported, true)) { return $header; } return self::fromAcceptLanguage((string) $request->header('Accept-Language', ''), $supported, $fallback); } /** @param list $supported */ private static function fromAcceptLanguage(string $header, array $supported, string $fallback): string { if ($header === '') { return $fallback; } foreach (explode(',', $header) as $part) { $code = strtolower(trim(explode(';', trim($part), 2)[0] ?? '')); if ($code === '') { continue; } $primary = explode('-', $code, 2)[0] ?? $code; if ($primary === 'zh' && in_array('zh', $supported, true)) { return 'zh'; } if ($primary === 'ne' && in_array('ne', $supported, true)) { return 'ne'; } if ($primary === 'en' && in_array('en', $supported, true)) { return 'en'; } } return $fallback; } }