Skip to content

Commit 1964329

Browse files
authored
Merge pull request #5 from marktopper/master
Cache the language strings
2 parents 811f929 + 2c79df3 commit 1964329

File tree

2 files changed

+187
-98
lines changed

2 files changed

+187
-98
lines changed

src/TranslationServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TranslationServiceProvider extends LaravelTranslationServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
parent::boot();
16+
//parent::boot();
1717

1818
$this->publishes([
1919
__DIR__.'/../config/languagecenter.php' => config_path('languagecenter.php'),

src/Translator.php

Lines changed: 186 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Novasa\LaravelLanguageCenter;
44

5+
use Carbon\Carbon;
56
use GuzzleHttp\Client;
67
use Illuminate\Support\Arr;
7-
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Translation\LoaderInterface;
910
use Illuminate\Translation\Translator as LaravelTranslator;
1011

@@ -26,7 +27,7 @@ public function __construct(LoaderInterface $loader, $locale)
2627
$this->loader = $loader;
2728
$this->locale = $locale;
2829

29-
// Load langauges from API
30+
// Load languages from API
3031
if (count($this->languages) == 0) {
3132
$this->loadLanguages();
3233
}
@@ -47,24 +48,30 @@ public function get($data, array $replace = [], $locale = null, $fallback = true
4748
// Make support for array data
4849
if (is_array($data)) {
4950
$key = $data['key'];
51+
5052
if (isset($data['string'])) {
5153
$string = $data['string'];
5254
}
55+
5356
if (isset($data['platform'])) {
5457
$platform = $data['platform'];
5558
}
59+
5660
if (isset($data['comment'])) {
5761
$comment = $data['comment'];
5862
}
5963
} else {
6064
$key = $data;
6165
}
66+
6267
if (!isset($string)) {
6368
$string = $key;
6469
}
70+
6571
if (!isset($platform)) {
6672
$platform = $this->getDefaultPlatform();
6773
}
74+
6875
if (!isset($comment)) {
6976
$comment = null;
7077
}
@@ -106,42 +113,37 @@ public function get($data, array $replace = [], $locale = null, $fallback = true
106113
return $line;
107114
}
108115

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)
121117
{
122-
if ($platform == null) {
123-
$platform = $this->getDefaultPlatform();
124-
}
118+
config()->set('app.locale', $locale);
119+
config()->set('fallback_locale', $locale);
125120

126-
$this->loadLanguageStrings($locale, $platform);
121+
$this->locale = $locale;
122+
}
127123

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();
131131
}
132132

133-
$line = Arr::get($this->loaded[$namespace][$group][$locale], $item);
133+
$languages = Cache::get('languagecenter.languages');
134134

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+
}
139141
}
140142
}
141143

142-
protected function loadLanguageStrings($locale, $platform = null, $force = false)
144+
public function loadStrings($locale, $platform = null, $check = null)
143145
{
144-
// Load langauges from API
146+
// Load languages from API
145147
if (count($this->languages) == 0) {
146148
$this->loadLanguages();
147149
}
@@ -150,82 +152,96 @@ protected function loadLanguageStrings($locale, $platform = null, $force = false
150152
$platform = $this->getDefaultPlatform();
151153
}
152154

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+
}
156158

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', []);
160161

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);
164164

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);
171167
}
172168
}
173169
}
174-
}
175170

176-
protected function getClient()
177-
{
178-
return new Client();
171+
$this->strings = Cache::get('languagecenter.strings', []);
179172
}
180173

181-
protected function getApiUrl()
174+
public function updateLanguages()
182175
{
183-
return Config::get('languagecenter.url');
184-
}
176+
$timestamp = Carbon::now()->timestamp;
185177

186-
protected function getUsername()
187-
{
188-
return Config::get('languagecenter.username');
189-
}
178+
$client = $this->getClient();
190179

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+
]);
195187

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+
}
202203
}
203204

204-
protected function loadLanguages()
205+
public function updateStrings($locale, $platform = null)
205206
{
206-
$client = $this->getClient();
207+
try {
208+
$timestamp = Carbon::now()->timestamp;
207209

208-
$res = $client->request('GET', $this->getApiUrl().'languages', [
209-
'auth' => $this->getAuthentication(),
210-
]);
210+
if (is_null($platform)) {
211+
$platform = $this->getDefaultPlatform();
212+
}
211213

212-
if ($res->getStatusCode() != 200) {
213-
throw new ApiException("API returned status [{$res->getStatusCode()}].");
214-
}
214+
$client = $this->getClient();
215215

216-
$languages = json_decode((string) $res->getBody());
216+
$res = $client->request('GET', $this->getApiUrl() . 'strings?platform=' . $platform . '&language=' . $locale, [
217+
'auth' => $this->getAuthentication(),
218+
]);
217219

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] = [];
224228
}
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
225241
}
226242
}
227243

228-
protected function createString($key, $string, $platform = null, $comment = null)
244+
public function createString($key, $string, $platform = null, $comment = null)
229245
{
230246
if ($platform == null) {
231247
$platform = $this->getDefaultPlatform();
@@ -240,30 +256,103 @@ protected function createString($key, $string, $platform = null, $comment = null
240256
$category = str_replace(['_'], [' '], ucfirst(substr($key, 0, $dotpos)));
241257
$name = str_replace(['_'], [' '], ucfirst(substr($key, $dotpos + 1)));
242258

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]);
244307

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);
258310
}
259311

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;
262318
}
263319
}
264320

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+
265346
protected function getDefaultPlatform()
266347
{
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+
];
268357
}
269358
}

0 commit comments

Comments
 (0)