public function user_lang() { if (isset($_COOKIE['language']) && $this->is_lang($_COOKIE['language'])) { return $_COOKIE['language']; } if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { return null; } $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); foreach ($languages as $locale_str) { $split = array_map('trim', explode(';', $locale_str, 2)); $lang_tag = $split[0]; $lang_tag = str_replace('-*', '', $lang_tag); if (function_exists('locale_parse')) { $locale = locale_parse($lang_tag); } else { $locale = array('language' => substr($lang_tag, 0, 2)); } if (!empty($locale['region'])) { $lang = $locale['language'] . '_' . $locale['region']; if ($this->is_lang($lang)) { return $lang; } } elseif ($this->is_lang($locale['language'])) { return $locale['language']; } } return null; }
/** * Set language to session based on the selected POSIX language string. * * @param string $posixLocale Requested language */ protected function setSessionLanguage($posixLocale) { $localeSubtags = locale_parse($posixLocale); $language = array_get($localeSubtags, 'language'); session()->set('language', $language); session()->set('applocale', $posixLocale); logger()->info("Language Switched: LANG='{$language}' POSIX='{$posixLocale}'"); }
public function normalizeLocale($locale) { $locale = str_replace('_', '-', (string) $locale); if (function_exists('locale_parse')) { $pieces = locale_parse($locale); } else { $pieces = explode('-', $locale); $pieces['language'] = $pieces[0]; $pieces['region'] = $pieces[1]; } if (isset($pieces['region'])) { return $pieces['language'] . '_' . $pieces['region']; } else { return $pieces['language']; } }