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
|
<?php
// define locations here
define('LANGDIR', './localization/'); // langdir location
define('LABELS', 'labels.inc'); // name of the labels file
define('MESSAGES', 'messages.inc'); // name of the messages file
define('ORIGINAL', 'en_US'); // always up-to-date language
// ---- EOF conf ---- //
function get_input_value($fname, $html = false)
{
$value = !empty($_REQUEST[$fname]) ? $_REQUEST[$fname] : "";
// strip slashes if magic_quotes enabled
if ((bool)get_magic_quotes_gpc())
$value = stripslashes($value);
// remove HTML tags if not allowed
if (!$html)
$value = strip_tags($value);
return $value;
}
function update_from_svn($lang, $file)
{
$host = "svn.roundcube.net";
$base = "/trunk/roundcubemail/program/localization/";
$lang_dir = $lang != "" ? $lang."/" : "";
$lang_prefix = $lang != "" ? $lang."_" : "";
// check if original file is up to date
$stat = @stat(LANGDIR."$lang_prefix$file");
if (!$stat || ($stat['mtime'] < time() - 3600))
{
if ($fp = fsockopen("ssl://$host", 443, $err, $err_str))
fwrite($fp, "GET $base$lang_dir$file HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n");
$headers = true;
if ($fp && !$err && ($fl = @fopen(LANGDIR."$lang_prefix$file", 'w')))
{
echo '<!-- Update from SVN: ' . $lang_dir.$file . "-->\n";
while (!feof($fp))
{
$line = fgets($fp, 4096);
if (trim($line) == "")
$headers = false;
if (!$headers)
fwrite($fl, $line);
}
fclose($fp);
fclose($fl);
}
}
if (is_file(LANGDIR."$lang_prefix$file"))
return LANGDIR."$lang_prefix$file";
else
return false;
}
function lang_selection($lang)
{
include(update_from_svn('', 'index.inc'));
$out = "<select name=\"lang\">\n<option value=\"_NEW_\">New Language</option>\n";
foreach ((array)$rcube_languages as $l_key => $l_value)
{
if ($l_key == ORIGINAL)
continue;
$out .= '<option value="'.$l_key.'"';
if ($l_key == $lang) $out .= ' selected';
$out .= '>' . htmlspecialchars($l_value) . "</option>\n";
}
$out .= "</select>";
return $out;
}
function build_localization($lang, $file)
{
global $orig_values, $edit_values;
$array = $file == "messages.inc" ? '$messages' : '$labels';
$fpath = $lang != "_NEW_" ? update_from_svn($lang, $file) : 'nope';
if (!strstr($fpath, 'http') && !is_file($fpath))
$fpath = realpath("./$file");
$out = "";
if ($fpath && ($fp = fopen($fpath, 'r')))
{
while (!feof($fp))
{
$line = trim(fgets($fp));
if (empty($line) && empty($out))
continue;
$out .= str_replace('#lang', $lang, $line) . "\n";
// reached begin of php array
if (strstr($line, $array))
break;
}
}
else
{
$out .= '<?php' . "\n";
$out .= $array . " = array();\n";
}
foreach((array)$orig_values as $t_key => $t_value)
{
$t_value = get_input_value('t_'.$t_key, true);
if (empty($t_value) && isset($edit_values[$t_key]))
$t_value = $edit_values[$t_key];
if (!empty($t_value))
$out .= $array . "['$t_key'] = '" . addcslashes($t_value, "'") . "';\n";
}
$out .= "\n?>\n";
return $out;
}
function count_lines($lang, $file)
{
$count = 0;
$lines = array();
if ($filename = update_from_svn($lang, $file))
$lines = file($filename);
// count lines starting with $ ($labels/$messages)
foreach($lines as $line)
if(strpos($line, '$') === 0)
$count++;
return $count;
}
function localization_stats($force = false)
{
$cachefile = LANGDIR . 'langstats.txt';
// use saved file (cache)
if (!$force && file_exists($cachefile))
if (filemtime($cachefile) + 60*60*12 > time())
return file_get_contents($cachefile);
$us_count = count_lines(ORIGINAL, LABELS);
$us_count += count_lines(ORIGINAL, MESSAGES);
include(LANGDIR . 'index.inc');
$i = 0;
foreach ((array)$rcube_languages as $l_key => $l_value)
{
if ($l_key == ORIGINAL)
continue;
$count = count_lines($l_key, LABELS);
$count += count_lines($l_key, MESSAGES);
$percent = ($count * 100) / $us_count;
$rows[] = sprintf("<tr><td class=\"lang%s\">%s</td><td class=\"percent%s\">%.1f %%</td></tr>\n",
($i%2 ? ' zebra' : ''), htmlspecialchars($l_value), ($i%2 ? ' zebra' : ''), $percent);
$i++;
}
// format output tables
$result = '<table class="stats"><tr><td><table class="langstats">';
for($i=0; $i<count($rows)/2; $i++)
$result .= $rows[$i];
$result .= '</table></td><td><table class="langstats">';
for(; $i<count($rows); $i++)
$result .= $rows[$i];
$result .= '</td></tr></table>';
// save to cache file
file_put_contents($cachefile, $result);
return $result;
}
// -------- EOF func --------//
$header = array();
$orig_values = array();
$labels = $messages = null;
$file = get_input_value('file');
$lang = get_input_value('lang');
$translated = !empty($_REQUEST['trans']);
// read reference file
if ($file && $lang)
include(update_from_svn(ORIGINAL, $file));
if ($file == 'labels.inc' && $labels)
$orig_values = $labels;
else if ($file == 'messages.inc' && $messages)
$orig_values = $messages;
unset($labels, $messages);
// read current localization file
if (!empty($lang) && !empty($file))
{
if ($lang != "_NEW_")
@include(update_from_svn($lang, $file));
if (!empty($labels))
$edit_values = $labels;
else if (!empty($messages))
$edit_values = $messages;
else
$edit_values = array();
unset($labels, $messages);
}
?>
|