Skip to content

Commit

Permalink
Fix #305 (#599)
Browse files Browse the repository at this point in the history
* Cleanup code in LaravelLocalizationRedirectFilter

* Fix problem when hideDefaultLocale and AcceptLangaugeHeader are both enabled

* Fixed problem with AcceptHeader & hideDEfault
  • Loading branch information
iwasherefirst2 authored and Marc Cámara committed Oct 16, 2018
1 parent c5671cf commit 6d0865d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ In order to activate it, you just have to attach this middleware to the routes y

If you want to hide the default locale but always show other locales in the url, switch the `hideDefaultLocaleInURL` config value to true. Once it's true, if the default locale is en (english) all URLs containing /en/ would be redirected to the same url without this fragment '/' but maintaining the locale as en (English).

**IMPORTANT** - When `hideDefaultLocaleInURL` is set to true, the unlocalized root is treated as the applications default locale `app.locale`. Because of this language negotiation using the Accept-Language header will **NEVER** occur when `hideDefaultLocaleInURL` is true.
When `hideDefaultLocaleInURL` and `useAcceptLanguageHeader` are both set to true,then the language negotiation using the Accept-Language header will only occur while the session('locale') is empty. After negotiation, the session('locale') will be set accordingly and will not be called again.

### Set current locale as view-base-path

Expand Down
18 changes: 18 additions & 0 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ public function setLocale($locale = null)
return $locale;
}

/**
* Check if $locale is default locale and supposed to be hidden in url
*
* @param string $locale Locale to be checked
*
* @return boolean Returns true if above requirement are met, otherwise false
*/

public function isHiddenDefault($locale)
{
return ($this->getDefaultLocale() === $locale && $this->hideDefaultLocaleInURL());
}

/**
* Set and return supported locales.
*
Expand Down Expand Up @@ -716,6 +729,11 @@ protected function useAcceptLanguageHeader()
return $this->configRepository->get('laravellocalization.useAcceptLanguageHeader');
}

public function hideUrlAndAcceptHeader()
{
return $this->hideDefaultLocaleInURL() && $this->useAcceptLanguageHeader();
}

/**
* Returns the translation key for a given path.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function handle($request, Closure $next)
array_shift($params);

if (\count($params) > 0) {
$localeCode = $params[0];
$locale = $params[0];

if (app('laravellocalization')->checkLocaleInSupportedLocales($localeCode)) {
if ($localeCode === app('laravellocalization')->getDefaultLocale() && app('laravellocalization')->hideDefaultLocaleInURL()) {
if (app('laravellocalization')->checkLocaleInSupportedLocales($locale)) {
if (app('laravellocalization')->isHiddenDefault($locale)) {
$redirection = app('laravellocalization')->getNonLocalizedURL();

// Save any flashed data for redirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Closure;
use Illuminate\Http\RedirectResponse;

class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
{

/**
Expand All @@ -27,7 +27,7 @@ public function handle($request, Closure $next) {
return $next($request)->withCookie(cookie()->forever('locale', $params[0]));
}

if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->getDefaultLocale() === $locale && app('laravellocalization')->hideDefaultLocaleInURL())) {
if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
$redirection = app('laravellocalization')->getLocalizedURL($locale);
$redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Closure;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\LanguageNegotiator;
use Mcamara\LaravelLocalization\LaravelLocalization;

class LocaleSessionRedirect extends LaravelLocalizationMiddlewareBase
{
Expand All @@ -30,8 +32,21 @@ public function handle($request, Closure $next)

return $next($request);
}
elseif(empty($locale) && app('laravellocalization')->hideUrlAndAcceptHeader()){
// When default locale is hidden and accept language header is true,
// then compute browser language when no session has been set.
// Once the session has been set, there is no need
// to negotiate language from browser again.
$negotiator = new LanguageNegotiator(app('laravellocalization')->getDefaultLocale(), app('laravellocalization')->getSupportedLocales(), $request);
$locale = $negotiator->negotiateLanguage();
session(['locale' => $locale]);
}

if($locale === false){
$locale = app('laravellocalization')->getCurrentLocale();
}

if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->getDefaultLocale() === $locale && app('laravellocalization')->hideDefaultLocaleInURL())) {
if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
app('session')->reflash();
$redirection = app('laravellocalization')->getLocalizedURL($locale);

Expand Down

0 comments on commit 6d0865d

Please sign in to comment.