-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI18nSupport.php
210 lines (164 loc) · 4.99 KB
/
I18nSupport.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?
/**
* PHP i18n Support - JSON data
*
* Based on sections (HTML semantic sections and content),
* and lang attribute (query string)
*
* Structured JSON and flat JSON structure support
*
* Author: Matej Lednár
*/
mb_internal_encoding('UTF-8');
class I18n {
private $translation = "";
private $lang = "en";
private $translationPath = "translation/i18n.json";
private $languages = [
'en',
'sk',
'de'];
function __construct($url = "") {
if ($url == "") {
$url = $this->translationPath;
}
$this->loadTranslation($url);
$this->getLanguage();
}
/**
* Detect and set language
*/
public function getLanguage() {
$lang = filter_input(INPUT_GET, "lang");
if (!isset($lang)) {
$lang = substr(filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE'), 0, 2);
}
if (!in_array($lang, $this->languages)) {
$lang = $this->$lang;
}
$this->lang = $lang;
}
/**
* Load JSON file with translation
*
* @param String $path
*/
public function loadTranslation($path) {
// Read JSON file
if (file_exists($path)) {
$json = file_get_contents($path);
} else {
$json = "{}";
}
//Decode JSON
$this->translation = json_decode($json, true);
}
/**
*
* @param String $section - section name for better JSON structure
* @param String $key - key name for translated text
* @return String
*/
public function getText($section = "", $key = "") {
$lang = $this->lang;
if ($key != "") {
$test = array_key_exists($section, $this->translation);
if (!$test) {
return ""; // if section doesn't exist, return empty string
}
$translation = $this->translation[$section];
$translation = $translation[$key];
$translation = $translation[$lang];
} else {
$key = $section;
$test = array_key_exists($key, $this->translation);
if (!$test) {
return ""; // if key doesn't exist, return empty string
}
$translation = $this->translation[$key];
$translation = $translation[$lang];
}
return $translation;
}
/**
* Show translated text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function showText($section = "", $key = "") {
echo $this->getText($section, $key);
}
/**
* Show translated uppercase text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function showUText($section = "", $key = "") {
echo mb_strtoupper($this->getText($section, $key));
}
/**
* Show translated lowercase text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function showLText($section = "", $key = "") {
echo mb_strtolower($this->getText($section, $key));
}
/**
* Show translated first character uppercase text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function showUFText($section = "", $key = "") {
echo ucfirst($this->getText($section, $key));
}
/**
* Get translated uppercase text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function getUText($section = "", $key = "") {
return mb_strtoupper($this->getText($section, $key));
}
/**
* Get translated lowercase text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function getLText($section = "", $key = "") {
return mb_strtolower($this->getText($section, $key));
}
/**
* Get translated first character uppercase text (key) from defined category (section)
*
* @param String $section
* @param String $key
*/
public function getUFText($section = "", $key = "") {
return ucfirst($this->getText($section, $key));
}
public function setDefaultLanguage($lang) {
$this->lang = $lang;
}
public function getDefaultLanguage() {
return $this->lang;
}
public function setSupportedLanguages($languages) {
$this->languages = $languages;
}
public function getSupportedLanguages() {
return $this->languages;
}
public function isSupported($lang) {
if (in_array($lang, $this->languages)) {
return true;
}
return false;
}
}