1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexJoffroy\Localization; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\HtmlString; |
7
|
|
|
use Illuminate\Contracts\Container\Container; |
8
|
|
|
|
9
|
|
|
class Localization |
10
|
|
|
{ |
11
|
|
|
/** @var \Illuminate\Contracts\Container\Container */ |
12
|
|
|
protected $app; |
13
|
|
|
|
14
|
27 |
|
public function __construct(Container $app) |
15
|
|
|
{ |
16
|
27 |
|
$this->app = $app; |
17
|
27 |
|
} |
18
|
|
|
|
19
|
23 |
|
protected function config(string $key) |
20
|
|
|
{ |
21
|
23 |
|
return config("localization.$key"); |
22
|
|
|
} |
23
|
|
|
|
24
|
21 |
|
public function getLocale(): string |
25
|
|
|
{ |
26
|
21 |
|
return $this->app->getLocale(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
22 |
|
public function setLocale(string $locale = '') |
30
|
|
|
{ |
31
|
22 |
|
$this->app->setLocale($locale); |
|
|
|
|
32
|
22 |
|
} |
33
|
|
|
|
34
|
3 |
|
public function isCurrentLocale(string $locale = ''): bool |
35
|
|
|
{ |
36
|
3 |
|
return $locale === $this->getLocale(); |
37
|
|
|
} |
38
|
|
|
|
39
|
22 |
|
public function getSupportedLocale(string $locale = ''): Collection |
40
|
|
|
{ |
41
|
22 |
|
$locales = $this->getSupportedLocales(); |
42
|
|
|
|
43
|
22 |
|
if ($locales->has($locale)) { |
44
|
22 |
|
return collect($locales->get($locale)); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
return collect([]); |
48
|
|
|
} |
49
|
|
|
|
50
|
23 |
|
public function getSupportedLocales(): Collection |
51
|
|
|
{ |
52
|
23 |
|
return collect($this->config('supported_locales')); |
53
|
|
|
} |
54
|
|
|
|
55
|
19 |
|
public function getSupportedLocalesKeys(): Collection |
56
|
|
|
{ |
57
|
19 |
|
return $this->getSupportedLocales()->keys(); |
58
|
|
|
} |
59
|
|
|
|
60
|
9 |
|
public function isSupportedLocale(string $locale = ''): bool |
61
|
|
|
{ |
62
|
9 |
|
return $this->getSupportedLocales()->has($locale); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
public function getDefaultLocale(): string |
66
|
|
|
{ |
67
|
4 |
|
return $this->config('default_locale'); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function isDefaultLocale(string $locale = ''): bool |
71
|
|
|
{ |
72
|
2 |
|
return $locale === $this->getDefaultLocale(); |
73
|
|
|
} |
74
|
|
|
|
75
|
19 |
|
public function shouldHideLocaleInUrl($locale) |
76
|
|
|
{ |
77
|
19 |
|
return $this->config('hide_default_locale_in_url') |
78
|
19 |
|
&& $this->isDefaultLocale($locale); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
public function route(string $name, array $parameters = [], bool $absolute = true, string $locale = ''): string |
82
|
|
|
{ |
83
|
6 |
|
$locale = $this->isSupportedLocale($locale) ? $locale : $this->getLocale(); |
84
|
6 |
|
$localesPattern = $this->getSupportedLocalesKeys()->implode('|'); |
85
|
6 |
|
$name = preg_replace("/^($localesPattern)\./", '', $name); |
86
|
|
|
|
87
|
6 |
|
return $this->app->url->route("$locale.$name", $parameters, $absolute); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function currentRoute(string $locale, bool $absolute = true): string |
91
|
|
|
{ |
92
|
2 |
|
$request = $this->app->request; |
|
|
|
|
93
|
2 |
|
$routeName = $request->route()->getName(); |
94
|
2 |
|
$parameters = $request->route()->parameters(); |
95
|
2 |
|
$url = $this->route($routeName, $parameters, $absolute, $locale); |
96
|
|
|
|
97
|
2 |
|
if ($query = $request->query()) { |
98
|
1 |
|
$url .= '?' . http_build_query($query); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return $url; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function renderSwitch(string $view = 'localization::switch', array $data = []): HtmlString |
105
|
|
|
{ |
106
|
2 |
|
return new HtmlString(view($view, array_merge($data, [ |
|
|
|
|
107
|
2 |
|
'l10n' => $this, |
108
|
2 |
|
]))->render()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: