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 string[] Translations (Key => Translation) |
40
|
|
|
*/ |
41
|
|
|
protected $translations = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string Language name |
45
|
|
|
*/ |
46
|
|
|
protected $language; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $translations Translations (Key => Translation) |
50
|
|
|
* @param string $language Language name |
51
|
|
|
*/ |
52
|
20 |
|
public function __construct(array $translations, string $language) { |
53
|
20 |
|
foreach ($translations as $key => $value) { |
54
|
14 |
|
if (is_string($key) && is_string($value)) { |
55
|
14 |
|
$this->translations[$key] = $value; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
20 |
|
$this->language = $language; |
60
|
20 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns language name |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
10 |
|
public function getLanguage(): string { |
68
|
10 |
|
return $this->language; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all translations |
73
|
|
|
* |
74
|
|
|
* @return string[] Translations (Key => Translation) |
75
|
|
|
*/ |
76
|
8 |
|
public function getTranslations(): array { |
77
|
8 |
|
return $this->translations; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Checks translation for key exists |
82
|
|
|
* |
83
|
|
|
* @param string $key Key |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
14 |
|
public function supports(string $key): bool { |
88
|
14 |
|
return isset($this->translations[$key]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Translates key |
93
|
|
|
* |
94
|
|
|
* @param string $key Key |
95
|
|
|
* |
96
|
|
|
* @return string Translation or key if is not exists |
97
|
|
|
*/ |
98
|
12 |
|
public function translate(string $key) { |
99
|
12 |
|
if (!$this->supports($key)) { |
100
|
8 |
|
return $key; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
return $this->translations[$key]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Makes Translator instance from config |
108
|
|
|
* |
109
|
|
|
* @param array $config Configuration array |
110
|
|
|
* @param string $language Language name |
111
|
|
|
* |
112
|
|
|
* @return static Made instance |
113
|
|
|
*/ |
114
|
2 |
|
public static function fromConfig(array $config, string $language): Translator { |
115
|
2 |
|
$translations = (new Processor())-> |
116
|
2 |
|
processConfiguration(new TranslatorConfiguration(), [$config]); |
117
|
|
|
|
118
|
2 |
|
return new static($translations, $language); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|