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