1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: fomvasss |
5
|
|
|
* Date: 13.11.18 |
6
|
|
|
* Time: 1:09 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Fomvasss\UrlAliases\Traits; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait Localization |
13
|
|
|
* |
14
|
|
|
* Thank developers package |
15
|
|
|
* Mcamara\LaravelLocalization\LaravelLocalization |
16
|
|
|
* |
17
|
|
|
* @package Fomvasss\UrlAliases\Traits |
18
|
|
|
*/ |
19
|
|
|
trait Localization |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns default locale. |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function getDefaultLocale() |
28
|
|
|
{ |
29
|
|
|
if (isset($this->defaultLocale)) { |
30
|
|
|
return $this->defaultLocale; |
31
|
|
|
} |
32
|
|
|
return $this->config->get('app.locale'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return an array of all supported Locales. |
37
|
|
|
* |
38
|
|
|
* @throws SupportedLocalesNotDefined |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getSupportedLocales() |
43
|
|
|
{ |
44
|
|
|
if (!empty($this->supportedLocales)) { |
45
|
|
|
return $this->supportedLocales; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$locales = $this->config->get('url-aliases-laravellocalization.supportedLocales'); |
49
|
|
|
|
50
|
|
|
if (empty($locales) || !\is_array($locales)) { |
51
|
|
|
throw new \Exception(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->supportedLocales = $locales; |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $locales; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return an array of all supported Locales but in the order the user |
61
|
|
|
* has specified in the config file. Useful for the language selector. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getLocalesOrder() |
66
|
|
|
{ |
67
|
|
|
$locales = $this->getSupportedLocales(); |
68
|
|
|
|
69
|
|
|
$order = $this->config->get('url-aliases-laravellocalization.localesOrder'); |
70
|
|
|
|
71
|
|
|
uksort($locales, function ($a, $b) use ($order) { |
72
|
|
|
$pos_a = array_search($a, $order); |
73
|
|
|
$pos_b = array_search($b, $order); |
74
|
|
|
return $pos_a - $pos_b; |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
return $locales; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns current locale name. |
82
|
|
|
* |
83
|
|
|
* @return string current locale name |
84
|
|
|
*/ |
85
|
|
|
public function getCurrentLocaleName() |
86
|
|
|
{ |
87
|
|
|
return $this->supportedLocales[$this->getCurrentLocale()]['name']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns current locale native name. |
92
|
|
|
* |
93
|
|
|
* @return string current locale native name |
94
|
|
|
*/ |
95
|
|
|
public function getCurrentLocaleNative() |
96
|
|
|
{ |
97
|
|
|
return $this->supportedLocales[$this->getCurrentLocale()]['native']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns current locale direction. |
102
|
|
|
* |
103
|
|
|
* @return string current locale direction |
104
|
|
|
*/ |
105
|
|
|
public function getCurrentLocaleDirection() |
106
|
|
|
{ |
107
|
|
|
if (!empty($this->supportedLocales[$this->getCurrentLocale()]['dir'])) { |
108
|
|
|
return $this->supportedLocales[$this->getCurrentLocale()]['dir']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
switch ($this->getCurrentLocaleScript()) { |
112
|
|
|
// Other (historic) RTL scripts exist, but this list contains the only ones in current use. |
113
|
|
|
case 'Arab': |
114
|
|
|
case 'Hebr': |
115
|
|
|
case 'Mong': |
116
|
|
|
case 'Tfng': |
117
|
|
|
case 'Thaa': |
118
|
|
|
return 'rtl'; |
119
|
|
|
default: |
120
|
|
|
return 'ltr'; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns current locale script. |
126
|
|
|
* |
127
|
|
|
* @return string current locale script |
128
|
|
|
*/ |
129
|
|
|
public function getCurrentLocaleScript() |
130
|
|
|
{ |
131
|
|
|
return $this->supportedLocales[$this->getCurrentLocale()]['script']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns current language's native reading. |
136
|
|
|
* |
137
|
|
|
* @return string current language's native reading |
138
|
|
|
*/ |
139
|
|
|
public function getCurrentLocaleNativeReading() |
140
|
|
|
{ |
141
|
|
|
return $this->supportedLocales[$this->getCurrentLocale()]['native']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns current language. |
146
|
|
|
* |
147
|
|
|
* @return string current language |
148
|
|
|
*/ |
149
|
|
|
public function getCurrentLocale() |
150
|
|
|
{ |
151
|
|
|
if ($this->currentLocale) { |
152
|
|
|
return $this->currentLocale; |
153
|
|
|
} |
154
|
|
|
// TODO |
155
|
|
|
// if ($this->useAcceptLanguageHeader() && !$this->app->runningInConsole()) { |
156
|
|
|
// $negotiator = new LanguageNegotiator($this->defaultLocale, $this->getSupportedLocales(), $this->request); |
157
|
|
|
// |
158
|
|
|
// return $negotiator->negotiateLanguage(); |
159
|
|
|
// } |
160
|
|
|
|
161
|
|
|
// or get application default language |
162
|
|
|
return $this->config->get('app.locale'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns current regional. |
167
|
|
|
* |
168
|
|
|
* @return string current regional |
169
|
|
|
*/ |
170
|
|
|
public function getCurrentLocaleRegional() |
171
|
|
|
{ |
172
|
|
|
// need to check if it exists, since 'regional' has been added |
173
|
|
|
// after version 1.0.11 and existing users will not have it |
174
|
|
|
if (isset($this->supportedLocales[$this->getCurrentLocale()]['regional'])) { |
175
|
|
|
return $this->supportedLocales[$this->getCurrentLocale()]['regional']; |
176
|
|
|
} else { |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns supported languages language key. |
183
|
|
|
* |
184
|
|
|
* @return array keys of supported languages |
185
|
|
|
*/ |
186
|
|
|
public function getSupportedLanguagesKeys() |
187
|
|
|
{ |
188
|
|
|
return array_keys($this->supportedLocales); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Check if Locale exists on the supported locales array. |
193
|
|
|
* |
194
|
|
|
* @param string|bool $locale string|bool Locale to be checked |
195
|
|
|
* |
196
|
|
|
* @throws SupportedLocalesNotDefined |
197
|
|
|
* |
198
|
|
|
* @return bool is the locale supported? |
199
|
|
|
*/ |
200
|
|
|
public function checkLocaleInSupportedLocales($locale) |
201
|
|
|
{ |
202
|
|
|
$locales = $this->getSupportedLocales(); |
203
|
|
|
if ($locale !== false && empty($locales[$locale])) { |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
} |