1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pmochine\LaravelTongue; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Foundation\Application; |
7
|
|
|
use Pmochine\LaravelTongue\Misc\Config; |
8
|
|
|
use Pmochine\LaravelTongue\Localization\Localization; |
9
|
|
|
use Pmochine\LaravelTongue\Exceptions\SupportedLocalesNotDefined; |
10
|
|
|
|
11
|
|
|
class Tongue |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Our instance of the Laravel app. |
15
|
|
|
* |
16
|
|
|
* @var Illuminate\Foundation\Application |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
protected $app = ''; |
19
|
|
|
|
20
|
|
|
public function __construct(Application $app) |
21
|
|
|
{ |
22
|
|
|
$this->app = $app; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Detects the tongue, the locale |
27
|
|
|
* of the User. |
28
|
|
|
* |
29
|
|
|
* @return Tongue :P |
30
|
|
|
*/ |
31
|
|
|
public function detect() |
32
|
|
|
{ |
33
|
|
|
$locale = $this->findLocale(); |
34
|
|
|
|
35
|
|
|
$this->speaks($locale); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Gets the current speaking tongue... |
42
|
|
|
* (language code). |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function current($key = null) |
47
|
|
|
{ |
48
|
|
|
$locale = $this->app->getLocale(); |
49
|
|
|
|
50
|
|
|
if (!$key) { |
51
|
|
|
return $locale; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->speaking($key, $locale); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets the twist of the tongue. |
59
|
|
|
* Return the direction left or right. |
60
|
|
|
* e.g. for arabic language. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function leftOrRight() |
65
|
|
|
{ |
66
|
|
|
switch (Config::supportedLocales()[$this->current()]['script']) { |
67
|
|
|
case 'Arab': |
68
|
|
|
case 'Hebr': |
69
|
|
|
case 'Mong': |
70
|
|
|
case 'Tfng': |
71
|
|
|
case 'Thaa': |
72
|
|
|
return 'rtl'; |
73
|
|
|
default: |
74
|
|
|
return 'ltr'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* A tongue-twister is a phrase that is |
80
|
|
|
* designed to be difficult to articulate properly, |
81
|
|
|
* So lets just asume the user just can't speak the |
82
|
|
|
* language... |
83
|
|
|
* |
84
|
|
|
* @return bool (yes if its not speakable) |
85
|
|
|
*/ |
86
|
|
|
public function twister() |
87
|
|
|
{ |
88
|
|
|
$locale = Localization::fromUrl(); |
89
|
|
|
|
90
|
|
|
if (tongue()->speaking('subdomains', $locale)) { |
91
|
|
|
//whitelisted subdomains! like admin.domain.com |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//custom subdomains with locale. gewinnen.domain.com -> de as locale |
96
|
|
|
if ($customLocale = tongue()->speaking('aliases', $locale)) { |
97
|
|
|
//but we need to check again if it is spoken or not |
98
|
|
|
return $this->current() != $customLocale; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
//fallback language is the same as the current language |
102
|
|
|
if (Config::beautify() && $this->current() === Config::fallbackLocale()) { |
103
|
|
|
//didn't found locale means browser is set to exmaple.com |
104
|
|
|
if (!$locale) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
//browser is set to en.example.com but should be forced back to example.com |
108
|
|
|
if ($locale === Config::fallbackLocale()) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
//decipher from |
114
|
|
|
return $this->current() != $locale; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The user speaks locale language. |
119
|
|
|
* Set the locale. |
120
|
|
|
* |
121
|
|
|
* @param string $locale |
122
|
|
|
* @return Tongue :P |
123
|
|
|
*/ |
124
|
|
|
public function speaks(string $locale) |
125
|
|
|
{ |
126
|
|
|
if (!$this->isSpeaking($locale)) { |
127
|
|
|
return abort(404); //oder error? |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->app->setLocale($this->locale = $locale); |
|
|
|
|
131
|
|
|
|
132
|
|
|
if ($locale != Localization::cookie() && Config::cookieLocalization()) { |
133
|
|
|
Localization::cookie($locale); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Regional locale such as de_DE, so formatLocalized works in Carbon |
137
|
|
|
$regional = $this->speaking('regional', $locale); |
138
|
|
|
|
139
|
|
|
if ($regional) { |
140
|
|
|
setlocale(LC_TIME, $regional . '.UTF-8'); |
|
|
|
|
141
|
|
|
setlocale(LC_MONETARY, $regional . '.UTF-8'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Used to return back to previous url. |
149
|
|
|
* e.g. if you change the language. its usefull. |
150
|
|
|
* |
151
|
|
|
* @return Illuminate\Routing\Redirector |
|
|
|
|
152
|
|
|
*/ |
153
|
|
|
public function back() |
154
|
|
|
{ |
155
|
|
|
return dialect()->redirect(dialect()->redirectUrl(url()->previous())); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Gets the collection list of all languages, |
160
|
|
|
* the website speaks. Or give us the specific keys. |
161
|
|
|
* |
162
|
|
|
* @return collection|string|array|null |
|
|
|
|
163
|
|
|
*/ |
164
|
|
|
public function speaking($key = null, $locale = null) |
165
|
|
|
{ |
166
|
|
|
$locales = Config::supportedLocales(); |
167
|
|
|
|
168
|
|
|
if (empty($locales) || !is_array($locales)) { |
169
|
|
|
throw new SupportedLocalesNotDefined(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!$key) { |
173
|
|
|
return collect($locales); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($key === 'BCP47') { |
177
|
|
|
return $this->BCP47($locale, $locales); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($key === 'subdomains') { |
181
|
|
|
return $this->getSubdomains($locale); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($key === 'aliases') { |
185
|
|
|
return $this->getAliases($locale); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (!Arr::has($locales, "{$locale}.{$key}")) { |
189
|
|
|
throw new SupportedLocalesNotDefined(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return data_get($locales, "{$locale}.{$key}"); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Finds the Subdomain in the URL. |
197
|
|
|
* Like en, de... |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
protected function findLocale() |
202
|
|
|
{ |
203
|
|
|
return Localization::decipherTongue(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Checks if your page is speaking the language. |
208
|
|
|
* |
209
|
|
|
* @param string $locale |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function isSpeaking($locale) |
213
|
|
|
{ |
214
|
|
|
return array_key_exists($locale, Config::supportedLocales()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Gets the BCP 47 Value of the regional |
219
|
|
|
* See for more: http://schneegans.de/lv/?tags=en&format=text. |
220
|
|
|
* |
221
|
|
|
* @param string $locale |
222
|
|
|
* @param array $loacles [the list in the config file] |
223
|
|
|
*/ |
224
|
|
|
protected function BCP47($locale, $locales) |
225
|
|
|
{ |
226
|
|
|
$bcp47 = data_get($locales, "{$locale}.regional"); |
227
|
|
|
|
228
|
|
|
if (!$bcp47) { |
229
|
|
|
return $locale; |
230
|
|
|
} //locale is the "minimum" of BCP 47 |
231
|
|
|
|
232
|
|
|
//regional value needs to replace underscore |
233
|
|
|
return str_replace('_', '-', $bcp47); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $subdomain [like "admin"] |
238
|
|
|
* |
239
|
|
|
* @return array|bool |
240
|
|
|
*/ |
241
|
|
|
protected function getSubdomains(string $subdomain = null) |
242
|
|
|
{ |
243
|
|
|
if (is_null($subdomain)) { |
244
|
|
|
return Config::subdomains(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return in_array($subdomain, Config::subdomains()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Gets the array of the config, or gets the locale value of a subdomain. |
252
|
|
|
* Like: "gewinnen" -> "de". |
253
|
|
|
* |
254
|
|
|
* @param string $subdomain |
255
|
|
|
* |
256
|
|
|
* @return array|string |
257
|
|
|
*/ |
258
|
|
|
protected function getAliases(string $subdomain = null) |
259
|
|
|
{ |
260
|
|
|
$domains = Config::aliases(); |
261
|
|
|
|
262
|
|
|
if (is_null($subdomain)) { |
263
|
|
|
return $domains; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if (array_key_exists($subdomain, $domains)) { |
267
|
|
|
return $domains[$subdomain]; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return ''; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|