本文整理汇总了PHP中Illuminate\Http\Request::getPreferredLanguage方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::getPreferredLanguage方法的具体用法?PHP Request::getPreferredLanguage怎么用?PHP Request::getPreferredLanguage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Http\Request
的用法示例。
在下文中一共展示了Request::getPreferredLanguage方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Detect the bot case => if bot we do nothing
$isBot = isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT']);
/**
* If User has never been logged => it will be redirected to his local url
*/
if (!$request->cookie('locale') && !$isBot) {
$locale = \App::getLocale();
// if local doesn't match the current user local => we redirect
$preferedLocale = $request->getPreferredLanguage($this->locales);
// If local doesn't match the current user => we redirect the user to the correct url
if ($locale !== $preferedLocale) {
// Get Locales of the preferred local
Session::put('locale', $preferedLocale);
}
} elseif ($request->cookie('locale')) {
Session::put('locale', $request->cookie('locale'));
}
if ($request->has('force_locale')) {
Session::put('locale', $request->get('force_locale'));
\App::setLocale(Session::get('locale', env('APP_DEFAULT_LOCALE', 'en')));
return $next($request)->withCookie(cookie()->forever('locale', Session::get('locale')));
}
\App::setLocale(Session::get('locale', env('APP_DEFAULT_LOCALE', 'en')));
return $next($request);
}
示例2: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $l
* @return mixed
*/
public function handle($request, Closure $next)
{
//Session::flush();
if (!Session::has('locale')) {
/**
* Get the browser local code and lang code.
*/
$localCode = $request->getPreferredLanguage();
$localLang = substr($localCode, 0, 2);
if (in_array($localLang, $this->lang)) {
Session::set('locale', $localLang);
} else {
Session::set('locale', Config::get('app.locale'));
}
}
/**
* Set the local config.
*/
App::setLocale(Session::get('locale'));
Config::set('app.locale', Session::get('locale'));
/**
* Share variables in view.
*/
if (Config::get('app.locale') == 'fr') {
View::share(['lang' => 'fr', 'langreverse' => 'en']);
} else {
View::share(['lang' => 'en', 'langreverse' => 'fr']);
}
return $next($request);
}
示例3: tryHeaders
/**
* Try to get a prefered Locale from Accept-Headers
*
* @return bool
*/
protected function tryHeaders()
{
$language_list = $this->job->languages->lists('slug');
if ($slug = $this->request->getPreferredLanguage($language_list)) {
return $this->isValidLanguage($slug);
}
return false;
}
示例4: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!Session::has('locale')) {
Session::put('locale', $request->getPreferredLanguage($this->languages));
}
app()->setLocale(Session::get('locale'));
return $next($request);
}
示例5: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->languages = array_keys(Config::get('app.locales'));
if (!Session::has('locale')) {
Session::put('locale', $request->getPreferredLanguage($this->languages));
}
$this->app->setLocale(Session::get('locale'));
return $next($request);
}
示例6: handle
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!Session::has('locale')) {
Session::put('locale', $request->getPreferredLanguage($this->languages));
}
app()->setLocale(Session::get('locale'));
if (!Session::has('statut')) {
Session::put('statut', Auth::check() ? Auth::user()->role->slug : 'visitor');
}
return $next($request);
}
示例7: getCurrentLocale
/**
* Returns current language
*
* @return string current language
*/
public function getCurrentLocale()
{
if ($this->currentLocale) {
return $this->currentLocale;
}
// Get preferred locale from browser
if ($this->getConfig('useAcceptLanguageHeader', true)) {
return $this->request->getPreferredLanguage($this->getSupportedLanguagesKeys());
}
// or get application default language
return $this->defaultLocale;
}
示例8: getPreferredLanguage
/**
* Returns the preferred language.
*
* @param array $locales An array of ordered available locales
* @return string|null The preferred locale
* @static
*/
public static function getPreferredLanguage($locales = null)
{
//Method inherited from \Symfony\Component\HttpFoundation\Request
return \Illuminate\Http\Request::getPreferredLanguage($locales);
}
示例9: getRequestLocale
/**
* @param \Illuminate\Http\Request $request
* @return string
*/
protected function getRequestLocale($request)
{
$locales = array_unique(array_merge([$this->repository->getDefault()], $this->repository->getAvailable()));
return $request->getPreferredLanguage($locales);
}