2
2
3
3
namespace Novasa \LaravelLanguageCenter ;
4
4
5
+ use Carbon \Carbon ;
5
6
use GuzzleHttp \Client ;
6
7
use Illuminate \Support \Arr ;
7
- use Illuminate \Support \Facades \Config ;
8
+ use Illuminate \Support \Facades \Cache ;
8
9
use Illuminate \Translation \LoaderInterface ;
9
10
use Illuminate \Translation \Translator as LaravelTranslator ;
10
11
@@ -26,7 +27,7 @@ public function __construct(LoaderInterface $loader, $locale)
26
27
$ this ->loader = $ loader ;
27
28
$ this ->locale = $ locale ;
28
29
29
- // Load langauges from API
30
+ // Load languages from API
30
31
if (count ($ this ->languages ) == 0 ) {
31
32
$ this ->loadLanguages ();
32
33
}
@@ -47,24 +48,30 @@ public function get($data, array $replace = [], $locale = null, $fallback = true
47
48
// Make support for array data
48
49
if (is_array ($ data )) {
49
50
$ key = $ data ['key ' ];
51
+
50
52
if (isset ($ data ['string ' ])) {
51
53
$ string = $ data ['string ' ];
52
54
}
55
+
53
56
if (isset ($ data ['platform ' ])) {
54
57
$ platform = $ data ['platform ' ];
55
58
}
59
+
56
60
if (isset ($ data ['comment ' ])) {
57
61
$ comment = $ data ['comment ' ];
58
62
}
59
63
} else {
60
64
$ key = $ data ;
61
65
}
66
+
62
67
if (!isset ($ string )) {
63
68
$ string = $ key ;
64
69
}
70
+
65
71
if (!isset ($ platform )) {
66
72
$ platform = $ this ->getDefaultPlatform ();
67
73
}
74
+
68
75
if (!isset ($ comment )) {
69
76
$ comment = null ;
70
77
}
@@ -106,42 +113,37 @@ public function get($data, array $replace = [], $locale = null, $fallback = true
106
113
return $ line ;
107
114
}
108
115
109
- /**
110
- * Retrieve a language line out the loaded array.
111
- *
112
- * @param string $namespace
113
- * @param string $group
114
- * @param string $locale
115
- * @param string $item
116
- * @param array $replace
117
- *
118
- * @return string|array|null
119
- */
120
- protected function getLine ($ namespace , $ group , $ locale , $ item , array $ replace , $ platform = null )
116
+ public function setLocale ($ locale )
121
117
{
122
- if ($ platform == null ) {
123
- $ platform = $ this ->getDefaultPlatform ();
124
- }
118
+ config ()->set ('app.locale ' , $ locale );
119
+ config ()->set ('fallback_locale ' , $ locale );
125
120
126
- $ this ->loadLanguageStrings ($ locale , $ platform );
121
+ $ this ->locale = $ locale ;
122
+ }
127
123
128
- $ key = implode ('. ' , [$ group , $ item ]);
129
- if (isset ($ this ->strings [$ locale ]) && isset ($ this ->strings [$ locale ][$ key ])) {
130
- return $ this ->makeReplacements ($ this ->strings [$ locale ][$ key ], $ replace );
124
+ public function loadLanguages ()
125
+ {
126
+ $ timestamp = Carbon::now ()->subSeconds ($ this ->getUpdateAfter ())->timestamp ;
127
+ $ lastUpdated = Cache::get ('languagecenter.timestamp ' , 0 );
128
+
129
+ if ($ timestamp > $ lastUpdated && !is_null ($ timestamp )) {
130
+ $ this ->updateLanguages ();
131
131
}
132
132
133
- $ line = Arr ::get ($ this -> loaded [ $ namespace ][ $ group ][ $ locale ], $ item );
133
+ $ languages = Cache ::get (' languagecenter.languages ' );
134
134
135
- if (is_string ($ line )) {
136
- return $ this ->makeReplacements ($ line , $ replace );
137
- } elseif (is_array ($ line ) && count ($ line ) > 0 ) {
138
- return $ line ;
135
+ foreach ($ languages as $ language ) {
136
+ $ this ->languages [] = $ language ->codename ;
137
+
138
+ if ($ language ->is_fallback ) {
139
+ $ this ->setLocale ($ language ->codename );
140
+ }
139
141
}
140
142
}
141
143
142
- protected function loadLanguageStrings ($ locale , $ platform = null , $ force = false )
144
+ public function loadStrings ($ locale , $ platform = null , $ check = null )
143
145
{
144
- // Load langauges from API
146
+ // Load languages from API
145
147
if (count ($ this ->languages ) == 0 ) {
146
148
$ this ->loadLanguages ();
147
149
}
@@ -150,82 +152,96 @@ protected function loadLanguageStrings($locale, $platform = null, $force = false
150
152
$ platform = $ this ->getDefaultPlatform ();
151
153
}
152
154
153
- if (in_array ( $ locale , $ this -> languages )) {
154
- if ( $ force or ! isset ($ this ->strings [ $ locale ])) {
155
- $ client = $ this -> getClient ();
155
+ if (is_null ( $ check )) {
156
+ $ check = ! is_null ($ this ->getUpdateAfter ());
157
+ }
156
158
157
- $ res = $ client ->request ('GET ' , $ this ->getApiUrl ().'strings?platform= ' .$ platform .'&language= ' .$ locale , [
158
- 'auth ' => $ this ->getAuthentication (),
159
- ]);
159
+ if ($ check ) {
160
+ $ languages = Cache::get ('languagecenter.languages ' , []);
160
161
161
- if ($ res ->getStatusCode () != 200 ) {
162
- throw new ApiException ("API returned status [ {$ res ->getStatusCode ()}]. " );
163
- }
162
+ foreach ($ languages as $ language ) {
163
+ $ timestamp = Cache::get ('languagecenter.language. ' .$ language ->codename .'.timestamp ' , 0 );
164
164
165
- $ strings = json_decode ((string ) $ res ->getBody ());
166
-
167
- $ this ->strings [$ locale ] = [];
168
- foreach ($ strings as $ string ) {
169
- $ this ->strings [$ locale ][$ string ->key ] = $ string ->value ;
170
- $ this ->strings [$ string ->language ][$ string ->key ] = $ string ->value ;
165
+ if ($ language ->timestamp > $ timestamp ) {
166
+ $ this ->updateStrings ($ locale , $ platform );
171
167
}
172
168
}
173
169
}
174
- }
175
170
176
- protected function getClient ()
177
- {
178
- return new Client ();
171
+ $ this ->strings = Cache::get ('languagecenter.strings ' , []);
179
172
}
180
173
181
- protected function getApiUrl ()
174
+ public function updateLanguages ()
182
175
{
183
- return Config::get ('languagecenter.url ' );
184
- }
176
+ $ timestamp = Carbon::now ()->timestamp ;
185
177
186
- protected function getUsername ()
187
- {
188
- return Config::get ('languagecenter.username ' );
189
- }
178
+ $ client = $ this ->getClient ();
190
179
191
- protected function getPassword ()
192
- {
193
- return Config::get ('languagecenter.password ' );
194
- }
180
+ try {
181
+ $ res = $ client ->request ('GET ' , $ this ->getApiUrl () . 'languages ' , [
182
+ 'auth ' => $ this ->getAuthentication (),
183
+ 'query ' => [
184
+ 'timestamp ' => 'on ' ,
185
+ ],
186
+ ]);
195
187
196
- protected function getAuthentication ()
197
- {
198
- return [
199
- $ this ->getUsername (),
200
- $ this ->getPassword (),
201
- ];
188
+ if ($ res ->getStatusCode () != 200 ) {
189
+ throw new ApiException ("API returned status [ {$ res ->getStatusCode ()}]. " );
190
+ }
191
+
192
+ $ languages = json_decode ((string )$ res ->getBody ());
193
+
194
+ Cache::forever ('languagecenter.languages ' , $ languages );
195
+ Cache::forever ('languagecenter.timestamp ' , $ timestamp );
196
+ } catch (\Exception $ exception ) {
197
+ $ languages = Cache::get ('languagecenter.languages ' );
198
+
199
+ if (is_null ($ languages )) {
200
+ throw $ exception ; // Nothing to do, can not restore data, throw exception
201
+ }
202
+ }
202
203
}
203
204
204
- protected function loadLanguages ( )
205
+ public function updateStrings ( $ locale , $ platform = null )
205
206
{
206
- $ client = $ this ->getClient ();
207
+ try {
208
+ $ timestamp = Carbon::now ()->timestamp ;
207
209
208
- $ res = $ client -> request ( ' GET ' , $ this -> getApiUrl (). ' languages ' , [
209
- ' auth ' => $ this ->getAuthentication (),
210
- ]);
210
+ if ( is_null ( $ platform )) {
211
+ $ platform = $ this ->getDefaultPlatform ();
212
+ }
211
213
212
- if ($ res ->getStatusCode () != 200 ) {
213
- throw new ApiException ("API returned status [ {$ res ->getStatusCode ()}]. " );
214
- }
214
+ $ client = $ this ->getClient ();
215
215
216
- $ languages = json_decode ((string ) $ res ->getBody ());
216
+ $ res = $ client ->request ('GET ' , $ this ->getApiUrl () . 'strings?platform= ' . $ platform . '&language= ' . $ locale , [
217
+ 'auth ' => $ this ->getAuthentication (),
218
+ ]);
217
219
218
- foreach ($ languages as $ language ) {
219
- $ this ->languages [] = $ language ->codename ;
220
- if ($ language ->is_fallback ) {
221
- Config::set ('app.locale ' , $ language ->codename );
222
- Config::set ('app.fallback_locale ' , $ language ->codename );
223
- $ this ->locale = $ language ->codename ;
220
+ if ($ res ->getStatusCode () != 200 ) {
221
+ throw new ApiException ("API returned status [ {$ res ->getStatusCode ()}]. " );
222
+ }
223
+
224
+ $ strings = json_decode ((string )$ res ->getBody ());
225
+
226
+ if (!isset ($ this ->strings [$ locale ])) {
227
+ $ this ->strings [$ locale ] = [];
224
228
}
229
+
230
+ $ this ->strings [$ locale ][$ platform ] = [];
231
+
232
+ foreach ($ strings as $ string ) {
233
+ $ this ->strings [$ locale ][$ platform ][$ string ->key ] = $ string ->value ;
234
+ $ this ->strings [$ string ->language ][$ platform ][$ string ->key ] = $ string ->value ;
235
+ }
236
+
237
+ Cache::forever ('languagecenter.strings ' , $ this ->strings );
238
+ Cache::forever ('languagecenter.language. ' . $ locale . '.timestamp ' , $ timestamp );
239
+ } catch (\Exception $ exception ) {
240
+ // failed to update string - it's okay - we do it later
225
241
}
226
242
}
227
243
228
- protected function createString ($ key , $ string , $ platform = null , $ comment = null )
244
+ public function createString ($ key , $ string , $ platform = null , $ comment = null )
229
245
{
230
246
if ($ platform == null ) {
231
247
$ platform = $ this ->getDefaultPlatform ();
@@ -240,30 +256,103 @@ protected function createString($key, $string, $platform = null, $comment = null
240
256
$ category = str_replace (['_ ' ], [' ' ], ucfirst (substr ($ key , 0 , $ dotpos )));
241
257
$ name = str_replace (['_ ' ], [' ' ], ucfirst (substr ($ key , $ dotpos + 1 )));
242
258
243
- $ client = $ this ->getClient ();
259
+ try {
260
+ $ client = $ this ->getClient ();
261
+
262
+ $ res = $ client ->request ('POST ' , $ this ->getApiUrl () . 'string ' , [
263
+ 'auth ' => $ this ->getAuthentication (),
264
+ 'form_params ' => [
265
+ 'platform ' => $ platform ,
266
+ 'category ' => $ category ,
267
+ 'key ' => $ name ,
268
+ 'value ' => $ string ,
269
+ 'comment ' => $ comment ,
270
+ ],
271
+ ]);
272
+
273
+ if ($ res ->getStatusCode () != 200 ) {
274
+ throw new ApiException ("API returned status [ {$ res ->getStatusCode ()}]. " );
275
+ }
276
+ } catch (\Exception $ exception ) {
277
+ // failed to create string, will just try later.
278
+ }
279
+
280
+ foreach ($ this ->strings as $ locale => $ platforms ) {
281
+ $ this ->strings [$ locale ][$ platform ][$ key ] = $ string ;
282
+ }
283
+
284
+ Cache::forever ('languagecenter.strings ' , $ this ->strings );
285
+ }
286
+
287
+ /**
288
+ * Retrieve a language line out the loaded array.
289
+ *
290
+ * @param string $namespace
291
+ * @param string $group
292
+ * @param string $locale
293
+ * @param string $item
294
+ * @param array $replace
295
+ *
296
+ * @return string|array|null
297
+ */
298
+ protected function getLine ($ namespace , $ group , $ locale , $ item , array $ replace , $ platform = null )
299
+ {
300
+ if ($ platform == null ) {
301
+ $ platform = $ this ->getDefaultPlatform ();
302
+ }
303
+
304
+ $ this ->loadStrings ($ locale , $ platform );
305
+
306
+ $ key = implode ('. ' , [$ group , $ item ]);
244
307
245
- $ res = $ client ->request ('POST ' , $ this ->getApiUrl ().'string ' , [
246
- 'auth ' => $ this ->getAuthentication (),
247
- 'form_params ' => [
248
- 'platform ' => $ platform ,
249
- 'category ' => $ category ,
250
- 'key ' => $ name ,
251
- 'value ' => $ string ,
252
- 'comment ' => $ comment ,
253
- ],
254
- ]);
255
-
256
- if ($ res ->getStatusCode () != 200 ) {
257
- throw new ApiException ("API returned status [ {$ res ->getStatusCode ()}]. " );
308
+ if (isset ($ this ->strings [$ locale ]) && isset ($ this ->strings [$ locale ][$ platform ]) && isset ($ this ->strings [$ locale ][$ platform ][$ key ])) {
309
+ return $ this ->makeReplacements ($ this ->strings [$ locale ][$ platform ][$ key ], $ replace );
258
310
}
259
311
260
- foreach ($ this ->strings as $ locale => $ strings ) {
261
- $ this ->strings [$ locale ][$ key ] = $ string ;
312
+ $ line = Arr::get ($ this ->loaded [$ namespace ][$ group ][$ locale ], $ item );
313
+
314
+ if (is_string ($ line )) {
315
+ return $ this ->makeReplacements ($ line , $ replace );
316
+ } elseif (is_array ($ line ) && count ($ line ) > 0 ) {
317
+ return $ line ;
262
318
}
263
319
}
264
320
321
+ protected function getClient ()
322
+ {
323
+ return new Client ();
324
+ }
325
+
326
+ protected function getApiUrl ()
327
+ {
328
+ return config ('languagecenter.url ' );
329
+ }
330
+
331
+ protected function getUsername ()
332
+ {
333
+ return config ('languagecenter.username ' );
334
+ }
335
+
336
+ protected function getPassword ()
337
+ {
338
+ return config ('languagecenter.password ' );
339
+ }
340
+
341
+ protected function getUpdateAfter ()
342
+ {
343
+ return config ('languagecenter.update_after ' , 60 );
344
+ }
345
+
265
346
protected function getDefaultPlatform ()
266
347
{
267
- return Config::get ('languagecenter.platform ' , 'web ' );
348
+ return config ('languagecenter.platform ' , 'web ' );
349
+ }
350
+
351
+ protected function getAuthentication ()
352
+ {
353
+ return [
354
+ $ this ->getUsername (),
355
+ $ this ->getPassword (),
356
+ ];
268
357
}
269
358
}
0 commit comments