Patch for postgresql happiness
[moodle.git] / admin / lang.php
blob41c1a9fc78010e46c9e0a89821334857995c8e01
1 <?PHP // $Id$
3 require_once("../config.php");
5 optional_variable($mode, "");
6 optional_variable($currentfile, "moodle.php");
8 require_login();
10 if (!isadmin()) {
11 error("You need to be admin to edit this page");
14 if (! $site = get_site()) {
15 error("Site not defined!");
18 $stradministration = get_string("administration");
19 $strconfiguration = get_string("configuration");
20 $strlanguage = get_string("language");
21 $strcurrentlanguage = get_string("currentlanguage");
22 $strmissingstrings = get_string("missingstrings");
23 $strcomparelanguage = get_string("comparelanguage");
24 $strthislanguage = get_string("thislanguage");
26 switch ($mode) {
27 case "missing":
28 $navigation = "<A HREF=\"lang.php\">$strlanguage</A> -> $strmissingstrings";
29 $title = $strmissingstrings;
30 $button = '<form target="'.$CFG->framename.'" method="get" action="'.$CFG->wwwroot.'/admin/lang.php">'.
31 '<input type="hidden" name="mode" value="compare" />'.
32 '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />'.
33 '<input type="submit" value="'.$strcomparelanguage.'" /></form>';
34 break;
35 case "compare":
36 $navigation = "<A HREF=\"lang.php\">$strlanguage</A> -> $strcomparelanguage";
37 $title = $strcomparelanguage;
38 $button = '<form target="'.$CFG->framename.'" method="get" action="'.$CFG->wwwroot.'/admin/lang.php">'.
39 '<input type="hidden" name="mode" value="missing" />'.
40 '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />'.
41 '<input type="submit" value="'.$strmissingstrings.'" /></form>';
42 break;
43 default:
44 $title = $strlanguage;
45 $navigation = $strlanguage;
46 $button = '';
47 break;
50 $currentlang = current_language();
52 print_header("$site->shortname: $title", "$site->fullname",
53 "<a href=\"index.php\">$stradministration</a> -> ".
54 "<a href=\"configure.php\">$strconfiguration</a> -> $navigation",
55 '', '', true, $button);
57 if (!$mode) {
58 $currlang = current_language();
59 $langs = get_list_of_languages();
60 echo "<table align=center><tr><td align=\"right\">";
61 echo "<b>$strcurrentlanguage:</b>";
62 echo "</td><td>";
63 echo popup_form ("$CFG->wwwroot/admin/lang.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
64 echo "</td></tr></table>";
65 print_heading("<a href=\"lang.php?mode=missing&sesskey=$USER->sesskey\">$strmissingstrings</a>");
66 print_heading("<a href=\"lang.php?mode=compare&sesskey=$USER->sesskey\">$strcomparelanguage</a>");
67 echo "<center><hr noshade size=1>";
68 $options["lang"] = $currentlang;
69 print_single_button("http://moodle.org/download/lang/", $options, get_string("latestlanguagepack"));
70 echo "</center>";
71 print_footer();
72 exit;
75 // Get a list of all the root files in the English directory
77 $langdir = "$CFG->dirroot/lang/$currentlang";
78 $enlangdir = "$CFG->dirroot/lang/en";
80 if (! $stringfiles = get_directory_list($enlangdir, "", false)) {
81 error("Could not find English language pack!");
84 foreach ($stringfiles as $key => $file) {
85 if (substr($file, -4) != ".php") {
86 unset($stringfiles[$key]);
90 if ($mode == "missing" and confirm_sesskey()) {
91 // For each file, check that a counterpart exists, then check all the strings
93 foreach ($stringfiles as $file) {
94 if (!file_exists("$langdir/$file")) {
95 if (!touch("$langdir/$file")) {
96 echo "<p><font color=red>".get_string("filemissing", "", "$langdir/$file")."</font></p>";
97 continue;
101 unset($string);
102 include("$enlangdir/$file");
103 $enstring = $string;
105 unset($string);
106 include("$langdir/$file");
108 $first = true;
109 foreach ($enstring as $key => $value) {
110 if (!isset($string[$key]) or $string[$key] == "") {
111 $value = htmlspecialchars($value);
112 $value = str_replace("$"."a", "\\$"."a", $value);
113 $value = str_replace("%%","%",$value);
114 if ($first) {
115 echo "<p><b>".get_string("stringsnotset","","$langdir/$file")."</b></p><pre>";
116 $first = false;
117 $somethingfound = true;
119 echo "$"."string['$key'] = \"$value\";<br />";
122 if (!$first) {
123 echo '</pre><hr />';
127 if (! $files = get_directory_list("$CFG->dirroot/lang/en/help", "CVS")) {
128 error("Could not find English language help files!");
131 foreach ($files as $filekey => $file) { // check all the help files.
132 if (!file_exists("$langdir/help/$file")) {
133 echo "<p><font color=red>".get_string("filemissing", "", "$langdir/help/$file")."</font></p>";
134 $somethingfound = true;
135 continue;
139 if (! $files = get_directory_list("$CFG->dirroot/lang/en/docs", "CVS")) {
140 error("Could not find English language docs files!");
142 foreach ($files as $filekey => $file) { // check all the docs files.
143 if (!file_exists("$langdir/docs/$file")) {
144 echo "<P><FONT COLOR=red>".get_string("filemissing", "", "$langdir/docs/$file")."</FONT></P>";
145 $somethingfound = true;
146 continue;
150 if (!empty($somethingfound)) {
151 print_continue("lang.php");
152 } else {
153 notice(get_string("languagegood"), "lang.php");
156 } else if ($mode == "compare" and confirm_sesskey()) {
158 if (isset($_POST['currentfile'])){ // Save a file
159 $newstrings = $_POST;
160 unset($newstrings['currentfile']);
161 if (lang_save_file($langdir, $currentfile, $newstrings)) {
162 notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
163 } else {
164 error("Could not save the file '$currentfile'!", "lang.php?mode=compare&currentfile=$currentfile&sesskey=$USER->sesskey");
168 print_heading_with_help($strcomparelanguage, "langedit");
170 print_simple_box_start("center", "80%");
171 echo '<center><font size="2">';
172 foreach ($stringfiles as $file) {
173 if ($file == $currentfile) {
174 echo "<b>$file</b> &nbsp; ";
175 } else {
176 echo "<a href=\"lang.php?mode=compare&currentfile=$file&sesskey=$USER->sesskey\">$file</a> &nbsp; ";
179 echo '</font></center>';
180 print_simple_box_end();
183 print_heading("$currentfile", "center", 4);
185 if (!file_exists("$langdir/$currentfile")) {
186 if (!touch("$langdir/$currentfile")) {
187 echo "<p><font color=red>".get_string("filemissing", "", "$langdir/$currentfile")."</font></p>";
188 continue;
192 error_reporting(0);
193 if ($f = fopen("$langdir/$currentfile","r+")) {
194 $editable = true;
195 fclose($f);
196 } else {
197 $editable = false;
198 echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")."</font></p>";
200 error_reporting(7);
203 unset($string);
204 include("$enlangdir/$currentfile");
205 $enstring = $string;
206 if ($currentlang != 'en') {
207 $enstring['thislanguage'] = "<< TRANSLATORS: Specify the name of your language here. If possible use Unicode Numeric Character References >>";
208 $enstring['thischarset'] = "<< TRANSLATORS: Specify the character set of your language here. Note that all text created while this language is active will be stored using this character set, so don't change it once you have set it. Example: iso-8859-1 >>";
209 $enstring['thisdirection'] = "<< TRANSLATORS: This string specifies the direction of your text, either left-to-right or right-to-left. Insert either 'ltr' or 'rtl' here. >>";
210 $enstring['parentlanguage'] = "<< TRANSLATORS: If your language has a Parent Language that Moodle should use when strings are missing from your language pack, then specify the code for it here. If you leave this blank then English will be used. Example: nl >>";
212 ksort($enstring);
214 unset($string);
215 include("$langdir/$currentfile");
217 if ($editable) {
218 echo "<form name=\"$currentfile\" action=\"lang.php\" method=\"post\">";
219 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
221 echo "<table width=\"100%\" cellpadding=2 cellspacing=3 border=0>";
222 foreach ($enstring as $key => $envalue) {
223 $envalue = nl2br(htmlspecialchars($envalue));
224 $envalue = preg_replace('/(\$a\-\&gt;[a-zA-Z0-9]*|\$a)/', '<b>$0</b>', $envalue); // Make variables bold.
225 $envalue = str_replace("%%","%",$envalue);
226 $envalue = str_replace("\\","",$envalue); // Delete all slashes
228 echo "\n\n<tr>";
229 echo "<td dir=ltr lang=en width=20% bgcolor=\"$THEME->cellheading\" nowrap valign=top>$key</td>\n";
230 echo "<td dir=ltr lang=en width=40% bgcolor=\"$THEME->cellheading\" valign=top>$envalue</td>\n";
232 $value = $string[$key];
233 $value = str_replace("\r","",$value); // Bad character caused by Windows
234 $value = preg_replace("/\n{3,}/", "\n\n", $value); // Collapse runs of blank lines
235 $value = trim($value, "\n"); // Delete leading/trailing lines
236 $value = str_replace("\\","",$value); // Delete all slashes
237 $value = str_replace("%%","%",$value);
238 $value = str_replace("<","&lt;",$value);
239 $value = str_replace(">","&gt;",$value);
240 $value = str_replace('"',"&quot;",$value);
242 $cellcolour = $value ? $THEME->cellcontent: $THEME->highlight;
244 if ($editable) {
245 echo "<td width=40% bgcolor=\"$cellcolour\" valign=top>\n";
246 if (isset($string[$key])) {
247 $valuelen = strlen($value);
248 } else {
249 $valuelen = strlen($envalue);
251 $cols=50;
252 if (strstr($value, "\r") or strstr($value, "\n") or $valuelen > $cols) {
253 $rows = ceil($valuelen / $cols);
254 echo "<textarea name=\"string-$key\" cols=\"$cols\" rows=\"$rows\">$value</textarea>\n";
255 } else {
256 if ($valuelen) {
257 $cols = $valuelen + 2;
259 echo "<input type=\"text\" name=\"string-$key\" value=\"$value\" size=\"$cols\"></td>";
261 echo "</TD>\n";
263 } else {
264 echo "<td width=40% bgcolor=\"$cellcolour\" valign=top>$value</td>\n";
267 if ($editable) {
268 echo "<tr><td colspan=2>&nbsp;<td><br />";
269 echo " <input type=\"hidden\" name=\"currentfile\" value=\"$currentfile\">";
270 echo " <input type=\"hidden\" name=\"mode\" value=\"compare\">";
271 echo " <input type=\"submit\" name=\"update\" value=\"".get_string("savechanges").": $currentfile\">";
272 echo "</td></tr>";
274 echo "</table>";
275 echo "</form>";
279 print_footer();
281 //////////////////////////////////////////////////////////////////////
283 function lang_save_file($path, $file, $strings) {
284 // Thanks to Petri Asikainen for the original version of code
285 // used to save language files.
287 // $path is a full pathname to the file
288 // $file is the file to overwrite.
289 // $strings is an array of strings to write
291 global $CFG, $USER;
293 error_reporting(0);
294 if (!$f = fopen("$path/$file","w")) {
295 return false;
297 error_reporting(7);
300 fwrite($f, "<?PHP // \$Id\$ \n");
301 fwrite($f, " // $file - created with Moodle $CFG->release ($CFG->version)\n\n\n");
303 ksort($strings);
305 foreach ($strings as $key => $value) {
306 list($id, $stringname) = explode("-",$key);
307 if ($CFG->lang != "zh_hk" and $CFG->lang != "zh_tw") { // Some MB languages include backslash bytes
308 $value = str_replace("\\","",$value); // Delete all slashes
310 $value = str_replace("'", "\\'", $value); // Add slashes for '
311 $value = str_replace('"', "\\\"", $value); // Add slashes for "
312 $value = str_replace("%","%%",$value); // Escape % characters
313 $value = str_replace("\r", "",$value); // Remove linefeed characters
314 if ($id == "string" and $value != ""){
315 fwrite($f,"\$string['$stringname'] = '$value';\n");
318 fwrite($f,"\n?>\n");
319 fclose($f);
321 return true;