-
Notifications
You must be signed in to change notification settings - Fork 1
/
LanguageManager.php
140 lines (123 loc) · 3.65 KB
/
LanguageManager.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
<?php
namespace App\Core\Framework\Classes;
use JsonException;
use App\Core\Server\Session;
use App\Core\Exceptions\LangException;
use App\Core\Server\Logger;
use App\Core\Framework\Abstracts\SingletonInstance;
use App\Core\Server\Router;
/**
* LanguageManager manages the language data for the application.
*/
class LanguageManager extends SingletonInstance
{
/**
* @var bool $usingDefaultLang Indicates whether the default language is being used.
*/
private $usingDefaultLang = false;
/**
* @var string $language The current language being used.
*/
private $language;
/**
* @var array $languageData The language data for the current language.
*/
private $languageData;
/**
* @var array $defaultLanguageData The language data for the default language.
*/
private $defaultLanguageData;
/**
* Constructs a new LanguageManager instance.
*
* @param string|null $language The language to use. If null, the language will be detected or retrieved from the session.
*/
function __construct($language = null)
{
if ($language === null) {
if (Session::isset('language')) {
$language = Session::get('language');
} else {
$language = $this->detectLanguage();
}
}
$this->setLanguage($language);
$this->loadDefaultLanguageData();
}
/**
* Sets the current language.
*
* @param string $language The language to set.
*/
public function setLanguage($language)
{
$this->language = $language;
Session::set('language', $this->language);
$this->loadLanguageData($this->language);
}
/**
* Loads the language data for the current language.
*/
private function loadLanguageData()
{
$filePath = "App/Langs/{$this->language}.json";
if (!file_exists($filePath)) {
$this->usingDefaultLang = true;
$this->loadDefaultLanguageData();
return;
}
$json = file_get_contents($filePath);
$this->languageData = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
Logger::LogError(self::class, "Error decoding language file \"{$this->language}\": " . json_last_error_msg() . ". On {$filePath}. Loading default language file.");
$this->usingDefaultLang = true;
$this->loadDefaultLanguageData();
}
}
/**
* Loads the language data for the default language.
*
* @throws LangException If the default language file does not exist.
*/
private function loadDefaultLanguageData()
{
$filePath = "App/Langs/default.json";
if (!file_exists($filePath)) {
throw new LangException("Language file \"default\" does not exist.");
}
$json = file_get_contents($filePath);
$this->defaultLanguageData = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JsonException('Error decoding language file: ' . json_last_error_msg());
}
}
/**
* Retrieves the language data for the specified key.
*
* @param string $key The key to retrieve the language data for.
* @return mixed|null The language data for the key, or null if the key does not exist.
*/
public function get($key)
{
if (isset($this->languageData[$key]) && !$this->usingDefaultLang) {
return $this->languageData[$key];
} elseif (isset($this->defaultLanguageData[$key])) {
if (!$this->usingDefaultLang) {
Logger::LogWarning(self::class, "The language key '{$key}' does not exist in lang file '{$this->language}.json'. Using default lang file.");
}
return $this->defaultLanguageData[$key];
} else {
Logger::LogWarning(self::class, "The language key '{$key}' does not exist in default lang file.");
return null;
}
}
/**
* Detects the user's language.
*
* @return string The detected language.
*/
private function detectLanguage()
{
return Router::getUserLanguage();
}
}