|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* Base Handler to extend from |
|
6
|
|
|
* |
|
7
|
|
|
* @package Hokan22\LaravelTranslator\Handler |
|
8
|
|
|
* @author Alexander Viertel <[email protected]> |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Hokan22\LaravelTranslator\Handler; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Custom Exception to distinguish if the Translation Identifier or |
|
15
|
|
|
* the Translation was not found for the given locale |
|
16
|
|
|
* |
|
17
|
|
|
* Class TranslationNotFoundException |
|
18
|
|
|
*/ |
|
19
|
|
|
class TranslationNotFoundException extends \Exception {} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Custom Exception to distinguish if the Translation Identifier was not found in Cache but could be in DB |
|
23
|
|
|
* |
|
24
|
|
|
* Class TranslationNotInCacheException |
|
25
|
|
|
*/ |
|
26
|
|
|
class TranslationNotInCacheException extends TranslationNotFoundException {} |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Custom Exception thrown when a cache file could not be found |
|
30
|
|
|
* |
|
31
|
|
|
* Class TranslationCacheNotFound |
|
32
|
|
|
*/ |
|
33
|
|
|
class TranslationCacheNotFound extends \Exception {} |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Interface HandlerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
class DefaultHandler |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string $locale The locale to translate to |
|
42
|
|
|
* @var array|array[] $translations Array with the identifiers as keys and the Texts object as value |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $locale, $translations; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* HandlerInterface constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param $locale |
|
50
|
|
|
*/ |
|
51
|
|
|
function __construct($locale){ |
|
52
|
|
|
$this->locale = $locale; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Should return the locale currently set in the handler |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
function getLocale(){ |
|
|
|
|
|
|
61
|
|
|
return $this->locale; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the translation of a given identifier |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $identifier |
|
68
|
|
|
* @param string $group |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
function getTranslation($identifier, $group){ |
|
|
|
|
|
|
72
|
|
|
return $identifier; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Refresh the internal Cache |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
function refreshCache(){ |
|
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get all translations of a given group |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $group |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
function getAllTranslations($group){ |
|
|
|
|
|
|
91
|
|
|
return []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.