forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.php
305 lines (253 loc) · 11.2 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?PHP // $Id$
require_once("../config.php");
optional_variable($mode, "");
optional_variable($currentfile, "moodle.php");
require_login();
if (!isadmin()) {
error("You need to be admin to edit this page");
}
if (! $site = get_site()) {
error("Site not defined!");
}
$stradministration = get_string("administration");
$strconfiguration = get_string("configuration");
$strlanguage = get_string("language");
$strcurrentlanguage = get_string("currentlanguage");
$strmissingstrings = get_string("missingstrings");
$strcomparelanguage = get_string("comparelanguage");
$strthislanguage = get_string("thislanguage");
switch ($mode) {
case "missing":
$navigation = "<A HREF=\"lang.php\">$strlanguage</A> -> $strmissingstrings";
$title = $strmissingstrings;
break;
case "compare":
$navigation = "<A HREF=\"lang.php\">$strlanguage</A> -> $strcomparelanguage";
$title = $strcomparelanguage;
break;
default:
$title = $strlanguage;
$navigation = $strlanguage;
break;
}
$currentlang = current_language();
print_header("$site->shortname: $title", "$site->fullname",
"<a href=\"index.php\">$stradministration</a> -> ".
"<a href=\"configure.php\">$strconfiguration</a> -> $navigation");
if (!$mode) {
$currlang = current_language();
$langs = get_list_of_languages();
echo "<table align=center><tr><td align=\"right\">";
echo "<b>$strcurrentlanguage:</b>";
echo "</td><td>";
echo popup_form ("$CFG->wwwroot/admin/lang.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
echo "</td></tr></table>";
print_heading("<a href=\"lang.php?mode=missing\">$strmissingstrings</a>");
print_heading("<a href=\"lang.php?mode=compare\">$strcomparelanguage</a>");
echo "<center><hr noshade size=1>";
$options["lang"] = $currentlang;
print_single_button("http://moodle.org/download/lang/", $options, get_string("latestlanguagepack"));
echo "</center>";
print_footer();
exit;
}
// Get a list of all the root files in the English directory
$langdir = "$CFG->dirroot/lang/$currentlang";
$enlangdir = "$CFG->dirroot/lang/en";
if (! $stringfiles = get_directory_list($enlangdir, "", false)) {
error("Could not find English language pack!");
}
foreach ($stringfiles as $key => $file) {
if (substr($file, -4) != ".php") {
unset($stringfiles[$key]);
}
}
if ($mode == "missing") {
// For each file, check that a counterpart exists, then check all the strings
foreach ($stringfiles as $file) {
if (!file_exists("$langdir/$file")) {
if (!touch("$langdir/$file")) {
echo "<p><font color=red>".get_string("filemissing", "", "$langdir/$file")."</font></p>";
continue;
}
}
unset($string);
include("$enlangdir/$file");
$enstring = $string;
unset($string);
include("$langdir/$file");
$first = true;
foreach ($enstring as $key => $value) {
if (!isset($string[$key]) or $string[$key] == "") {
$value = htmlspecialchars($value);
$value = str_replace("$"."a", "\\$"."a", $value);
$value = str_replace("%%","%",$value);
if ($first) {
echo "</PRE><HR><P><B>".get_string("stringsnotset","","$langdir/$file")."</B></P><PRE>";
$first = false;
$somethingfound = true;
}
echo "$"."string['$key'] = \"$value\";<BR>";
}
}
}
if (! $files = get_directory_list("$CFG->dirroot/lang/en/help", "CVS")) {
error("Could not find English language help files!");
}
foreach ($files as $filekey => $file) { // check all the help files.
if (!file_exists("$langdir/help/$file")) {
echo "<p><font color=red>".get_string("filemissing", "", "$langdir/help/$file")."</font></p>";
$somethingfound = true;
continue;
}
}
if (! $files = get_directory_list("$CFG->dirroot/lang/en/docs", "CVS")) {
error("Could not find English language docs files!");
}
foreach ($files as $filekey => $file) { // check all the docs files.
if (!file_exists("$langdir/docs/$file")) {
echo "<P><FONT COLOR=red>".get_string("filemissing", "", "$langdir/docs/$file")."</FONT></P>";
$somethingfound = true;
continue;
}
}
if (!empty($somethingfound)) {
print_continue("lang.php");
} else {
notice(get_string("languagegood"), "lang.php");
}
} else if ($mode == "compare") {
if (isset($_POST['currentfile'])){ // Save a file
$newstrings = $_POST;
unset($newstrings['currentfile']);
if (lang_save_file($langdir, $currentfile, $newstrings)) {
notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
} else {
error("Could not save the file '$currentfile'!", "lang.php?mode=compare¤tfile=$currentfile");
}
}
print_heading_with_help($strcomparelanguage, "langedit");
print_simple_box_start("center", "80%");
echo '<center><font size="2">';
foreach ($stringfiles as $file) {
if ($file == $currentfile) {
echo "<b>$file</b> ";
} else {
echo "<a href=\"lang.php?mode=compare¤tfile=$file\">$file</a> ";
}
}
echo '</font></center>';
print_simple_box_end();
print_heading("$currentfile", "center", 4);
if (!file_exists("$langdir/$currentfile")) {
if (!touch("$langdir/$currentfile")) {
echo "<p><font color=red>".get_string("filemissing", "", "$langdir/$currentfile")."</font></p>";
continue;
}
}
error_reporting(0);
if ($f = fopen("$langdir/$currentfile","r+")) {
$editable = true;
fclose($f);
} else {
$editable = false;
echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")."</font></p>";
}
error_reporting(7);
unset($string);
include("$enlangdir/$currentfile");
$enstring = $string;
ksort($enstring);
unset($string);
include("$langdir/$currentfile");
if ($editable) {
echo "<form name=\"$currentfile\" action=\"lang.php\" method=\"post\">";
}
echo "<table width=\"100%\" cellpadding=2 cellspacing=3 border=0>";
foreach ($enstring as $key => $envalue) {
$envalue = nl2br(htmlspecialchars($envalue));
$envalue = preg_replace('/(\$a\-\>[a-zA-Z0-9]*|\$a)/', '<b>$0</b>', $envalue); // Make variables bold.
$envalue = str_replace("%%","%",$envalue);
$envalue = str_replace("\\","",$envalue); // Delete all slashes
echo "<tr>";
echo "<td width=20% bgcolor=\"$THEME->cellheading\" nowrap valign=top>$key</td>";
echo "<td width=40% bgcolor=\"$THEME->cellheading\" valign=top>$envalue</td>";
$value = $string[$key];
$value = str_replace("\r","",$value); // Bad character caused by Windows
$value = str_replace("\n\n\n\n\n\n","\n",$value); // Collapse runs of blank lines
$value = str_replace("\n\n\n\n\n","\n",$value);
$value = str_replace("\n\n\n\n","\n",$value);
$value = str_replace("\n\n\n","\n",$value);
$value = str_replace("\n\n\n","\n",$value);
$value = str_replace("\\","",$value); // Delete all slashes
$value = str_replace("%%","%",$value);
$value = htmlspecialchars($value);
$cellcolour = $value ? $THEME->cellcontent: $THEME->highlight;
if ($editable) {
echo "<td width=40% bgcolor=\"$cellcolour\" valign=top>";
if (isset($string[$key])) {
$valuelen = strlen($value);
} else {
$valuelen = strlen($envalue);
}
$cols=50;
if (strstr($value, "\r") or strstr($value, "\n") or $valuelen > $cols) {
$rows = ceil($valuelen / $cols);
echo "<textarea name=\"string-$key\" cols=\"$cols\" rows=\"$rows\">$value</textarea>";
} else {
if ($valuelen) {
$cols = $valuelen + 2;
}
echo "<input type=\"text\" name=\"string-$key\" value=\"$value\" size=\"$cols\"></td>";
}
echo "</TD>";
} else {
echo "<td width=40% bgcolor=\"$cellcolour\" valign=top>$value</td>";
}
}
if ($editable) {
echo "<tr><td colspan=2> <td><br />";
echo " <input type=\"hidden\" name=\"currentfile\" value=\"$currentfile\">";
echo " <input type=\"hidden\" name=\"mode\" value=\"compare\">";
echo " <input type=\"submit\" name=\"update\" value=\"".get_string("savechanges").": $currentfile\">";
echo "</td></tr>";
}
echo "</table>";
echo "</form>";
}
print_footer();
//////////////////////////////////////////////////////////////////////
function lang_save_file($path, $file, $strings) {
// Thanks to Petri Asikainen for the original version of code
// used to save language files.
//
// $path is a full pathname to the file
// $file is the file to overwrite.
// $strings is an array of strings to write
global $CFG, $USER;
error_reporting(0);
if (!$f = fopen("$path/$file","w")) {
return false;
}
error_reporting(7);
fwrite($f, "<?PHP // \$Id\$ \n");
fwrite($f, " // $file - created with Moodle $CFG->release ($CFG->version)\n\n\n");
ksort($strings);
foreach ($strings as $key => $value) {
list($id, $stringname) = explode("-",$key);
if ($CFG->lang != "zh_hk" and $CFG->lang != "zh_tw") { // Some MB languages include backslash bytes
$value = str_replace("\\","",$value); // Delete all slashes
}
$value = str_replace("'", "\\'", $value); // Add slashes for '
$value = str_replace('"', "\\\"", $value); // Add slashes for "
$value = str_replace("%","%%",$value); // Escape % characters
$value = str_replace("\r", "",$value); // Remove linefeed characters
if ($id == "string" and $value != ""){
fwrite($f,"\$string['$stringname'] = '$value';\n");
}
}
fwrite($f,"\n?>\n");
fclose($f);
return true;
}
?>