|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel MultiLang package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\LaravelMultiLang; |
|
12
|
|
|
|
|
13
|
|
|
use Closure; |
|
14
|
|
|
use Illuminate\Cache\CacheManager as Cache; |
|
15
|
|
|
use Illuminate\Database\DatabaseManager as Database; |
|
16
|
|
|
use Illuminate\Http\Request; |
|
17
|
|
|
use Illuminate\Routing\Router; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
use Longman\LaravelMultiLang\Config; |
|
20
|
|
|
use Longman\LaravelMultiLang\Repository; |
|
21
|
|
|
|
|
22
|
|
|
class MultiLang |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Language/Locale. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $lang; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* System environment |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $environment; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Config. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \Longman\LaravelMultiLang\Config |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $config; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Repository |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $repository; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Texts. |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $texts; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Missing texts. |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $new_texts; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create a new MultiLang instance. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $environment |
|
70
|
|
|
* @param array $config |
|
71
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
|
72
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
|
73
|
|
|
*/ |
|
74
|
23 |
|
public function __construct($environment, array $config, Cache $cache, Database $db) |
|
75
|
|
|
{ |
|
76
|
23 |
|
$this->environment = $environment; |
|
77
|
23 |
|
$this->cache = $cache; |
|
|
|
|
|
|
78
|
23 |
|
$this->db = $db; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
23 |
|
$this->setConfig($config); |
|
81
|
|
|
|
|
82
|
23 |
|
$this->setRepository(new Repository($this->config, $cache, $db)); |
|
83
|
23 |
|
} |
|
84
|
|
|
|
|
85
|
23 |
|
public function setConfig(array $config) |
|
86
|
|
|
{ |
|
87
|
23 |
|
$this->config = new Config($config); |
|
88
|
23 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
23 |
|
public function setRepository(Repository $repository) |
|
92
|
|
|
{ |
|
93
|
23 |
|
$this->repository = $repository; |
|
|
|
|
|
|
94
|
23 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
public function getRepository() |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->repository; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set locale and load texts |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $lang |
|
106
|
|
|
* @param array $texts |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
21 |
|
public function setLocale($lang, array $texts = null) |
|
110
|
|
|
{ |
|
111
|
21 |
|
if (!$lang) { |
|
112
|
1 |
|
throw new InvalidArgumentException('Locale is empty'); |
|
113
|
|
|
} |
|
114
|
20 |
|
$this->lang = $lang; |
|
115
|
|
|
|
|
116
|
20 |
|
if (!is_array($texts)) { |
|
117
|
17 |
|
$texts = $this->loadTexts($this->getLocale()); |
|
118
|
17 |
|
} |
|
119
|
|
|
|
|
120
|
20 |
|
$this->texts = $texts; |
|
121
|
20 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Load texts |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $lang |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
17 |
|
public function loadTexts($lang) |
|
130
|
|
|
{ |
|
131
|
17 |
|
if ($this->environment != 'production' || $this->config->get('cache.enabled', true) === false) { |
|
132
|
15 |
|
$texts = $this->repository->loadFromDatabase($lang); |
|
|
|
|
|
|
133
|
15 |
|
return $texts; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
if ($this->repository->existsInCache($lang)) { |
|
|
|
|
|
|
137
|
|
|
$texts = $this->repository->loadFromCache($lang); |
|
|
|
|
|
|
138
|
|
|
} else { |
|
139
|
3 |
|
$texts = $this->repository->loadFromDatabase($lang); |
|
|
|
|
|
|
140
|
3 |
|
$this->repository->storeInCache($lang, $texts); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
3 |
|
return $texts; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get translated text |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $key |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
8 |
|
public function get($key) |
|
153
|
|
|
{ |
|
154
|
|
|
|
|
155
|
8 |
|
if (empty($key)) { |
|
156
|
1 |
|
throw new InvalidArgumentException('String key not provided'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
7 |
|
if (!$this->lang) { |
|
160
|
1 |
|
return $key; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
6 |
|
if (!isset($this->texts[$key])) { |
|
164
|
4 |
|
$this->queueToSave($key); |
|
165
|
4 |
|
return $key; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
2 |
|
$text = $this->texts[$key]; |
|
169
|
|
|
|
|
170
|
2 |
|
return $text; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get texts |
|
175
|
|
|
* |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
4 |
|
public function getRedirectUrl(Request $request) |
|
179
|
|
|
{ |
|
180
|
4 |
|
$locale = $request->segment(1); |
|
181
|
4 |
|
$fallback_locale = $this->config->get('default_locale', 'en'); |
|
182
|
4 |
|
$exclude_segments = $this->config->get('exclude_segments', []); |
|
183
|
4 |
|
if (in_array($locale, $exclude_segments)) { |
|
184
|
|
|
return null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
4 |
|
if (strlen($locale) == 2) { |
|
188
|
3 |
|
$locales = $this->config->get('locales', []); |
|
189
|
|
|
|
|
190
|
3 |
|
if (!isset($locales[$locale])) { |
|
191
|
2 |
|
$segments = $request->segments(); |
|
192
|
2 |
|
$segments[0] = $fallback_locale; |
|
193
|
2 |
|
$url = implode('/', $segments); |
|
194
|
2 |
|
if ($query_string = $request->server->get('QUERY_STRING')) { |
|
195
|
1 |
|
$url .= '?' . $query_string; |
|
196
|
1 |
|
} |
|
197
|
|
|
|
|
198
|
2 |
|
return $url; |
|
199
|
|
|
} |
|
200
|
1 |
|
} else { |
|
201
|
1 |
|
$segments = $request->segments(); |
|
202
|
1 |
|
$url = $fallback_locale . '/' . implode('/', $segments); |
|
203
|
1 |
|
if ($query_string = $request->server->get('QUERY_STRING')) { |
|
204
|
|
|
$url .= '?' . $query_string; |
|
205
|
|
|
} |
|
206
|
1 |
|
return $url; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
1 |
|
return null; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
public function detectLocale(Request $request) |
|
213
|
|
|
{ |
|
214
|
1 |
|
$locale = $request->segment(1); |
|
215
|
1 |
|
$locales = $this->config->get('locales'); |
|
216
|
|
|
|
|
217
|
1 |
|
if (isset($locales[$locale])) { |
|
218
|
1 |
|
return isset($locales[$locale]['locale']) ? $locales[$locale]['locale'] : $locale; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $this->config->get('default_locale', 'en'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function routeGroup(Closure $callback) |
|
225
|
|
|
{ |
|
226
|
|
|
$router = app('router'); |
|
227
|
|
|
|
|
228
|
|
|
$locales = $this->config->get('locales', []); |
|
229
|
|
|
|
|
230
|
|
|
foreach ($locales as $locale => $val) { |
|
231
|
|
|
$router->group([ |
|
232
|
|
|
'prefix' => $locale, |
|
233
|
|
|
'as' => $locale.'.', |
|
234
|
|
|
], $callback); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function manageTextsRoutes() |
|
240
|
|
|
{ |
|
241
|
|
|
$router = app('router'); |
|
242
|
|
|
$route = $this->config->get('text-route.route', 'texts'); |
|
243
|
|
|
$controller = $this->config->get('text-route.controller', '\Longman\LaravelMultiLang\Controllers\TextsController'); |
|
244
|
|
|
|
|
245
|
|
|
$router->get( |
|
246
|
|
|
$route, |
|
247
|
|
|
['uses' => $controller . '@index'] |
|
248
|
|
|
); |
|
249
|
|
|
$router->post( |
|
250
|
|
|
$route, |
|
251
|
|
|
['uses' => $controller . '@save'] |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Get texts |
|
257
|
|
|
* |
|
258
|
|
|
* @return array |
|
259
|
|
|
*/ |
|
260
|
4 |
|
public function getTexts() |
|
261
|
|
|
{ |
|
262
|
|
|
|
|
263
|
4 |
|
return $this->texts; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Set texts manually |
|
268
|
|
|
* |
|
269
|
|
|
* @param array $texts_array |
|
270
|
|
|
* @return \Longman\LaravelMultiLang\MultiLang |
|
271
|
|
|
*/ |
|
272
|
3 |
|
public function setTexts(array $texts_array) |
|
273
|
|
|
{ |
|
274
|
3 |
|
$texts = []; |
|
275
|
3 |
|
foreach ($texts_array as $key => $value) { |
|
276
|
3 |
|
$texts[$key] = $value; |
|
277
|
3 |
|
} |
|
278
|
|
|
|
|
279
|
3 |
|
$this->texts = $texts; |
|
280
|
|
|
|
|
281
|
3 |
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Queue missing texts |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $key |
|
288
|
|
|
* @return void |
|
289
|
|
|
*/ |
|
290
|
4 |
|
protected function queueToSave($key) |
|
291
|
|
|
{ |
|
292
|
4 |
|
$this->new_texts[$key] = $key; |
|
293
|
4 |
|
} |
|
294
|
|
|
|
|
295
|
2 |
|
public function getUrl($path) |
|
296
|
|
|
{ |
|
297
|
2 |
|
$locale = $this->getLocale(); |
|
298
|
2 |
|
if ($locale) { |
|
299
|
2 |
|
$path = $locale . '/' . $path; |
|
300
|
2 |
|
} |
|
301
|
2 |
|
return $path; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
5 |
|
public function autoSaveIsAllowed() |
|
305
|
|
|
{ |
|
306
|
5 |
|
if ($this->environment == 'local' && $this->config->get('db.autosave', true)) { |
|
307
|
1 |
|
return true; |
|
308
|
|
|
} |
|
309
|
5 |
|
return false; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
18 |
|
public function getLocale() |
|
313
|
|
|
{ |
|
314
|
18 |
|
return $this->lang; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
1 |
|
public function getLocales() |
|
318
|
|
|
{ |
|
319
|
1 |
|
return $this->config->get('locales'); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
3 |
|
public function saveTexts() |
|
323
|
|
|
{ |
|
324
|
3 |
|
if (empty($this->new_texts)) { |
|
325
|
3 |
|
return false; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
3 |
|
$this->repository->save($this->new_texts); |
|
|
|
|
|
|
329
|
3 |
|
return true; |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: