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 Illuminate\Contracts\Cache\Factory as CacheContract; |
14
|
|
|
use Illuminate\Database\DatabaseManager as DatabaseContract; |
15
|
|
|
use Illuminate\Support\Collection; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
class MultiLang |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Language/Locale. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $lang; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* System environment |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $environment; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The instance of the laravel app. |
36
|
|
|
* |
37
|
|
|
* @var \Illuminate\Foundation\Application |
38
|
|
|
*/ |
39
|
|
|
protected $app; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The instance of the cache. |
43
|
|
|
* |
44
|
|
|
* @var \Illuminate\Cache\CacheManager |
45
|
|
|
*/ |
46
|
|
|
protected $cache; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Config. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $config; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The instance of the database. |
57
|
|
|
* |
58
|
|
|
* @var \Illuminate\Database\DatabaseManager |
59
|
|
|
*/ |
60
|
|
|
protected $db; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Name of the cache. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $cache_name; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Texts collection. |
71
|
|
|
* |
72
|
|
|
* @var \Illuminate\Support\Collection |
73
|
|
|
*/ |
74
|
|
|
protected $texts; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Missing texts. |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected $new_texts; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create a new MultiLang instance. |
85
|
|
|
* |
86
|
|
|
* @param string $environment |
87
|
|
|
* @param array $config |
88
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
89
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
90
|
|
|
* @return void |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function __construct($environment, array $config, CacheContract $cache, DatabaseContract $db) |
93
|
|
|
{ |
94
|
|
|
$this->environment = $environment; |
95
|
|
|
$this->cache = $cache; |
|
|
|
|
96
|
|
|
$this->db = $db; |
97
|
|
|
|
98
|
|
|
$this->setConfig($config); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function setConfig(array $config) |
102
|
|
|
{ |
103
|
|
|
$this->config = [ |
104
|
|
|
'enabled' => true, |
105
|
|
|
'locales' => [ |
106
|
|
|
'en' => [ |
107
|
|
|
'name' => 'English', |
108
|
|
|
'native_name' => 'English', |
109
|
|
|
'default' => true, |
110
|
|
|
], |
111
|
|
|
], |
112
|
|
|
'autosave' => true, |
113
|
|
|
'cache' => true, |
114
|
|
|
'cache_lifetime' => 1440, |
115
|
|
|
'texts_table' => 'texts', |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
foreach ($config as $k => $v) { |
119
|
|
|
$this->config[$k] = $v; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getConfig($key = null) |
124
|
|
|
{ |
125
|
|
|
if ($key === null) { |
126
|
|
|
return $this->config; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return isset($this->config[$key]) ? $this->config[$key] : null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set locale and load texts |
134
|
|
|
* |
135
|
|
|
* @param string $lang |
136
|
|
|
* @param \Illuminate\Support\Collection|array $text |
|
|
|
|
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function setLocale($lang, $texts = null) |
140
|
|
|
{ |
141
|
|
|
if (!$lang) { |
142
|
|
|
throw new InvalidArgumentException('Locale is empty!'); |
143
|
|
|
} |
144
|
|
|
$this->lang = $lang; |
145
|
|
|
|
146
|
|
|
$this->setCacheName(); |
147
|
|
|
|
148
|
|
|
if (is_array($texts)) { |
149
|
|
|
$texts = new Collection($texts); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->texts = $texts ? $texts : $this->loadTexts($this->getLocale()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Load texts |
157
|
|
|
* |
158
|
|
|
* @param string $lang |
159
|
|
|
* @return \Illuminate\Support\Collection |
160
|
|
|
*/ |
161
|
|
|
public function loadTexts($lang = null) |
162
|
|
|
{ |
163
|
|
|
$cache = $this->getConfig('cache'); |
164
|
|
|
|
165
|
|
|
if (!$cache || $this->cache === null || $this->environment != 'production') { |
166
|
|
|
$texts = $this->loadTextsFromDatabase($lang); |
167
|
|
|
return $texts; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if ($this->mustLoadFromCache()) { |
171
|
|
|
$texts = $this->loadTextsFromCache(); |
172
|
|
|
} else { |
173
|
|
|
$texts = $this->loadTextsFromDatabase($lang); |
174
|
|
|
$this->storeTextsInCache($texts); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$texts = new Collection($texts); |
178
|
|
|
|
179
|
|
|
return $texts; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get translated text |
184
|
|
|
* |
185
|
|
|
* @param string $key |
186
|
|
|
* @param string $default |
|
|
|
|
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function get($key) |
190
|
|
|
{ |
191
|
|
|
|
192
|
|
|
if (empty($key)) { |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (!$this->lang) { |
197
|
|
|
return $key; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (!$this->texts->has($key)) { |
201
|
|
|
$this->queueToSave($key); |
202
|
|
|
return $key; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$text = $this->texts->get($key); |
206
|
|
|
|
207
|
|
|
return $text; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get texts |
212
|
|
|
* |
213
|
|
|
* @param string $lang |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
public function getTexts($lang = null) |
217
|
|
|
{ |
218
|
|
|
|
219
|
|
|
return $this->loadTexts($lang); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set texts manually |
224
|
|
|
* |
225
|
|
|
* @param array $texts_array |
226
|
|
|
* @return \Longman\LaravelMultiLang\MultiLang |
227
|
|
|
*/ |
228
|
|
|
public function setTexts(array $texts_array) |
229
|
|
|
{ |
230
|
|
|
$texts = []; |
231
|
|
|
foreach ($texts_array as $key => $value) { |
232
|
|
|
$texts[$key] = $value; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->texts = new Collection($texts); |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Queue missing texts |
242
|
|
|
* |
243
|
|
|
* @param string $key |
244
|
|
|
* @return void |
245
|
|
|
*/ |
246
|
|
|
protected function queueToSave($key) |
247
|
|
|
{ |
248
|
|
|
$this->new_texts[$key] = $key; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Check if we must load texts from cache |
253
|
|
|
* |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
protected function mustLoadFromCache() |
257
|
|
|
{ |
258
|
|
|
return $this->cache->has($this->cache_name); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
protected function storeTextsInCache(array $texts) |
262
|
|
|
{ |
263
|
|
|
$cache_lifetime = $this->getConfig('cache_lifetime', 1440); |
|
|
|
|
264
|
|
|
$status = $this->cache->put($this->cache_name, $texts, $cache_lifetime); |
265
|
|
|
return $status; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
protected function loadTextsFromDatabase($lang) |
269
|
|
|
{ |
270
|
|
|
$texts = $lang ? $this->db->table($this->getTableName()) |
271
|
|
|
->where('lang', $lang) |
272
|
|
|
->get(['key', 'value', 'lang', 'scope']) : $this->db->table($this->getTableName())->get(['key', 'value', 'lang', 'scope']); |
273
|
|
|
|
274
|
|
|
$array = []; |
275
|
|
|
foreach ($texts as $row) { |
276
|
|
|
$array[$row->key] = $row->value; |
277
|
|
|
} |
278
|
|
|
return $array; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function loadTextsFromCache() |
282
|
|
|
{ |
283
|
|
|
$texts = $this->cache->get($this->cache_name); |
|
|
|
|
284
|
|
|
return $texts; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
protected function setCacheName() |
288
|
|
|
{ |
289
|
|
|
$this->cache_name = 'texts.' . $this->lang; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function getUrl($path) |
293
|
|
|
{ |
294
|
|
|
$locale = $this->getLocale(); |
295
|
|
|
if ($locale) { |
296
|
|
|
$path = $locale . '/' . $path; |
297
|
|
|
} |
298
|
|
|
return $path; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function autoSaveIsAllowed() |
302
|
|
|
{ |
303
|
|
|
if ($this->environment == 'local' && $this->getConfig('autosave') && $this->db !== null) { |
304
|
|
|
return true; |
305
|
|
|
} |
306
|
|
|
return false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function getLocale() |
310
|
|
|
{ |
311
|
|
|
return $this->lang; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function saveTexts() |
315
|
|
|
{ |
316
|
|
|
if (empty($this->new_texts)) { |
317
|
|
|
return null; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$ins = []; |
321
|
|
|
$placeholders = []; |
322
|
|
|
$lang = $this->lang; |
323
|
|
|
$i = 1; |
324
|
|
|
foreach ($this->new_texts as $k => $v) { |
325
|
|
|
$ins['key' . $i] = $k; |
326
|
|
|
$ins['lang' . $i] = $lang; |
327
|
|
|
$ins['value' . $i] = $v; |
328
|
|
|
|
329
|
|
|
$placeholders[] = '(:key' . $i . ', :lang' . $i . ', :value' . $i . ')'; |
330
|
|
|
$i++; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$fields = ['key', 'lang', 'value']; |
334
|
|
|
|
335
|
|
|
$placeholders = implode(', ', $placeholders); |
336
|
|
|
|
337
|
|
|
$table = $this->getTableName(true); |
338
|
|
|
|
339
|
|
|
$query = 'INSERT IGNORE |
340
|
|
|
INTO `' . $table . '` (`' . implode('`, `', $fields) . '`) |
341
|
|
|
VALUES ' . $placeholders; |
342
|
|
|
|
343
|
|
|
$this->db->insert($query, $ins); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
protected function getTableName($with_prefix = false) |
347
|
|
|
{ |
348
|
|
|
$table = $this->getConfig('texts_table'); |
349
|
|
|
if ($with_prefix) { |
350
|
|
|
$prefix = $this->db->getTablePrefix(); |
351
|
|
|
$table = $prefix . $table; |
352
|
|
|
} |
353
|
|
|
return $table; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.