|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Translation handler for cached translations in JSON Format |
|
5
|
|
|
*/ |
|
|
|
|
|
|
6
|
|
|
namespace Hokan22\LaravelTranslator\Handler; |
|
7
|
|
|
|
|
8
|
|
|
use Hokan22\LaravelTranslator\TranslatorFacade; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class CacheJSONHandler |
|
12
|
|
|
* |
|
13
|
|
|
* @package Hokan22\LaravelTranslator\Handler |
|
|
|
|
|
|
14
|
|
|
* @author Alexander Viertel <[email protected]> |
|
|
|
|
|
|
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
|
|
|
|
|
16
|
|
|
*/ |
|
|
|
|
|
|
17
|
|
|
class CacheJSONHandler extends DefaultHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* DatabaseHandler constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $locale The locale of the translations |
|
|
|
|
|
|
23
|
|
|
* @throws TranslationCacheNotFound |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($locale) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($locale); |
|
28
|
|
|
|
|
29
|
|
|
$this->refreshCache(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Returns the translation for the given identifier |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $identifier Identifier for the database query |
|
|
|
|
|
|
36
|
|
|
* @param string $group |
|
|
|
|
|
|
37
|
|
|
* @throws TranslationNotInCacheException |
|
|
|
|
|
|
38
|
|
|
* @return string returns the found translation for locale and identifier |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
public function getTranslation($identifier, $group = 'default') |
|
41
|
|
|
{ |
|
42
|
|
|
// NOTE: This should never trigger the addition of the identifier to the database! |
|
43
|
|
|
// Because the cache will not be updated automatically. |
|
44
|
|
|
// the same identifier will not be found twice in the cache, which will result in a duplicate key sql error. |
|
45
|
|
|
if (isset($this->translations[$group][$identifier])) { |
|
46
|
|
|
return $this->translations[$group][$identifier]; |
|
47
|
|
|
} |
|
48
|
|
|
else { |
|
|
|
|
|
|
49
|
|
|
throw new TranslationNotInCacheException("The translation identifier '".$identifier."' could not be found in Cache"); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Refresh the internal Cache |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $group |
|
|
|
|
|
|
57
|
|
|
* @throws TranslationCacheNotFound |
|
|
|
|
|
|
58
|
|
|
*/ |
|
|
|
|
|
|
59
|
|
|
public function refreshCache($group = 'default') |
|
60
|
|
|
{ |
|
61
|
|
|
$locale_dir = TranslatorFacade::getConfigValue('cache_path').$this->locale; |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$trans_identifier = json_decode(file_get_contents($locale_dir.'/'.$group.'.json'), true); |
|
65
|
|
|
} catch (\ErrorException $e) { |
|
66
|
|
|
throw new TranslationCacheNotFound("The Translation cache file '".$locale_dir.'/'.$group.'.json'."' could not be found!"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->translations[$group] = $trans_identifier; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get all translation of $group |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $group Group of the translations to return |
|
|
|
|
|
|
76
|
|
|
* @return array|mixed Translations of the given group |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function getAllTranslations($group) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->translations[$group]; |
|
81
|
|
|
} |
|
82
|
|
|
} |