-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathqhm_message.php
More file actions
126 lines (103 loc) · 2.44 KB
/
Copy pathqhm_message.php
File metadata and controls
126 lines (103 loc) · 2.44 KB
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
<?php
/**
* QHM Message Class
* -------------------------------------------
* qhm_massage.php
*
* Copyright (c) 2010 hokuken
* http://hokuken.com/
*
* created : 2010-09-15
* modified :
*
* save system messages for localization
*
* Usage :
*
*/
class QHM_Message {
// Singleton Start: ------------------------------------------
private static $instance;
public static function get_instance() {
if (isset( self::$instance )) {
return self::$instance;
} else {
self::$instance = new QHM_Message();
return self::$instance;
}
}
// Singleton End: --------------------------------------------
//messages
var $m;
var $file;
var $file_ja;
var $cache;
var $locales;
private function QHM_Message() {
$this->m = array();
$this->file = 'lng.'. LANG. '.txt';
$this->file_ja = 'lng.ja.txt';
$this->cache = CACHE_DIR. '/lng.'. LANG. '.qmc';
$this->readCache();
}
function replace() {
$args = func_get_args();
$name = array_shift($args);
if (strpos($name, '.')) {
list($section, $name) = explode('.', $name);
$str = $this->m[$section][$name];
} else {
$str = $this->m[$name];
}
$srcs = array('$1', '$2', '$3', '$4', '$5');
$args = array_pad($args, 5, '');
return str_replace($srcs, $args, $str);
}
function readCache() {
if ($this->checkCache()) {
$this->m = unserialize(file_get_contents($this->cache));
}
}
function checkCache() {
//cache OK
if (file_exists($this->cache) && (filemtime($this->cache) > filemtime($this->file))) {
return true;
}
//make cache
else {
$this->buildCache();
return false;
}
}
function buildCache() {
$ini = parse_ini_file($this->file, true);
if (LANG != 'ja') {
$ini_ja = parse_ini_file($this->file_ja, true);
foreach ($ini_ja as $key => $value) {
if (is_array($value)) {
$ini[$key] = array_merge($value, $ini[$key]);
} else {
$ini[$key] = isset($ini[$key])? $ini[$key]: $value;
}
}
}
//" を" へ変換する
//##LF## を\n へ変換する
$src = array('"', '##LF##');
$rpl = array('"', "\n");
$ini = str_replace($src, $rpl, $ini);
foreach ($ini as $section => $values) {
if (is_array($values)) {
$ini[$section] = str_replace($src, $rpl, $values);
}
}
$this->m = $ini;
//save cache
$str = serialize($this->m);
file_put_contents($this->cache, $str);
}
}
function get_qm() {
return QHM_Message::get_instance();
}
?>