fix(get_browser_language): return defaultLocale if browser doesn't send user preferred language

This commit is contained in:
Yassine Doghri 2022-07-18 16:07:44 +00:00
parent b5263107da
commit 9cc2996261

View File

@ -11,12 +11,17 @@ declare(strict_types=1);
if (! function_exists('get_browser_language')) { if (! function_exists('get_browser_language')) {
/** /**
* Gets the browser default language using the request header key `HTTP_ACCEPT_LANGUAGE` * Gets the browser default language using the request header key `HTTP_ACCEPT_LANGUAGE`. Returns Castopod's default
* locale if `HTTP_ACCEPT_LANGUAGE` is null.
* *
* @return string ISO 639-1 language code * @return string ISO 639-1 language code
*/ */
function get_browser_language(string $httpAcceptLanguage): string function get_browser_language(?string $httpAcceptLanguage = null): string
{ {
if ($httpAcceptLanguage === null) {
return config('App')->defaultLocale;
}
$langs = explode(',', $httpAcceptLanguage); $langs = explode(',', $httpAcceptLanguage);
return substr($langs[0], 0, 2); return substr($langs[0], 0, 2);