1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* MIT License |
4
|
|
|
|
5
|
|
|
Copyright (c) 2018 Eridan Domoratskiy |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
15
|
|
|
copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
SOFTWARE. */ |
24
|
|
|
|
25
|
|
|
namespace MaintenanceScreen; |
26
|
|
|
|
27
|
|
|
use Symfony\Component\Config\Definition\Processor; |
28
|
|
|
|
29
|
|
|
use MaintenanceScreen\Configurations\TranslatorConfiguration; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Translates to some language |
33
|
|
|
* |
34
|
|
|
* @author ProgMiner |
35
|
|
|
*/ |
36
|
|
|
class Translator { |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array(string => string) Translations |
40
|
|
|
*/ |
41
|
|
|
protected $translations; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string Language name |
45
|
|
|
*/ |
46
|
|
|
protected $language; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $translations Array with translations |
50
|
|
|
* @param string $language Language name |
51
|
|
|
*/ |
52
|
|
|
public function __construct(array $translations, string $language) { |
53
|
|
|
$this->translations = $translations; |
54
|
|
|
$this->language = $language; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns language name |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getLanguage(): string { |
63
|
|
|
return $this->language; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns all translations |
68
|
|
|
* |
69
|
|
|
* @return array(string => string) |
70
|
|
|
*/ |
71
|
|
|
public function getTranslations(): array { |
72
|
|
|
return $this->translations; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Checks translation for key exists |
77
|
|
|
* |
78
|
|
|
* @param string $key Key |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function supports(string $key): bool { |
83
|
|
|
return isset($this->translations[$key]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Translates key |
88
|
|
|
* |
89
|
|
|
* @param string $key Key |
90
|
|
|
* |
91
|
|
|
* @return string Translation or key if is not exists |
92
|
|
|
*/ |
93
|
|
|
public function translate(string $key) { |
94
|
|
|
if (!$this->supports($key)) { |
95
|
|
|
return $key; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->translations[$key]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Makes Translator instance from config |
103
|
|
|
* |
104
|
|
|
* @param array $config Config |
105
|
|
|
* @param string $language Language name |
106
|
|
|
* |
107
|
|
|
* @return static Made instance |
108
|
|
|
*/ |
109
|
|
|
public static function fromConfig(array $config, string $language): Translator { |
110
|
|
|
$translations = (new Processor())-> |
111
|
|
|
processConfiguration(new TranslatorConfiguration(), [$config]); |
112
|
|
|
|
113
|
|
|
return new static($translations, $language); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|