1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pmochine\LaravelTongue\Localization; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Pmochine\LaravelTongue\Misc\Config; |
7
|
|
|
use Illuminate\Contracts\Encryption\DecryptException; |
8
|
|
|
|
9
|
|
|
class Localization |
10
|
|
|
{ |
11
|
|
|
/** Name of the cookie */ |
12
|
|
|
const COOKIE = 'tongue-locale'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The goal is to find the language (the locale). |
16
|
|
|
* We look at the subdomain or in the cookies or browser language. |
17
|
|
|
* |
18
|
|
|
* @return stirng [Finds the locale of the url] |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
public static function decipherTongue() |
21
|
|
|
{ |
22
|
|
|
$locale = self::fromUrl(); |
23
|
|
|
|
24
|
|
|
// there is no subdomain found |
25
|
|
|
if ($locale === false) { |
26
|
|
|
// this could be a future bug |
27
|
|
|
// when no middleware is active the language is not set right |
28
|
|
|
// domain.com could be in german etc... |
29
|
|
|
if (! Config::beautify()) { |
30
|
|
|
// if the middleware is active we should be redirected to en.domain.com |
31
|
|
|
// if not the fallback language is going to be used |
32
|
|
|
return Config::fallbackLocale(); |
33
|
|
|
} |
34
|
|
|
// we are checking if we have languages set in cookies or in the browser |
35
|
|
|
return self::currentTongue(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// could be a custom subdomain |
39
|
|
|
if (! tongue()->isSpeaking($locale)) { |
40
|
|
|
// check if it is a white listed domain |
41
|
|
|
if (tongue()->speaking('subdomains', $locale)) { |
42
|
|
|
return self::currentTongue(); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
// check if we have a custom locale subdomain, if not it returns a null |
45
|
|
|
$locale = tongue()->speaking('custom-subdomains', $locale); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $locale; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets the locale from the url. |
53
|
|
|
* @return string|bool false [when hostname===locale] |
54
|
|
|
*/ |
55
|
|
|
public static function fromUrl() |
56
|
|
|
{ |
57
|
|
|
$hostname = explode('.', self::domain())[0]; |
58
|
|
|
$locale = explode('.', request()->getHost())[0]; |
59
|
|
|
|
60
|
|
|
return $hostname === $locale ? false : $locale; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the registrable Domain of the website from the config. |
65
|
|
|
* If not set we are going to get it with TLDExtract |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public static function domain(): string |
69
|
|
|
{ |
70
|
|
|
if ($domain = Config::domain()) return $domain; |
71
|
|
|
|
72
|
|
|
return self::extractDomain(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
* Gets the registrable Domain of the website. |
78
|
|
|
* |
79
|
|
|
* https://github.com/layershifter/TLDExtract |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected static function extractDomain(): string |
84
|
|
|
{ |
85
|
|
|
$extract = new \LayerShifter\TLDExtract\Extract(); |
86
|
|
|
|
87
|
|
|
$result = $extract->parse(request()->getHost()); |
88
|
|
|
|
89
|
|
|
return $result->getRegistrableDomain(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Tries to get the current locale of the user. |
94
|
|
|
* Via the Browser or the fallback language. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected static function currentTongue() |
99
|
|
|
{ |
100
|
|
|
if (Config::cookieLocalization() && $locale = self::cookie()) { |
101
|
|
|
return $locale; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (Config::acceptLanguage() && self::languageIsSet()) { |
105
|
|
|
$detector = new TongueDetector(Config::fallbackLocale(), Config::supportedLocales(), request()); |
106
|
|
|
|
107
|
|
|
return $detector->negotiateLanguage(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return Config::fallbackLocale(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Only for testing the TongueDetector. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected static function languageIsSet() |
119
|
|
|
{ |
120
|
|
|
return ! app()->runningInConsole() || Arr::has(request()->server(), 'HTTP_ACCEPT_LANGUAGE'); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets or gets the cookie of the locale. |
125
|
|
|
* Important to set config/session domain to .exmaple.com |
126
|
|
|
* https://gistlog.co/JacobBennett/15558410de2a394373ac. |
127
|
|
|
* |
128
|
|
|
* @param string $locale |
129
|
|
|
* @return string|null |
130
|
|
|
*/ |
131
|
|
|
public static function cookie($locale = null) |
132
|
|
|
{ |
133
|
|
|
if ($locale != null) { |
|
|
|
|
134
|
|
|
return cookie()->queue(cookie()->forever(self::COOKIE, $locale)); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (! request()->hasCookie(self::COOKIE)) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
//Somehow I got this error: unserialize(): Error at offset 0 of 2 bytes |
143
|
|
|
//I needed to change decrypt(value, unserialize = false); |
144
|
|
|
return app('encrypter')->decrypt(request()->cookie(self::COOKIE), false); |
145
|
|
|
} catch (DecryptException $e) { |
146
|
|
|
//Somehow the middleware for decrypting does not kick in here... |
147
|
|
|
//but it even fails if we use php artisan <something> (weird) |
148
|
|
|
//if it happes we can simply give it normally back |
149
|
|
|
return request()->cookie(self::COOKIE); |
|
|
|
|
150
|
|
|
} catch (Exception $e) { |
|
|
|
|
151
|
|
|
//So I don't return a cookie in that case |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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