-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLang.php
235 lines (205 loc) · 5.97 KB
/
Lang.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
class Lang{
const
CURRENT_AT_TOP = 1;
private static
$curLang = null,
$defLang = null,
$RIGHT_ALIGNED = array("ar");
public $id, $name, $code,
$texts = null;
private function __construct($data){
$this->id = $data["id"];
$this->code = $data["code"];
$this->name = $data["name"];
}
public function text($id)
{
if($this->texts !== null && isset($this->texts[$id])) return $this->texts[$id];
return
(
($text = App::db()->query("SELECT `text` FROM `lang` WHERE `id` = \"" . App::db()->real_escape_string($id) . "\" AND `lang` = {$this->id}")->fetch_assoc()) ?
$text["text"] :
null
);
}
public function allTexts()
{
if($this->texts !== null) return $this->texts;
$this->texts = [];
$texts = App::db()->query("SELECT `id`, `text` FROM `lang` WHERE `lang` = {$this->id} ORDER BY `id` ASC");
while($next = $texts->fetch_assoc()) $this->texts[$next["id"]] = $next["text"];
return $this->texts;
}
public static function get($id){
return
(
($lang = App::db()->query("SELECT * FROM `langs` WHERE `id` = " . ($id * 1))->fetch_assoc()) ?
new static($lang) :
null
);
}
public static function find($code)
{
return
(
($lang = App::db()->query("SELECT * FROM `langs` WHERE `code` = \"" . App::db()->real_escape_string($code) . "\"")->fetch_assoc()) ?
new static($lang) :
null
);
}
public static function getAll($flags = 0){
if($flags & static::CURRENT_AT_TOP) $order = "`id` = ".(static::getCurrentLanguage()->id)." DESC,";
$ret = [];
$langs = App::db()->query("SELECT * FROM `langs` ORDER BY $order `id` ASC ");
while($next = $langs->fetch_assoc())
$ret[] = new static($next);
return $ret;
}
public static function editTexts($texts)
{
if(!is_array($texts)) return;
foreach($texts as $id => $langs)
{
foreach($langs as $lang => $text)
{
App::db()->query("DELETE FROM `lang` WHERE `id` = '".App::db()->real_escape_string($id)."' AND `lang` = $lang");
if($text !== "")
App::db()->query("INSERT INTO `lang` (`id`, `lang`, `text`) VALUES ('".App::db()->real_escape_string($id)."', $lang, '".App::db()->real_escape_string($text)."')");
}
}
}
public static function addTexts($texts)
{
if(!is_array($texts)) return;
foreach($texts as $next)
{
if(!$next["id"]) continue;
foreach($next["langs"] as $lang => $text)
{
if($text === "") continue;
App::db()->query("INSERT INTO `lang` (`id`, `lang`, `text`) VALUES ('".App::db()->real_escape_string($next['id'])."', $lang, '".App::db()->real_escape_string($text)."')");
}
}
}
public static function getDefaultLanguage()
{
if(static::$defLang !== null) return $defLang;
return (static::$defLang = static::get(1));
}
public static function setCurLang($lang)
{
if($lang instanceof Lang) return (static::$curLang = $lang);
if(!($lang = static::get($lang))) return null;
if(!isset($_SESSION["lang"])) $_SESSION["lang"] = [];
$_SESSION["lang"]["curId"] = $lang->id;
return (static::$curLang = $lang);
}
public static function getSelectedLang()
{
if(static::$curLang !== null) return static::$curLang;
if(!isset($_SESSION["lang"])) $_SESSION["lang"] = [];
if(isset($_SESSION["lang"]["curId"]) && ($lang = static::get($_SESSION["lang"]["curId"]))) return (static::$curLang = $lang);
return null;
}
public static function getPreferedLangs()
{
static $langs = null;
if($langs !== null) return $langs;
$langs = [];
$found = [];
// Если пользователь уже выбрал язык, то он первый в приоритете
if($cur = static::getSelectedLang())
{
$found[$cur->code] = 1;
$langs[] = $cur;
}
// Далее идут языки из настроек его браузера
preg_replace_callback("/([a-z]{2,3})(-?[a-z0-9-;.=]*)?,/", function($match) use(&$langs, &$found)
{
if(isset($found[$match[1]])) return;
$found[$match[1]] = 1;
if($add = static::find($match[1])) $langs[] = $add;
}, strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"] . ","));
// В конце все остальные языки сайта, включая язык по-умолчанию, если его ещё не было
foreach(static::getAll() as $lang)
{
if(!isset($found[$lang->code]))
{
$found[$lang->code] = 1;
$langs[] = $lang;
}
}
return $langs;
}
public static function getCurrentLanguage()
{
return static::getPreferedLangs()[0];
}
public static function write($text, $ln = "html", &$outLang = null)
{
foreach(static::getPreferedLangs() as $lang)
{
if(($ret = $lang->text($text)) !== null)
{
$outLang = $lang;
if(gettype($ln) == 'object' && get_class($ln) == 'Closure') return $ln($ret);
switch($ln)
{
case "html":
return static::html($ret);
break;
case "attr":
return str_replace("\n", '\n', $ret);
break;
case "array":
return explode("\n", $ret);
break;
default:
return str_replace("\n", $ln, $ret);
}
return $ret;
}
}
App::log("Lang: '$text' wasn't found in any language!");
return "";
}
public static function file($name, &$outLang = null)
{
foreach(static::getPreferedLangs() as $lang)
{
if(is_file($ret = str_replace("%lang", $lang->code, $name)))
{
$outLang = $lang;
return $ret;
}
}
App::log("Lang: file '$name' wasn't found in any language!");
return $name;
}
public static function isRightAligned(Lang $lang)
{
return in_array($lang->code, static::$RIGHT_ALIGNED);
}
public static function dir(Lang $lang = null)
{
if(!($lang instanceof Lang)) $lang = Lang::getCurrentLanguage();
return (static::isRightAligned($lang) ? "rtl" : "ltr");
}
public static function html($text)
{
// теги
$text = preg_replace_callback('/\[(\/?)([A-z _\-]*)\]/', function($m){
switch($m[2])
{
default:
if($m[1] == '/') return "</span>";
return "<span class='$m[2]'>";
}
}, $text);
// Перенос строки
$text = str_replace("\n", '<br />', $text);
return $text;
}
}
?>