1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Translation handler for cached translations in JSON Format |
5
|
|
|
*/ |
6
|
|
|
namespace Hokan22\LaravelTranslator\Handler; |
7
|
|
|
|
8
|
|
|
use Hokan22\LaravelTranslator\Models\TranslationIdentifier; |
9
|
|
|
use Symfony\Component\Translation\Exception\NotFoundResourceException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class LocaleHandler |
13
|
|
|
* |
14
|
|
|
* @package Hokan22\LaravelTranslator\Handler |
15
|
|
|
* @author Alexander Viertel <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
17
|
|
|
*/ |
18
|
|
|
class DatabaseHandler extends DefaultHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* DatabaseHandler constructor. |
22
|
|
|
* |
23
|
|
|
* @param string $locale The locale of the translations |
24
|
|
|
*/ |
25
|
|
|
public function __construct($locale) { |
26
|
|
|
parent::__construct($locale); |
27
|
|
|
|
28
|
|
|
$this->refreshCache(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the translation for the given identifier |
33
|
|
|
* |
34
|
|
|
* @param string $identifier Identifier of the translation |
35
|
|
|
* @param string $group Group for the database query |
36
|
|
|
* @throws NotFoundResourceException |
37
|
|
|
* @throws TranslationNotFoundException |
38
|
|
|
* @return string returns the found translation for identifier |
39
|
|
|
*/ |
40
|
|
|
public function getTranslation($identifier, $group) { |
41
|
|
|
if (isset($this->translations[$identifier])) { |
42
|
|
|
if ($this->translations[$identifier]->translation == null) { |
43
|
|
|
throw new TranslationNotFoundException("The translation for identifier '" . $identifier . "' and locale '" . $this->locale . "' could not be found"); |
44
|
|
|
} |
45
|
|
|
return $this->translations[$identifier]->translation; |
46
|
|
|
} else { |
47
|
|
|
throw new NotFoundResourceException("The translation identifier '" . $identifier . "' could not be found"); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Refresh the internal Cache |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function refreshCache() { |
57
|
|
|
$translations = new TranslationIdentifier(); |
58
|
|
|
$translations = $translations->leftJoin('translations', function($join) { |
59
|
|
|
$join->on('translation_identifiers.id', '=', 'translations.translation_identifier_id') |
60
|
|
|
->where('locale', $this->locale); |
61
|
|
|
} |
62
|
|
|
)->get(); |
63
|
|
|
|
64
|
|
|
foreach ($translations as $identifier) { |
65
|
|
|
$this->translations[$identifier->identifier] = $identifier; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all translation of $group |
71
|
|
|
* |
72
|
|
|
* @param string $group Group of the translations to return |
73
|
|
|
* @return array Translations of the given group |
74
|
|
|
*/ |
75
|
|
|
public function getAllTranslations($group = 'default') { |
76
|
|
|
$return = []; |
77
|
|
|
foreach (collect($this->translations)->where('group', $group) as $key => $translation) { |
78
|
|
|
if ($translation->translation == null) { |
79
|
|
|
$return[$key] = $translation->identifier; |
80
|
|
|
} else { |
81
|
|
|
$return[$key] = $translation->translation; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the DB ID of the Identifier |
89
|
|
|
* |
90
|
|
|
* @param $identifier |
91
|
|
|
* @throws NotFoundResourceException |
92
|
|
|
* @return integer |
93
|
|
|
*/ |
94
|
|
|
public function getDatabaseID($identifier) { |
95
|
|
|
if (isset($this->translations[$identifier])) { |
96
|
|
|
return $this->translations[$identifier]->id; |
97
|
|
|
} else { |
98
|
|
|
throw new NotFoundResourceException("The translation identifier '" . $identifier . "' could not be found"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |