-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlanguage.php
102 lines (100 loc) · 3.55 KB
/
language.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
<?php
// นำเข้าภาษา
$dir = ROOT_PATH.'language/';
if (is_dir(ROOT_PATH.'language/')) {
// ตาราง language
$table = $db_config['prefix'].'_language';
// อ่านไฟล์ภาษาที่ติดตั้ง
$f = opendir($dir);
if ($f) {
while (false !== ($text = readdir($f))) {
if (preg_match('/^([a-z]{2,2})\.(php|js)$/', $text, $match)) {
if ($db->fieldExists($table, $match[1]) == false) {
// เพิ่มคอลัมน์ภาษา ถ้ายังไม่มีภาษาที่ต้องการ
$db->query("ALTER TABLE `$table` ADD `$match[1]` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci AFTER `en`");
}
if ($match[2] == 'php') {
importPHP($db, $table, $match[1], $dir.$text);
} else {
importJS($db, $table, $match[1], $dir.$text);
}
}
}
closedir($f);
}
$content[] = '<li class="correct">นำเข้า `'.$table.'` สำเร็จ</li>';
}
/**
* นำเข้าข้อมูลไฟล์ภาษา PHP
*
* @param Db $db Database Class
* @param string $table ชื่อตาราง language
* @param string $lang ชื่อภาษา
* @param string $file_name ไฟล์ภาษา
*/
function importPHP($db, $table, $lang, $file_name)
{
foreach (include ($file_name) as $key => $value) {
if (is_array($value)) {
$type = 'array';
} elseif (is_int($value)) {
$type = 'int';
} else {
$type = 'text';
}
$search = $db->first($table, ['key' => $key, 'js' => 0]);
if ($type == 'array') {
$value = serialize($value);
}
if ($search) {
$db->update($table, [
'id' => $search->id
], [
$lang => $value
]);
} else {
$db->insert($table, [
'key' => $key,
'js' => 0,
'type' => $type,
'owner' => 'index',
$lang => $value
]);
}
}
}
/**
* นำเข้าข้อมูลไฟล์ภาษา Javascript
*
* @param Database $db Database Object
* @param string $table ชื่อตาราง language
* @param string $lang ชื่อภาษา
* @param string $file_name ไฟล์ภาษา
*/
function importJS($db, $table, $lang, $file_name)
{
$patt = '/^var[\s]+([A-Z0-9_]+)[\s]{0,}=[\s]{0,}[\'"](.*)[\'"];$/';
foreach (file($file_name) as $item) {
$item = trim($item);
if ($item != '') {
if (preg_match($patt, $item, $match)) {
$search = $db->first($table, ['key' => $match[1], 'js' => 1]);
if ($search) {
$db->update($table, [
'id' => $search->id
], [
$lang => $match[2]
]);
} else {
$db->insert($table, [
'key' => $match[1],
'js' => 1,
'type' => 'text',
'owner' => 'index',
$lang => $match[2]
]);
}
}
}
}
}