1 | <?php |
||||||
2 | |||||||
3 | namespace Pmochine\LaravelTongue\Localization; |
||||||
4 | |||||||
5 | use Illuminate\Support\Arr; |
||||||
6 | use Pmochine\LaravelTongue\Misc\Config; |
||||||
7 | use Pmochine\LaravelTongue\Misc\Cookie; |
||||||
8 | use Pmochine\LaravelTongue\Misc\Url; |
||||||
9 | |||||||
10 | class Localization |
||||||
11 | { |
||||||
12 | /** |
||||||
13 | * The goal is to find the language (the locale). |
||||||
14 | * We look at the subdomain or in the cookies or browser language. |
||||||
15 | * |
||||||
16 | * @return string [Finds the locale of the url] |
||||||
17 | */ |
||||||
18 | public static function decipherTongue() |
||||||
19 | { |
||||||
20 | $locale = self::fromUrl(); |
||||||
21 | |||||||
22 | // there is no subdomain found |
||||||
23 | if ($locale === false) { |
||||||
24 | // this could be a future bug |
||||||
25 | // when no middleware is active the language is not set right |
||||||
26 | // domain.com could be in german etc... |
||||||
27 | if (! Config::beautify()) { |
||||||
28 | // if the middleware is active we should be redirected to en.domain.com |
||||||
29 | // if not the fallback language is going to be used |
||||||
30 | return Config::fallbackLocale(); |
||||||
31 | } |
||||||
32 | // we are checking if we have languages set in cookies or in the browser |
||||||
33 | return self::currentTongue(); |
||||||
34 | } |
||||||
35 | |||||||
36 | // could be a custom subdomain |
||||||
37 | if (! tongue()->isSpeaking($locale)) { |
||||||
38 | // check if it is a white listed domain |
||||||
39 | |||||||
40 | if (tongue()->speaking('subdomains', $locale)) { |
||||||
41 | return self::currentTongue(); |
||||||
42 | } |
||||||
43 | |||||||
44 | // check if we have a custom locale subdomain, if not it returns a null |
||||||
45 | $locale = tongue()->speaking('aliases', $locale) ?: $locale; |
||||||
46 | } |
||||||
47 | |||||||
48 | return $locale; |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
49 | } |
||||||
50 | |||||||
51 | /** |
||||||
52 | * Gets the locale from the url. |
||||||
53 | * |
||||||
54 | * @return string|bool false [when hostname===locale] |
||||||
55 | */ |
||||||
56 | public static function fromUrl() |
||||||
57 | { |
||||||
58 | return Url::hasSubdomain() ? Url::subdomain() : false; |
||||||
59 | } |
||||||
60 | |||||||
61 | /** |
||||||
62 | * Tries to get the current locale of the user. |
||||||
63 | * Via the Browser or the fallback language. |
||||||
64 | * |
||||||
65 | * @return string |
||||||
66 | */ |
||||||
67 | public static function currentTongue(): string |
||||||
68 | { |
||||||
69 | if (Config::cookieLocalization() && $locale = self::cookie()) { |
||||||
70 | return $locale; |
||||||
71 | } |
||||||
72 | |||||||
73 | if (Config::acceptLanguage() && self::languageIsSet()) { |
||||||
74 | $detector = new TongueDetector(Config::fallbackLocale(), Config::supportedLocales(), request()); |
||||||
75 | |||||||
76 | return $detector->negotiateLanguage(); |
||||||
77 | } |
||||||
78 | |||||||
79 | return Config::fallbackLocale(); |
||||||
80 | } |
||||||
81 | |||||||
82 | /** |
||||||
83 | * Only for testing the TongueDetector. |
||||||
84 | * |
||||||
85 | * @return bool |
||||||
86 | */ |
||||||
87 | protected static function languageIsSet(): bool |
||||||
88 | { |
||||||
89 | return ! app()->runningInConsole() || Arr::has(request()->server(), 'HTTP_ACCEPT_LANGUAGE'); |
||||||
0 ignored issues
–
show
The method
runningInConsole() does not exist on Illuminate\Container\Container . Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
request()->server() can also be of type string ; however, parameter $array of Illuminate\Support\Arr::has() does only seem to accept ArrayAccess|array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
90 | } |
||||||
91 | |||||||
92 | /** |
||||||
93 | * Sets or gets the cookie of the locale. |
||||||
94 | * Important to set config/session domain to .exmaple.com |
||||||
95 | * https://gistlog.co/JacobBennett/15558410de2a394373ac. |
||||||
96 | * |
||||||
97 | * @param string $locale |
||||||
98 | * @return string|null |
||||||
99 | */ |
||||||
100 | public static function cookie(string $locale = null): ?string |
||||||
101 | { |
||||||
102 | $cookie = new Cookie('tongue-locale', Config::cookieSerialize()); //Name of the cookie |
||||||
103 | |||||||
104 | if ($locale !== null) { |
||||||
105 | $cookie->save($locale); |
||||||
106 | |||||||
107 | return null; |
||||||
108 | } |
||||||
109 | |||||||
110 | return $cookie->get(); |
||||||
111 | } |
||||||
112 | } |
||||||
113 |