-
Notifications
You must be signed in to change notification settings - Fork 1
/
Localization.php
executable file
·110 lines (87 loc) · 2.87 KB
/
Localization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace AlexJoffroy\Localization;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
class Localization
{
/** @var \Illuminate\Contracts\Container\Container */
protected $app;
public function __construct(Container $app)
{
$this->app = $app;
}
protected function config(string $key)
{
return config("localization.$key");
}
public function getLocale(): string
{
return $this->app->getLocale();
}
public function setLocale(string $locale = '')
{
$this->app->setLocale($locale);
}
public function isCurrentLocale(string $locale = ''): bool
{
return $locale === $this->getLocale();
}
public function getSupportedLocale(string $locale = ''): Collection
{
$locales = $this->getSupportedLocales();
if ($locales->has($locale)) {
return collect($locales->get($locale));
}
return collect([]);
}
public function getSupportedLocales(): Collection
{
return collect($this->config('supported_locales'));
}
public function getSupportedLocalesKeys(): Collection
{
return $this->getSupportedLocales()->keys();
}
public function isSupportedLocale(string $locale = ''): bool
{
return $this->getSupportedLocales()->has($locale);
}
public function getDefaultLocale(): string
{
return $this->config('default_locale');
}
public function isDefaultLocale(string $locale = ''): bool
{
return $locale === $this->getDefaultLocale();
}
public function shouldHideLocaleInUrl($locale)
{
return $this->config('hide_default_locale_in_url')
&& $this->isDefaultLocale($locale);
}
public function route(string $name, array $parameters = [], bool $absolute = true, string $locale = ''): string
{
$locale = $this->isSupportedLocale($locale) ? $locale : $this->getLocale();
$localesPattern = $this->getSupportedLocalesKeys()->implode('|');
$name = preg_replace("/^($localesPattern)\./", '', $name);
return $this->app->url->route("$locale.$name", $parameters, $absolute);
}
public function currentRoute(string $locale, bool $absolute = true): string
{
$request = $this->app->request;
$routeName = $request->route()->getName();
$parameters = $request->route()->parameters();
$url = $this->route($routeName, $parameters, $absolute, $locale);
if ($query = $request->query()) {
$url .= '?' . http_build_query($query);
}
return $url;
}
public function renderSwitch(string $view = 'localization::switch', array $data = []): HtmlString
{
return new HtmlString(view($view, array_merge($data, [
'l10n' => $this,
]))->render());
}
}