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