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
|
|
|
{ |
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 of the translation |
36
|
|
|
* @param string $group Group for the database query |
37
|
|
|
* @throws NotFoundResourceException |
38
|
|
|
* @throws TranslationNotFoundException |
39
|
|
|
* @return string returns the found translation for identifier |
40
|
|
|
*/ |
41
|
|
|
public function getTranslation($identifier, $group) |
42
|
|
|
{ |
43
|
|
|
if (isset($this->translations[$identifier])) { |
44
|
|
|
if ($this->translations[$identifier]->translation == null) { |
45
|
|
|
throw new TranslationNotFoundException("The translation for identifier '".$identifier."' and locale '".$this->locale."' could not be found"); |
46
|
|
|
} |
47
|
|
|
return $this->translations[$identifier]->translation; |
48
|
|
|
} else { |
49
|
|
|
throw new NotFoundResourceException("The translation identifier '".$identifier."' could not be found"); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Refresh the internal Cache |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function refreshCache() |
59
|
|
|
{ |
60
|
|
|
$translations = new TranslationIdentifier(); |
61
|
|
|
$translations = $translations->leftJoin('translations', function ($join) |
62
|
|
|
{ |
63
|
|
|
$join->on( 'translation_identifiers.id', '=', 'translations.translation_identifier_id') |
64
|
|
|
->where('locale', $this->locale); |
65
|
|
|
} |
66
|
|
|
)->get(); |
67
|
|
|
|
68
|
|
|
foreach ($translations as $identifier) { |
69
|
|
|
$this->translations[$identifier->identifier] = $identifier; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get all translation of $group |
75
|
|
|
* |
76
|
|
|
* @param string $group Group of the translations to return |
77
|
|
|
* @return array Translations of the given group |
78
|
|
|
*/ |
79
|
|
|
public function getAllTranslations($group = 'default') |
80
|
|
|
{ |
81
|
|
|
$return = []; |
82
|
|
|
foreach (collect($this->translations)->where('group', $group) as $key => $translation) { |
83
|
|
|
if ($translation->translation == null) { |
84
|
|
|
$return[$key] = $translation->identifier; |
85
|
|
|
} else { |
86
|
|
|
$return[$key] = $translation->translation; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return $return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the DB ID of the Identifier |
94
|
|
|
* |
95
|
|
|
* @param $identifier |
96
|
|
|
* @throws NotFoundResourceException |
97
|
|
|
* @return integer |
98
|
|
|
*/ |
99
|
|
|
function getDatabaseID($identifier) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if(isset($this->translations[$identifier])) { |
102
|
|
|
return $this->translations[$identifier]->id; |
103
|
|
|
} else { |
104
|
|
|
throw new NotFoundResourceException("The translation identifier '".$identifier."' could not be found"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.