/** * Extract user browser language * @return string|null Language code to use */ public function getBrowserLanguage() { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && strlen($_SERVER['HTTP_ACCEPT_LANGUAGE']) >= 2) { $locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']); $aux = $this->container->bdd->select('SELECT abbr FROM languages', '', '', false); if ($aux) { foreach ($aux as $lang) { $languages[] = $lang['abbr']; } } else { $table_info = $this->container->bdd->free_query('SHOW TABLES LIKE \'languages\''); if ($table_info === false) { $aux = $this->container->bdd->free_query('CREATE TABLE `languages` (`languages_id` int(11) NOT NULL AUTO_INCREMENT,`language` varchar(255) COLLATE utf8_spanish2_ci DEFAULT NULL,`abbr` varchar(6) COLLATE utf8_spanish2_ci DEFAULT NULL,PRIMARY KEY (`languages_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci;'); if ($aux === false) { $this->container->__warn("No ha sido posible crear la base de datos para la información de los idiomas (languages), el usuario de base de datos puede no tener permisos de creación de tablas."); } else { $this->container->bdd->insert("languages", array('language', 'abbr'), array($locale, $locale)); } } $languages = array($locale); } return locale_lookup($languages, $locale, false, DEFAULT_LANG); } else { return NULL; } }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($request->has('lang') && Languages::has($request->input('lang'))) { // 1. Check URL parameter $locale = $request->input('lang'); App::setLocale($locale); // Logged-in users: save to database if (Auth::check()) { $user = Auth::user(); $user->locale = $locale; $user->save(); } Cookie::queue('locale', $locale, 24 * 60); } elseif (Auth::check() && Languages::has(Auth::user()->locale)) { // 2. Check database for logged in users App::setLocale(Auth::user()->locale); } elseif (Languages::has($request->cookie('locale'))) { // 3. Check cookies App::setLocale($request->cookie('locale')); } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // 4. Check browser languages. Note that Googlebot do not have this. $accept_languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); foreach ($accept_languages as $lang) { $lang = locale_accept_from_http($lang); $locale = locale_lookup(Languages::all(), $lang, true, 'en'); if (!empty($locale)) { App::setLocale($locale); break; } } } setlocale(LC_TIME, Languages::withRegion(App::getLocale()) . '.utf8'); return $next($request); }
public function getLocale() { // Search and return the best locale to use foreach ($this['client_locales'] as $locale) { if (function_exists('locale_lookup')) { $lc = locale_lookup(array_keys($this['translations']), $locale); } else { foreach (array_keys($this['translations']) as $available_locale) { if (strpos($locale, $available_locale) === 0) { $lc = $available_locale; } } } if ($lc) { return $lc; } } // Return the default locale return $this['client_locales'][count($this['client_locales']) - 1]; }
<?php $arr = array('de-DEVA', 'de-DE-1996', 'de', 'de-De'); echo locale_lookup($arr, 'de-DE-1996-x-prv1-prv2');