-
Notifications
You must be signed in to change notification settings - Fork 7
/
Translator.php
154 lines (133 loc) · 3.89 KB
/
Translator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Aternos\Codex\Minecraft\Translator;
use InvalidArgumentException;
/**
* Class Translator
*
* @package Aternos\Codex\Minecraft\Translator
*/
class Translator
{
protected const DEFAULT_LANGUAGE = "en";
protected static ?Translator $instance = null;
protected static ?Translator $fallbackInstance = null;
/**
* @return static
*/
public static function getInstance(): Translator
{
if (!static::$instance) {
static::$instance = new Translator();
}
return static::$instance;
}
/**
* @return static
*/
public static function getFallbackInstance(): Translator
{
if (!static::$fallbackInstance) {
static::$fallbackInstance = (new Translator())->setLanguage(static::DEFAULT_LANGUAGE);
}
return static::$fallbackInstance;
}
protected string $language = self::DEFAULT_LANGUAGE;
/**
* @var string[]
*/
protected array $translations = [];
/**
* Get the current language
*
* @return string
*/
public function getLanguage(): string
{
return $this->language;
}
/**
* Set the current language
*
* @param string $language
* @return $this
*/
public function setLanguage(string $language = self::DEFAULT_LANGUAGE): static
{
if (!$this->checkTranslationFile($language)) {
throw new InvalidArgumentException("Language file for language '" . $language . "' not found.");
}
$this->language = $language;
return $this;
}
/**
* Get a translation string
*
* The string might contain {{placeholders}} that can be
* replaced through $replacements = ["placeholder" => "value"]
*
* @param string $variable
* @param array $replacements
* @return string
*/
public function getTranslation(string $variable, array $replacements = []): string
{
$translations = $this->loadTranslations();
if (!isset($translations[$variable])) {
if ($this->getLanguage() !== static::DEFAULT_LANGUAGE) {
return static::getFallbackInstance()->getTranslation($variable, $replacements);
}
throw new InvalidArgumentException("Translation variable '" . $variable . "' not found.");
}
$value = $translations[$variable];
if ($replacements) {
foreach ($replacements as $placeholder => $replacement) {
$value = str_replace("{{" . $placeholder . "}}", $replacement, $value);
}
}
return $value;
}
/**
* Check if the translation file exists
*
* @param string $language
* @return bool
*/
protected function checkTranslationFile(string $language): bool
{
return file_exists($this->getTranslationFile($language));
}
/**
* Get the path to a translation file
*
* @param string $language
* @return string
*/
protected function getTranslationFile(string $language): string
{
return __DIR__ . "/../../lang/" . $language . ".json";
}
/**
* Load translations from translation file for current language
*
* @return string[]
*/
protected function loadTranslations(): array
{
if (!isset($this->translations[$this->language])) {
$file = $this->getTranslationFile($this->language);
$content = file_get_contents($file);
$translations = json_decode($content, true);
if (!is_array($translations)) {
throw new InvalidArgumentException("Could not parse JSON translation file: " . $file);
}
$this->translations[$this->language] = $translations;
}
return $this->translations[$this->language];
}
protected function __construct()
{
}
protected function __clone()
{
}
}