1
|
|
|
<?php |
2
|
|
|
namespace Translation\Http\Middleware; |
3
|
|
|
|
4
|
|
|
use Closure; |
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
|
|
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
|
|
|
7
|
|
|
use Illuminate\View\Factory as ViewFactory; |
|
|
|
|
8
|
|
|
use Translation\Repositories\LanguageRepository; |
9
|
|
|
use Translation\UriLocalizer; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class TranslationMiddleware |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Constructor |
15
|
|
|
* |
16
|
|
|
* @param Translation\UriLocalizer $uriLocalizer |
|
|
|
|
17
|
|
|
* @param Translation\Repositories\LanguageRepository $languageRepository |
|
|
|
|
18
|
|
|
* @param Illuminate\Config\Repository $config Laravel config |
|
|
|
|
19
|
|
|
* @param Illuminate\View\Factory $viewFactory |
|
|
|
|
20
|
|
|
* @param Illuminate\Foundation\Application $app |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
public function __construct(UriLocalizer $uriLocalizer, LanguageRepository $languageRepository, Config $config, ViewFactory $viewFactory, Application $app) |
23
|
|
|
{ |
24
|
|
|
$this->uriLocalizer = $uriLocalizer; |
|
|
|
|
25
|
|
|
$this->languageRepository = $languageRepository; |
|
|
|
|
26
|
|
|
$this->config = $config; |
|
|
|
|
27
|
|
|
$this->viewFactory = $viewFactory; |
|
|
|
|
28
|
|
|
$this->app = $app; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Handle an incoming request. |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
35
|
|
|
* @param \Closure $next |
36
|
|
|
* @param integer $segment Index of the segment containing locale info |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function handle($request, Closure $next, $segment = 0) |
40
|
|
|
{ |
41
|
|
|
// Ignores all non GET requests: |
42
|
|
|
if ($request->method() !== 'GET') { |
43
|
|
|
return $next($request); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$currentUrl = $request->getUri(); |
47
|
|
|
$uriLocale = $this->uriLocalizer->getLocaleFromUrl($currentUrl, $segment); |
48
|
|
|
$defaultLocale = $this->config->get('app.locale'); |
49
|
|
|
|
50
|
|
|
// If a locale was set in the url: |
51
|
|
|
if ($uriLocale) { |
52
|
|
|
$currentLanguage = $this->languageRepository->findByLocale($uriLocale); |
53
|
|
|
$selectableLanguages = $this->languageRepository->allExcept($uriLocale); |
54
|
|
|
$altLocalizedUrls = []; |
55
|
|
|
foreach ($selectableLanguages as $lang) { |
56
|
|
|
$altLocalizedUrls[] = [ |
57
|
|
|
'locale' => $lang->locale, |
58
|
|
|
'name' => $lang->name, |
59
|
|
|
'url' => $this->uriLocalizer->localize($currentUrl, $lang->locale, $segment), |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Set app locale |
64
|
|
|
$this->app->setLocale($uriLocale); |
65
|
|
|
|
66
|
|
|
// Share language variable with views: |
67
|
|
|
$this->viewFactory->share('currentLanguage', $currentLanguage); |
68
|
|
|
$this->viewFactory->share('selectableLanguages', $selectableLanguages); |
69
|
|
|
$this->viewFactory->share('altLocalizedUrls', $altLocalizedUrls); |
70
|
|
|
|
71
|
|
|
// Set locale in session: |
72
|
|
|
if ($request->hasSession() && $request->session()->get('waavi.translation.locale') !== $uriLocale) { |
73
|
|
|
$request->session()->put('waavi.translation.locale', $uriLocale); |
74
|
|
|
} |
75
|
|
|
return $next($request); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// If no locale was set in the url, check the session locale |
79
|
|
|
if ($request->hasSession() && $sessionLocale = $request->session()->get('waavi.translation.locale')) { |
80
|
|
|
if ($this->languageRepository->isValidLocale($sessionLocale)) { |
81
|
|
|
return redirect()->to($this->uriLocalizer->localize($currentUrl, $sessionLocale, $segment)); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// If no locale was set in the url, check the browser's locale: |
86
|
|
|
$browserLocale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2); |
87
|
|
|
if ($this->languageRepository->isValidLocale($browserLocale)) { |
88
|
|
|
return redirect()->to($this->uriLocalizer->localize($currentUrl, $browserLocale, $segment)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// If not, redirect to the default locale: |
92
|
|
|
// Keep flash data. |
93
|
|
|
if ($request->hasSession()) { |
94
|
|
|
$request->session()->reflash(); |
95
|
|
|
} |
96
|
|
|
return redirect()->to($this->uriLocalizer->localize($currentUrl, $defaultLocale, $segment)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths