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 \Longman\LaravelMultiLang\Repository |
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
|
|
|
* Application scope. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $scope; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create a new MultiLang instance. |
75
|
|
|
* |
76
|
|
|
* @param string $environment |
77
|
|
|
* @param array $config |
78
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
79
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
80
|
|
|
*/ |
81
|
23 |
|
public function __construct($environment, array $config, Cache $cache, Database $db) |
82
|
|
|
{ |
83
|
23 |
|
$this->environment = $environment; |
84
|
23 |
|
$this->cache = $cache; |
|
|
|
|
85
|
23 |
|
$this->db = $db; |
|
|
|
|
86
|
|
|
|
87
|
23 |
|
$this->setConfig($config); |
88
|
|
|
|
89
|
23 |
|
$this->setRepository(new Repository($this->config, $cache, $db)); |
90
|
23 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set multilang config |
94
|
|
|
* |
95
|
|
|
* @param array $config |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
23 |
|
public function setConfig(array $config) |
99
|
|
|
{ |
100
|
23 |
|
$this->config = new Config($config); |
101
|
23 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set repository object |
106
|
|
|
* |
107
|
|
|
* @param \Longman\LaravelMultiLang\Repository $repository |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
23 |
|
public function setRepository(Repository $repository) |
111
|
|
|
{ |
112
|
23 |
|
$this->repository = $repository; |
113
|
23 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get repository object |
118
|
|
|
* |
119
|
|
|
* @return \Longman\LaravelMultiLang\Repository |
120
|
|
|
*/ |
121
|
2 |
|
public function getRepository() |
122
|
|
|
{ |
123
|
2 |
|
return $this->repository; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set application scope |
128
|
|
|
* |
129
|
|
|
* @param $scope |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function setScope($scope) |
133
|
|
|
{ |
134
|
|
|
$this->scope = $scope; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get application scope |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getScope() |
144
|
|
|
{ |
145
|
|
|
return $this->scope; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set locale and load texts |
150
|
|
|
* |
151
|
|
|
* @param string $lang |
152
|
|
|
* @param array $texts |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
21 |
|
public function setLocale($lang, array $texts = null) |
156
|
|
|
{ |
157
|
21 |
|
if (!$lang) { |
158
|
1 |
|
throw new InvalidArgumentException('Locale is empty'); |
159
|
|
|
} |
160
|
20 |
|
$this->lang = $lang; |
161
|
|
|
|
162
|
20 |
|
if (!is_array($texts)) { |
163
|
17 |
|
$texts = $this->loadTexts($this->getLocale(), $this->scope); |
164
|
|
|
} |
165
|
|
|
|
166
|
20 |
|
$this->texts = $texts; |
167
|
20 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Load texts |
171
|
|
|
* |
172
|
|
|
* @param string $lang |
173
|
|
|
* @param string $scope |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
17 |
|
public function loadTexts($lang, $scope = null) |
177
|
|
|
{ |
178
|
17 |
|
if ($this->environment != 'production' || $this->config->get('cache.enabled', true) === false) { |
179
|
15 |
|
$texts = $this->repository->loadFromDatabase($lang, $scope); |
180
|
15 |
|
return $texts; |
181
|
|
|
} |
182
|
|
|
|
183
|
3 |
|
if ($this->repository->existsInCache($lang)) { |
184
|
|
|
$texts = $this->repository->loadFromCache($lang, $scope); |
185
|
|
|
} else { |
186
|
3 |
|
$texts = $this->repository->loadFromDatabase($lang, $scope); |
187
|
3 |
|
$this->repository->storeInCache($lang, $texts, $scope); |
188
|
|
|
} |
189
|
|
|
|
190
|
3 |
|
return $texts; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get translated text |
195
|
|
|
* |
196
|
|
|
* @param string $key |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
8 |
|
public function get($key) |
200
|
|
|
{ |
201
|
|
|
|
202
|
8 |
|
if (empty($key)) { |
203
|
1 |
|
throw new InvalidArgumentException('String key not provided'); |
204
|
|
|
} |
205
|
|
|
|
206
|
7 |
|
if (!$this->lang) { |
207
|
1 |
|
return $key; |
208
|
|
|
} |
209
|
|
|
|
210
|
6 |
|
if (!isset($this->texts[$key])) { |
211
|
4 |
|
$this->queueToSave($key); |
212
|
4 |
|
return $key; |
213
|
|
|
} |
214
|
|
|
|
215
|
2 |
|
$text = $this->texts[$key]; |
216
|
|
|
|
217
|
2 |
|
return $text; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get redirect url in middleware |
222
|
|
|
* |
223
|
|
|
* @param \Illuminate\Http\Request $request |
224
|
|
|
* @return null|string |
225
|
|
|
*/ |
226
|
4 |
|
public function getRedirectUrl(Request $request) |
227
|
|
|
{ |
228
|
4 |
|
$locale = $request->segment(1); |
229
|
4 |
|
$fallback_locale = $this->config->get('default_locale', 'en'); |
230
|
4 |
|
$exclude_segments = $this->config->get('exclude_segments', []); |
231
|
4 |
|
if (in_array($locale, $exclude_segments)) { |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
4 |
|
if (strlen($locale) == 2) { |
236
|
3 |
|
$locales = $this->config->get('locales', []); |
237
|
|
|
|
238
|
3 |
|
if (!isset($locales[$locale])) { |
239
|
2 |
|
$segments = $request->segments(); |
240
|
2 |
|
$segments[0] = $fallback_locale; |
241
|
2 |
|
$url = implode('/', $segments); |
242
|
2 |
|
if ($query_string = $request->server->get('QUERY_STRING')) { |
243
|
1 |
|
$url .= '?' . $query_string; |
244
|
|
|
} |
245
|
|
|
|
246
|
3 |
|
return $url; |
247
|
|
|
} |
248
|
|
|
} else { |
249
|
1 |
|
$segments = $request->segments(); |
250
|
1 |
|
$url = $fallback_locale . '/' . implode('/', $segments); |
251
|
1 |
|
if ($query_string = $request->server->get('QUERY_STRING')) { |
252
|
|
|
$url .= '?' . $query_string; |
253
|
|
|
} |
254
|
1 |
|
return $url; |
255
|
|
|
} |
256
|
|
|
|
257
|
1 |
|
return null; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Detect locale based on url segment |
262
|
|
|
* |
263
|
|
|
* @param \Illuminate\Http\Request $request |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
1 |
|
public function detectLocale(Request $request) |
267
|
|
|
{ |
268
|
1 |
|
$locale = $request->segment(1); |
269
|
1 |
|
$locales = $this->config->get('locales'); |
270
|
|
|
|
271
|
1 |
|
if (isset($locales[$locale])) { |
272
|
1 |
|
return isset($locales[$locale]['locale']) ? $locales[$locale]['locale'] : $locale; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->config->get('default_locale', 'en'); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Wrap routes to available languages group |
280
|
|
|
* |
281
|
|
|
* @param \Closure $callback |
282
|
|
|
*/ |
283
|
|
|
public function routeGroup(Closure $callback) |
284
|
|
|
{ |
285
|
|
|
$router = app('router'); |
286
|
|
|
|
287
|
|
|
$locales = $this->config->get('locales', []); |
288
|
|
|
|
289
|
|
|
foreach ($locales as $locale => $val) { |
290
|
|
|
$router->group([ |
291
|
|
|
'prefix' => $locale, |
292
|
|
|
'as' => $locale . '.', |
293
|
|
|
], $callback); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Manage texts |
300
|
|
|
*/ |
301
|
|
|
public function manageTextsRoutes() |
302
|
|
|
{ |
303
|
|
|
$router = app('router'); |
304
|
|
|
$route = $this->config->get('text-route.route', 'texts'); |
305
|
|
|
$controller = $this->config->get( |
306
|
|
|
'text-route.controller', |
307
|
|
|
'\Longman\LaravelMultiLang\Controllers\TextsController' |
308
|
|
|
); |
309
|
|
|
|
310
|
|
|
$router->get( |
311
|
|
|
$route, |
312
|
|
|
['uses' => $controller . '@index'] |
313
|
|
|
); |
314
|
|
|
$router->post( |
315
|
|
|
$route, |
316
|
|
|
['uses' => $controller . '@save'] |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get texts |
322
|
|
|
* |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
4 |
|
public function getTexts() |
326
|
|
|
{ |
327
|
|
|
|
328
|
4 |
|
return $this->texts; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Set texts manually |
333
|
|
|
* |
334
|
|
|
* @param array $texts_array |
335
|
|
|
* @return \Longman\LaravelMultiLang\MultiLang |
336
|
|
|
*/ |
337
|
3 |
|
public function setTexts(array $texts_array) |
338
|
|
|
{ |
339
|
3 |
|
$texts = []; |
340
|
3 |
|
foreach ($texts_array as $key => $value) { |
341
|
3 |
|
$texts[$key] = $value; |
342
|
|
|
} |
343
|
|
|
|
344
|
3 |
|
$this->texts = $texts; |
345
|
|
|
|
346
|
3 |
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Queue missing texts |
351
|
|
|
* |
352
|
|
|
* @param string $key |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
4 |
|
protected function queueToSave($key) |
356
|
|
|
{ |
357
|
4 |
|
$this->new_texts[$key] = $key; |
358
|
4 |
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get language prefixed url |
362
|
|
|
* |
363
|
|
|
* @param $path |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
2 |
|
public function getUrl($path) |
367
|
|
|
{ |
368
|
2 |
|
$locale = $this->getLocale(); |
369
|
2 |
|
if ($locale) { |
370
|
2 |
|
$path = $locale . '/' . $path; |
371
|
|
|
} |
372
|
2 |
|
return $path; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Check if autosave allowed |
377
|
|
|
* |
378
|
|
|
* @return bool |
379
|
|
|
*/ |
380
|
4 |
|
public function autoSaveIsAllowed() |
381
|
|
|
{ |
382
|
4 |
|
if ($this->environment == 'local' && $this->config->get('db.autosave', true)) { |
383
|
1 |
|
return true; |
384
|
|
|
} |
385
|
4 |
|
return false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Get locale |
390
|
|
|
* |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
18 |
|
public function getLocale() |
394
|
|
|
{ |
395
|
18 |
|
return $this->lang; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Get available locales |
400
|
|
|
* |
401
|
|
|
* @return array |
402
|
|
|
*/ |
403
|
1 |
|
public function getLocales() |
404
|
|
|
{ |
405
|
1 |
|
return $this->config->get('locales'); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Save missing texts |
410
|
|
|
* |
411
|
|
|
* @return bool |
412
|
|
|
*/ |
413
|
3 |
|
public function saveTexts() |
414
|
|
|
{ |
415
|
3 |
|
if (empty($this->new_texts)) { |
416
|
3 |
|
return false; |
417
|
|
|
} |
418
|
|
|
|
419
|
3 |
|
$this->repository->save($this->new_texts, $this->scope); |
|
|
|
|
420
|
3 |
|
return true; |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|
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: