|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Thinktomorrow\Locale\Detectors\FallbackDetector; |
|
7
|
|
|
use Thinktomorrow\Locale\Detectors\HiddenSegmentDetector; |
|
8
|
|
|
use Thinktomorrow\Locale\Detectors\QueryDetector; |
|
9
|
|
|
use Thinktomorrow\Locale\Detectors\SegmentDetector; |
|
10
|
|
|
use Thinktomorrow\Locale\Values\Config; |
|
11
|
|
|
use Thinktomorrow\Locale\Values\Locale; |
|
12
|
|
|
|
|
13
|
|
|
final class Detect |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Request |
|
18
|
|
|
*/ |
|
19
|
|
|
private $request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Config |
|
23
|
|
|
*/ |
|
24
|
|
|
private $config; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Locale |
|
28
|
|
|
*/ |
|
29
|
|
|
private $locale; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Current scope of locales |
|
33
|
|
|
* @var Scope |
|
34
|
|
|
*/ |
|
35
|
|
|
private $scope; |
|
36
|
|
|
|
|
37
|
73 |
|
public function __construct(Request $request, Config $config) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
73 |
|
$this->request = $request; |
|
40
|
73 |
|
$this->config = $config; |
|
41
|
73 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Detect the locale from current request url. |
|
45
|
|
|
* Once the locale has been determined, it will be set as the application locale. |
|
46
|
|
|
* A locale is only validated if it is present within the current locale scope. |
|
47
|
|
|
* |
|
48
|
|
|
* Detection honours following priority: |
|
49
|
|
|
* 1) If locale is found in request as query parameter e.g. ?locale=fr, |
|
50
|
|
|
* 2) If locale is found in request url, either from host or segment eg. nl.example.com, example.nl or example.com/nl |
|
51
|
|
|
* 3) Otherwise set locale to our fallback language (app.locale) |
|
52
|
|
|
*/ |
|
53
|
28 |
|
public function detectLocale(): self |
|
54
|
|
|
{ |
|
55
|
28 |
|
$locale = null; |
|
56
|
|
|
|
|
57
|
|
|
$detectors = [ |
|
58
|
28 |
|
FallbackDetector::class, |
|
59
|
|
|
HiddenSegmentDetector::class, |
|
60
|
|
|
SegmentDetector::class, |
|
61
|
|
|
QueryDetector::class, |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
28 |
|
foreach($detectors as $detector) |
|
65
|
|
|
{ |
|
66
|
28 |
|
$locale = app($detector)->get($this->getScope(), $this->config) ?? $locale; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
28 |
|
$this->locale = $locale; |
|
70
|
|
|
|
|
71
|
28 |
|
app()->setLocale($locale->get()); |
|
72
|
|
|
|
|
73
|
28 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function getLocale(): Locale |
|
77
|
|
|
{ |
|
78
|
1 |
|
if( ! $this->locale ) $this->detectLocale(); |
|
79
|
|
|
|
|
80
|
1 |
|
return $this->locale; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
72 |
|
public function getScope(): Scope |
|
84
|
|
|
{ |
|
85
|
72 |
|
if( ! $this->scope ) $this->detectScope(); |
|
86
|
|
|
|
|
87
|
72 |
|
return $this->scope; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* This is handy for setting allowed scope via other source than config file. |
|
92
|
|
|
* TODO: we should allow to set config from other source such as db as well so |
|
93
|
|
|
* this can be configurable from a cms. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Scope|null $scope |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
11 |
|
public function setScope(Scope $scope = null) |
|
99
|
|
|
{ |
|
100
|
11 |
|
if($scope) $this->scope = $scope; |
|
101
|
|
|
|
|
102
|
11 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
69 |
|
private function detectScope() |
|
106
|
|
|
{ |
|
107
|
69 |
|
$this->scope = ScopeCollection::fromConfig($this->config)->findByRoot($this->request->root()); |
|
108
|
69 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|