forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.php
1374 lines (1264 loc) · 58.7 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?PHP // $Id$
/**
* Display the admin/language menu and process strings translation.
*
* @param string $mode the mode of the script: null, "compare", "missing"
* @param string $currentfile the filename of the English file to edit (if mode==compare)
* @param bool $uselocal save translations into *_local pack?
*/
$dbg = ''; // debug output;
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup('langedit');
$context = get_context_instance(CONTEXT_SYSTEM);
define('LANG_SUBMIT_REPEAT', 1); // repeat displaying submit button?
define('LANG_SUBMIT_REPEAT_EVERY', 20); // if so, after how many lines?
define('LANG_DISPLAY_MISSING_LINKS', 1); // display "go to first/next missing string" links?
define('LANG_DEFAULT_FILE', ''); // default file to translate. Empty allowed
define('LANG_DEFAULT_HELPFILE', ''); // default helpfile to translate. Empty allowed
define('LANG_LINK_MISSING_STRINGS', 1); // create links from "missing" page to "compare" page?
define('LANG_DEFAULT_USELOCAL', 0); // should *_utf8_local be used by default?
define('LANG_MISSING_TEXT_MAX_LEN', 60); // maximum length of the missing text to display
define('LANG_KEEP_ORPHANS', 1); // keep orphaned strings (i.e. strings w/o English reference)
define('LANG_SEARCH_EXTRA', 1); // search lang files in extra locations
define('LANG_ALWAYS_TEXTAREA', 1); // always use <textarea> even for short strings MDL-15738
$mode = optional_param('mode', '', PARAM_ALPHA);
if ($mode == 'helpfiles') {
// use different PARAM_ options according to mode
$currentfile = optional_param('currentfile', LANG_DEFAULT_HELPFILE, PARAM_PATH);
} else {
$currentfile = optional_param('currentfile', LANG_DEFAULT_FILE, PARAM_FILE);
}
$uselocal = optional_param('uselocal', -1, PARAM_INT);
if ($uselocal == -1) {
if (isset($SESSION->langtranslateintolocal)) {
$uselocal = $SESSION->langtranslateintolocal;
} else {
$uselocal = LANG_DEFAULT_USELOCAL;
}
} else {
$SESSION->langtranslateintolocal = $uselocal;
}
if (!has_capability('moodle/site:langeditmaster', $context, $USER->id, false)) {
// Force using _local
$uselocal = 1;
}
if (!has_capability('moodle/site:langeditmaster', $context, $USER->id, false) && (!$uselocal)) {
print_error('cannoteditmasterlang', 'error');
}
if ((!has_capability('moodle/site:langeditlocal', $context, $USER->id, false)) && ($uselocal)) {
print_error('cannotcustomizelocallang', 'error');
}
$strlanguage = get_string("language");
$strcurrentlanguage = get_string("currentlanguage");
$strmissingstrings = get_string("missingstrings");
$streditstrings = get_string("editstrings", 'admin');
$stredithelpdocs = get_string("edithelpdocs", 'admin');
$strthislanguage = get_string("thislanguage");
$strgotofirst = get_string('gotofirst','admin');
$strfilestoredin = get_string('filestoredin', 'admin');
$strfilestoredinhelp = get_string('filestoredinhelp', 'admin');
$strswitchlang = get_string('switchlang', 'admin');
$strchoosefiletoedit = get_string('choosefiletoedit', 'admin');
$streditennotallowed = get_string('langnoeditenglish', 'admin');
$strfilecreated = get_string('filecreated', 'admin');
$strprev = get_string('previous');
$strnext = get_string('next');
$strlocalstringcustomization = get_string('localstringcustomization', 'admin');
$strlangpackmaintaining = get_string('langpackmaintaining', 'admin');
$strnomissingstrings = get_string('nomissingstrings', 'admin');
$streditingnoncorelangfile = get_string('editingnoncorelangfile', 'admin');
$strlanglocalpackage = get_string('langlocalpackage', 'admin');
$strlangmasterpackage = get_string('langmasterpackage', 'admin');
$strlangmasterenglish = get_string('langmasterenglish', 'admin');
$currentlang = current_language();
switch ($mode) {
case "missing":
// Missing array keys are not bugs here but missing strings
error_reporting(E_ALL ^ E_NOTICE);
$title = $strmissingstrings;
break;
case "compare":
$title = $streditstrings;
break;
case "helpfiles":
$title = $stredithelpdocs;
default:
$title = $strlanguage;
break;
}
$navlinks[] = array('name' => $strlanguage, 'link' => "$CFG->wwwroot/$CFG->admin/lang.php", 'type' => 'misc');
$navigation = build_navigation($navlinks);
admin_externalpage_print_header();
// Prepare and render menu tabs
$firstrow = array();
$secondrow = array();
$inactive = NULL;
$activated = NULL;
$currenttab = $mode;
if ($uselocal) {
$inactive = array('uselocal');
$activated = array('uselocal');
} else {
$inactive = array('usemaster');
$activated = array('usemaster');
}
if (has_capability('moodle/site:langeditlocal', $context, $USER->id, false)) {
$firstrow[] = new tabobject('uselocal',
"$CFG->wwwroot/$CFG->admin/lang.php?mode=$mode&currentfile=$currentfile&uselocal=1",
$strlocalstringcustomization );
}
if (has_capability('moodle/site:langeditmaster', $context, $USER->id, false)) {
$firstrow[] = new tabobject('usemaster',
"$CFG->wwwroot/$CFG->admin/lang.php?mode=$mode&currentfile=$currentfile&uselocal=0",
$strlangpackmaintaining );
}
$secondrow[] = new tabobject('missing', "$CFG->wwwroot/$CFG->admin/lang.php?mode=missing", $strmissingstrings );
$secondrow[] = new tabobject('compare', "$CFG->wwwroot/$CFG->admin/lang.php?mode=compare", $streditstrings );
$secondrow[] = new tabobject('helpfiles', "$CFG->wwwroot/$CFG->admin/lang.php?mode=helpfiles", $stredithelpdocs );
$tabs = array($firstrow, $secondrow);
print_tabs($tabs, $currenttab, $inactive, $activated);
if (!$mode) {
// TODO this is a very nice place to put some translation statistics
print_box_start();
$currlang = current_language();
$langs = get_list_of_languages(false, true);
popup_form ("$CFG->wwwroot/$CFG->admin/lang.php?lang=", $langs, "chooselang", $currlang, "", "", "", false, 'self', $strcurrentlanguage.':');
print_box_end();
admin_externalpage_print_footer();
exit;
}
// Get a list of all the root files in the English directory
$langbase = $CFG->dataroot . '/lang';
$enlangdir = "$CFG->dirroot/lang/en_utf8";
if ($currentlang == 'en_utf8') {
$langdir = $enlangdir;
} else {
$langdir = "$langbase/$currentlang";
}
$locallangdir = "$langbase/{$currentlang}_local";
$saveto = $uselocal ? $locallangdir : $langdir;
if (($mode == 'missing') || ($mode == 'compare')) {
// get the list of all English stringfiles
$stringfiles = lang_standard_locations();
if (LANG_SEARCH_EXTRA) {
$stringfiles += lang_extra_locations();
}
if (count($stringfiles) == 0) {
print_error('cannotfindlang', 'error', '', 'English');
}
} elseif ($mode == 'helpfiles') {
$helpfiles = lang_help_standard_locations();
if (LANG_SEARCH_EXTRA) {
$helpfiles += lang_help_extra_locations();
}
if (count($helpfiles) == 0) {
print_error("cannotfindhelp", 'error', '', 'English');
}
}
if ($mode == 'missing') {
if (!file_exists($langdir)) {
print_error('invalidlangpack');
}
// Following variables store the HTML output to be echo-ed
$m = '';
$o = '';
$m_x = false;
// Total number of strings and missing strings
$totalcounter->strings = 0;
$totalcounter->missing = 0;
// For each file, check that a counterpart exists, then check all the strings
foreach ($stringfiles as $stringfile) {
$location = $stringfile['location'];
$plugin = $stringfile['plugin'];
$prefix = $stringfile['prefix'];
$filename = $stringfile['filename'];
unset($string);
// Get some information about file locations:
// $enfilepath = the path to the English file distributed either in the core space or in plugin space
// $trfilepath = the path to the translated file distributed either in the lang pack or in plugin space
// $lcfilepath = the path to the _local customization
// $trfilename = the filename of the translated version of the file (including prefix for non-core files)
if ($location || $plugin) {
// non-core file in an extra location
$enfilepath = "$CFG->dirroot/$location/$plugin/lang/en_utf8/$filename";
$trfilepath = "$CFG->dirroot/$location/$plugin/lang/$currentlang/$filename";
$lcfilepath = "$locallangdir/$filename";
$trfilename = $filename;
if (!$m_x) {
$m .= '<hr />';
$m_x = true;
}
} else {
// core file in standard location
$enfilepath = "$CFG->dirroot/lang/en_utf8/$filename";
$trfilepath = "$langdir/$filename";
$lcfilepath = "$locallangdir/$filename";
$trfilename = $filename;
}
// $enstring = English strings distributed either in the core space or in plugin space
include($enfilepath);
$enstring = isset($string) ? $string : array();
unset($string);
ksort($enstring);
//$lcstring = local customizations
$lcstring = array();
if (file_exists($lcfilepath)) {
include($lcfilepath);
$localfileismissing = 0;
if (isset($string) && is_array($string)) {
$lcstring = $string;
}
unset($string);
ksort($lcstring);
} else {
$localfileismissing = 1;
}
// $string = translated strings distibuted either in core lang pack or in plugin space
$string = array();
if (file_exists($trfilepath)) {
include($trfilepath);
$fileismissing = 0;
} else {
$fileismissing = 1;
$o .= notify(get_string("filemissing", "", $trfilepath), "notifyproblem", "center", true);
}
$missingcounter = 0;
$first = true; // first missing string found in the file
// For all English strings in the current file check distributed translations and _local customizations
foreach ($enstring as $key => $value) {
$totalcounter->strings++;
$missingstring = false;
$missinglocalstring = false;
$translationsdiffer = false;
if (empty($string[$key]) and $string[$key] != "0") { // MDL-4735
// string is missing in distributed pack
$missingstring = true;
}
if (empty($lcstring[$key]) and $lcstring[$key] != "0") { // MDL-4735
// string is missing in _local customization
$missinglocalstring = true;
}
if (!$missingstring && !$missinglocalstring && ($lcstring[$key] != $string[$key])) {
$translationsdiffer = true;
}
if ($missingstring || $translationsdiffer) {
$value = htmlspecialchars($value);
$value = str_replace("$"."a", "\\$"."a", $value);
$value = str_replace("%%","%",$value);
if ($first) {
$m .= "<a href=\"lang.php?mode=missing#$trfilename\">$trfilename";
$m .= $fileismissing ? '*' : '';
$m .= '</a> ';
$o .= "<p><a name=\"$trfilename\"></a><b>".
get_string("stringsnotset","", $trfilepath)."</b></p><pre>";
$first = false;
$somethingfound = true;
}
if ($missingstring) {
$missingcounter++;
$totalcounter->missing++;
}
if ($translationsdiffer) {
$missingcounter++;
}
if (LANG_LINK_MISSING_STRINGS && ($missingstring || $translationsdiffer)) {
$missinglinkstart = "<a href=\"lang.php?mode=compare&currentfile=$filename#$key\">";
$missinglinkend = '</a>';
} else {
$missinglinkstart = '';
$missinglinkend = '';
}
if (strlen($value) > LANG_MISSING_TEXT_MAX_LEN) {
$value = lang_xhtml_save_substr($value, 0, LANG_MISSING_TEXT_MAX_LEN) . ' ...'; // MDL-8852
}
if ($translationsdiffer) {
$o .= '// ';
}
$o .= "$"."string['".$missinglinkstart.$key.$missinglinkend."'] = \"$value\";";
if ($translationsdiffer) {
$o .= ' // differs from the translation in _local';
} elseif (!$missinglocalstring) {
$o .= ' // translated only in _local';
}
$o .= "\n";
}
}
if (!$first) {
$o .= '</pre><hr />';
}
}
if ($totalcounter->missing > 0) {
$totalcounter->missingpercent = sprintf('%02.1f', ($totalcounter->missing / $totalcounter->strings * 100));
print_heading(get_string('numberofstrings', 'admin', $totalcounter), '', 4);
} else {
print_heading($strnomissingstrings, '', 4, 'notifysuccess');
}
if ($m <> '') {
print_box($m, 'filenames');
}
echo $o;
if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
print_error("cannotfindhelp", 'error', '', 'English');
}
foreach ($files as $filekey => $file) { // check all the help files.
if (!file_exists("$langdir/help/$file")) {
notify(get_string("filemissing", "", "$langdir/help/$file"), 'notifyproblem');
$somethingfound = true;
continue;
}
}
if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
print_error("cannotfinddoc", 'error', '', 'English');
}
foreach ($files as $filekey => $file) { // check all the docs files.
if (!file_exists("$langdir/docs/$file")) {
notify(get_string("filemissing", "", "$langdir/docs/$file"), 'notifyproblem');
$somethingfound = true;
continue;
}
}
if (!empty($somethingfound)) {
print_continue("lang.php");
} else {
notice(get_string("languagegood"), "lang.php" );
}
} else if ($mode == 'compare') {
if (!file_exists($langbase) ){
if (!lang_make_directory($langbase) ){
print_error('cannotcreatelangbase', 'error');
} else {
echo '<div class="notifysuccess">Created directory '.
$langbase .'</div>'."<br />\n";
}
}
if (!$uselocal && !file_exists($langdir)) {
if (!lang_make_directory($langdir)) {
print_error('cannotcreatelangdir', 'error');
} else {
echo '<div class="notifysuccess">Created directory '.
$langdir .'</div>'."<br />\n";
}
}
if ($uselocal && !file_exists($locallangdir)) {
if (!lang_make_directory($locallangdir)) {
echo '<div class="notifyproblem">ERROR: Could not create directory '.
$locallangdir .'</div>'."<br />\n";
$uselocal = 0;
} else {
echo '<div class="notifysuccess">Created directory '.
$locallangdir .'</div>'."<br />\n";
}
}
if ($currentfile <> '') {
if (!$fileinfo = lang_get_file_info($currentfile, $stringfiles)) {
print_error('cannotfindinfo', 'error', '', $currentfile);
}
// check the filename is set up correctly, prevents bugs similar to MDL-10920
$location = $fileinfo['location'];
$plugin = $fileinfo['plugin'];
$prefix = $fileinfo['prefix'];
$filename = $fileinfo['filename'];
if ($location || $plugin) {
// file in an extra location
if ($currentfile != "{$prefix}{$plugin}.php") {
print_error('filemismatch', 'error', '', (object)array('current'=>$currectfile, 'file'=>$prefix.$plugin.'.php'));
}
if (!$uselocal) {
notify($streditingnoncorelangfile);
$editable = false;
}
} else {
// file in standard location
if ($currentfile != $filename) {
print_error('filemismatch', 'error', '', (object)array('current'=>$currectfile, 'file'=>$filename.'.php'));
}
}
// Get some information about file locations:
// $enfilepath = the path to the English file distributed either in the core space or in plugin space
// $trfilepath = the path to the translated file distributed either in the lang pack or in plugin space
// $lcfilepath = the path to the _local customization
// $trfilename = the filename of the translated version of the file (including prefix for non-core files)
if ($location || $plugin) {
// non-core file in an extra location
$enfilepath = "$CFG->dirroot/$location/$plugin/lang/en_utf8/$filename";
$trfilepath = "$CFG->dirroot/$location/$plugin/lang/$currentlang/$filename";
$lcfilepath = "$locallangdir/$filename";
$trfilename = $filename;
} else {
// core file in standard location
$enfilepath = "$CFG->dirroot/lang/en_utf8/$filename";
$trfilepath = "$langdir/$filename";
$lcfilepath = "$locallangdir/$filename";
$trfilename = $filename;
}
}
if (isset($_POST['currentfile'])){ // Save a file
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', 'error');
}
$newstrings = array();
foreach ($_POST as $postkey => $postval) {
$stringkey = lang_file_string_key($postkey);
$newstrings[$stringkey] = $postval;
}
unset($newstrings['currentfile']);
$packstring = array();
$saveinto = $langdir;
if ($uselocal) {
if(file_exists($trfilepath)) {
include($trfilepath);
if (isset($string)) {
$packstring = $string;
}
unset($string);
}
$saveinto = $locallangdir;
}
if (lang_save_file($saveinto, $currentfile, $newstrings, $uselocal, $packstring)) {
notify(get_string("changessaved")." ($saveinto/$currentfile)", "notifysuccess");
} else {
print_error('cannotsavefile', 'error', 'lang.php?mode=compare&currentfile=$currentfile', $saveinto.'/'.$currentfile);
}
unset($packstring);
}
print_box_start('generalbox editstrings');
$menufiles = array();
$menufiles_coregrp = 1;
foreach ($stringfiles as $stringfile) {
$item_key = $stringfile['filename'];
$item_label = $stringfile['filename'];
if ($stringfile['location'] != '' && $stringfile['plugin'] != '') {
$item_label .= ' ('.$stringfile['location'].'/'.$stringfile['plugin'].')';
if ($menufiles_coregrp == 1) {
$menufiles['extra'] = '------------';
$menufiles_coregrp = 0;
}
}
$menufiles[$item_key] = $item_label;
}
$selectionlabel = '<code class="path">';
//$selectionlabel .= $strfilestoredin;
$selectionlabel .= $uselocal ? "{$currentlang}_local" : $currentlang;
$selectionlabel .= '/</code>';
popup_form("$CFG->wwwroot/$CFG->admin/lang.php?mode=compare&currentfile=", $menufiles, "choosefile",
$currentfile, $strchoosefiletoedit, '', '', false, 'self', $selectionlabel);
helpbutton('langswitchstorage', $strfilestoredinhelp, 'moodle');
print_box_end();
if ($currentfile <> '') {
error_reporting(0);
if (!isset($editable) || $editable) {
if (!file_exists("$saveto/$currentfile")) {
if (!@touch("$saveto/$currentfile")) {
print_heading(get_string("filemissing", "", "$saveto/$currentfile"), '', 4, 'error');
} else {
print_heading($strfilecreated, '', 4, 'notifysuccess');
}
}
if ($currentlang == "en_utf8" && !$uselocal) {
$editable = false;
print_heading($streditennotallowed, '', 4);
} elseif ($f = fopen("$saveto/$currentfile","r+")) {
$editable = true;
fclose($f);
} else {
$editable = false;
notify(get_string("makeeditable", "", "$saveto/$currentfile"), 'notifyproblem');
}
}
error_reporting($CFG->debug);
$o = ''; // stores the HTML output to be echo-ed
unset($string);
include($enfilepath);
$enstring = isset($string) ? $string : array();
//
// Following strings have moved into langconfig.php, but keep the here for backward compatibility
//
if ($currentlang != 'en' and $currentfile == 'moodle.php') {
$enstring['thislanguage'] = "<< TRANSLATORS: Specify the name of your language here. If possible use Unicode Numeric Character References >>";
$enstring['thischarset'] = "<< TRANSLATORS: Charset encoding - always use utf-8 >>";
$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. >>";
$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 >>";
}
unset($string);
ksort($enstring);
@include($lcfilepath);
$localstring = isset($string) ? $string : array();
unset($string);
ksort($localstring);
@include($trfilepath);
$string = isset($string) ? $string : array();
ksort($string);
if ($editable) {
$o .= "<form id=\"$currentfile\" action=\"lang.php\" method=\"post\">";
$o .= '<div>';
}
$o .= "<table summary=\"\" width=\"100%\" class=\"translator\">";
$linescounter = 0;
$missingcounter = 0;
foreach ($enstring as $key => $envalue) {
$linescounter++ ;
if (LANG_SUBMIT_REPEAT && $editable && $linescounter % LANG_SUBMIT_REPEAT_EVERY == 0) {
$o .= '<tr><td> </td><td><br />';
$o .= '<input type="submit" tabindex="'.$missingcounter.'" name="update" value="'.get_string('savechanges').': '.$currentfile.'" />';
$o .= '<br /> </td></tr>';
}
$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
$o .= "\n\n".'<tr class="';
if ($linescounter % 2 == 0) {
$o .= 'r0';
} else {
$o .= 'r1';
}
$o .= '">';
$o .= '<td dir="ltr" lang="en">';
$o .= '<span id="'.$key.'" class="stren">'.$envalue.'</span>';
$o .= '<br />'."\n";
$o .= '<span class="strkey">'.$key.'</span>';
$o .= '</td>'."\n";
// Missing array keys are not bugs here but missing strings
error_reporting(E_ALL ^ E_NOTICE);
if ($uselocal) {
$value = lang_fix_value_from_file($localstring[$key]);
$value2 = lang_fix_value_from_file($string[$key]);
if ($value == '') {
$value = $value2;
}
} else {
$value = lang_fix_value_from_file($string[$key]);
$value2 = lang_fix_value_from_file($localstring[$key]);
}
error_reporting($CFG->debug);
$missingtarget = '';
$missingnext = '';
$missingprev = '';
$cellcolour = '';
$usetabindex = false;
if (!$value) {
// the string is not present in the pack being processed
if (!$value2) {
$cellcolour = 'class="bothmissing"';
$usetabindex = true;
} else {
$cellcolour = 'class="mastermissing"';
$usetabindex = true;
}
$missingcounter++;
if (LANG_DISPLAY_MISSING_LINKS) {
$missingtarget = '<a name="missing'.$missingcounter.'"></a>';
$missingnext = '<a href="#missing'.($missingcounter+1).'">'.
'<img src="' . $CFG->pixpath . '/t/down.gif" class="iconsmall" alt="'.$strnext.'" /></a>';
$missingprev = '<a href="#missing'.($missingcounter-1).'">'.
'<img src="' . $CFG->pixpath . '/t/up.gif" class="iconsmall" alt="'.$strprev.'" /></a>';
}
} else {
// the string is translated in the pack being processed
if ($value <> $value2 && ($value2 <> '')) {
$cellcolour = 'class="localdifferent"';
$usetabindex = true;
$missingcounter++;
if (LANG_DISPLAY_MISSING_LINKS) {
$missingtarget = '<a name="missing'.$missingcounter.'"></a>';
$missingnext = '<a href="#missing'.($missingcounter+1).'">'.
'<img src="' . $CFG->pixpath . '/t/down.gif" class="iconsmall" alt="'.$strnext.'" /></a>';
$missingprev = '<a href="#missing'.($missingcounter-1).'">'.
'<img src="' . $CFG->pixpath . '/t/up.gif" class="iconsmall" alt="'.$strprev.'" /></a>';
}
}
}
if ($editable) {
$o .= '<td '.$cellcolour.' valign="top">';
if ($missingcounter > 1) {
$o .= $missingprev;
}
$o .= $missingtarget."\n";
if (isset($string[$key])) {
$valuelen = strlen($value);
} else {
$valuelen = strlen($envalue);
}
$cols=40;
if ($usetabindex) {
$tabindex = 'tabindex="'.$missingcounter.'"';
} else {
$tabindex = '';
}
if (strstr($value, "\r") or strstr($value, "\n") or $valuelen > $cols) {
$rows = ceil($valuelen / $cols);
$o .= '<textarea name="stringXXX'.lang_form_string_key($key).'" cols="'.$cols.'" rows="'.$rows.'" '.$tabindex.'>'.$value.'</textarea>'."\n";
} else {
if ($valuelen) {
$cols = $valuelen + 5;
}
if (LANG_ALWAYS_TEXTAREA) {
$o .= '<textarea name="stringXXX'.lang_form_string_key($key).'" cols="'.$cols.'" rows="1" '.$tabindex.'>'.$value.'</textarea>'."\n";
} else {
$o .= '<input type="text" name="stringXXX'.lang_form_string_key($key).'" value="'.$value.'" size="'.$cols.'" '.$tabindex.' />';
}
}
if ($value2 <> '' && $value <> $value2) {
$o .= '<br /><span style="font-size:small">'.$value2.'</span>';
}
$o .= $missingnext . '</td>';
} else {
$o .= '<td '.$cellcolour.' valign="top">'.$value.'<br />'.$value2.'</td>';
}
$o .= '</tr>'."\n";
}
if ($editable) {
$o .= '<tr><td> </td><td><br />';
$o .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$o .= '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
$o .= '<input type="hidden" name="mode" value="compare" />';
$o .= '<input type="submit" name="update" tabindex="'.$missingcounter.'" value="'.get_string('savechanges').': '.$currentfile.'" />';
$o .= '</td></tr>';
}
$o .= '</table>';
if ($editable) {
$o .= '</div>';
$o .= '</form>';
}
if (LANG_DISPLAY_MISSING_LINKS) {
if ($missingcounter > 0) {
print_heading(get_string('numberofmissingstrings', 'admin', $missingcounter), '', 4);
if ($editable) {
print_heading('<a href="#missing1">'.$strgotofirst.'</a>', "", 4);
}
} else {
print_heading($strnomissingstrings, '', 4, 'notifysuccess');
}
}
echo $o;
} else {
// no $currentfile specified
// no useful information to display - maybe some help? instructions?
}
} elseif ($mode == 'helpfiles') {
$saveto = $saveto.'/help'; // the edited content will be saved to
$enlangdir = $enlangdir.'/help'; // English master help files localtion
$langdir = $langdir.'/help'; // current language master help files location
$locallangdir = $locallangdir.'/help'; // local modifications of help files location
$altdir = $uselocal ? $langdir : $locallangdir; // alternative to $saveto
$fileeditorrows = 10; // number of textareas' rows
$fileeditorcols = 100; // dtto cols
$filemissingmark = ' (***)'; // mark to add to non-existing or zero-length files
$fileoldmark = ' (old?)'; // mark to add to filenames in selection form if the English version is newer
$filetemplate = ''; // template for new files, e.g. CVS identification
if (isset($_POST['currentfile'])) { // Save a file
if (!confirm_sesskey()) {
print_error('confirmsesskeybad', 'error');
}
if (lang_help_save_file($saveto, $currentfile, $_POST['filedata'])) {
notify(get_string("changessaved")." ($saveto/$currentfile)", "notifysuccess");
} else {
print_error('cannotsavefile', 'error', 'lang.php?mode=compare&currentfile=$currentfile', array($saveinto, $currentfile));
}
}
print_box_start('generalbox editstrings');
$menufiles = array();
$menufiles_coregrp = 1;
$origlocation = ''; // the location of the currentfile's English source will be stored here
$origplugin = ''; // dtto plugin
foreach ($helpfiles as $helppath => $helpfile) {
$item_key = $helpfile['filename'];
$item_label = $helpfile['filename'];
if ((!file_exists($saveto.'/'.$helpfile['filename'])) || (filesize($saveto.'/'.$helpfile['filename']) == 0)) {
$item_label .= $filemissingmark;
} else {
if (filemtime($saveto.'/'.$helpfile['filename']) < filemtime($helppath)) {
$item_label .= $fileoldmark;
}
if ($helpfile['location'] != '' && $helpfile['plugin'] != '') {
$item_label .= ' ('.$helpfile['location'].'/'.$helpfile['plugin'].')';
if ($menufiles_coregrp == 1) {
$menufiles['extra'] = '------------';
$menufiles_coregrp = 0;
}
}
}
$menufiles[$item_key] = $item_label;
if ($currentfile == $helpfile['filename']) {
$origlocation = $helpfile['location'];
$origplugin = $helpfile['plugin'];
}
}
$selectionlabel = '<code class="path">';
//$selectionlabel .= $strfilestoredin;
$selectionlabel .= $uselocal ? "{$currentlang}_local" : $currentlang;
$selectionlabel .= '/help/</code>';
popup_form("$CFG->wwwroot/$CFG->admin/lang.php?mode=helpfiles&currentfile=", $menufiles, "choosefile",
$currentfile, $strchoosefiletoedit, '', '', false, 'self', $selectionlabel);
helpbutton('langswitchstorage', $strfilestoredinhelp, 'moodle');
print_box_end();
if (!empty($currentfile)) {
if (!file_exists("$saveto/$currentfile")) {
$dbg .= "File does not exist: $saveto/$currentfile\n";
//check if directory exist
if (!file_exists(dirname("$saveto/$currentfile"))) {
if(!lang_make_directory(dirname("$saveto/$currentfile"))) {
echo ('Cannot create directory: '.dirname("$saveto/$currentfile"));
}
}
//
// file doesn't exist - let's check webserver's permission to create it
//
if (!@touch("$saveto/$currentfile")) {
//
// webserver is unable to create new file
//
notify(get_string('filemissing', '', "$saveto/$currentfile" ));
notify(get_string('makeeditable', '', "$saveto/$currentfile"));
$editable = false;
} else {
//
// webserver can create new file - we can delete it now and let
// it create again if its filesize() > 0
//
$editable = true;
unlink("$saveto/$currentfile");
}
} elseif (is_writable("$saveto/$currentfile")) {
$editable = true;
} else {
//
// file exists but it is not writeable by web server process :-(
//
$editable = false;
notify(get_string('makeeditable', '', "$saveto/$currentfile"));
}
// master en_utf8 in dataroot is not editable
if ((!$uselocal) && ($currentlang == 'en_utf8')) {
$editable = false;
}
echo '<div>';
if ($uselocal) {
$strsavetotitle = $strlanglocalpackage . helpbutton('langpackages', $strlanglocalpackage, 'moodle', true, false, '', true);
$straltdirtitle = $strlangmasterpackage . helpbutton('langpackages', $strlangmasterpackage, 'moodle', true, false, '', true);
} else {
$straltdirtitle = $strlanglocalpackage . helpbutton('langpackages', $strlanglocalpackage, 'moodle', true, false, '', true);
$strsavetotitle = $strlangmasterpackage . helpbutton('langpackages', $strlangmasterpackage, 'moodle', true, false, '', true);
}
if ($editable) {
// generate an editor for the current help file in $saveto
echo '<fieldset><legend>'.$strsavetotitle.'</legend>';
echo "<form id=\"helpfileeditor\" action=\"lang.php\" method=\"post\">";
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
echo '<input type="hidden" name="mode" value="helpfiles" />';
echo "<div class='mdl-align'>\n";
echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">";
if (file_exists("$saveto/$currentfile")) {
echo htmlspecialchars(file_get_contents("$saveto/$currentfile"));
} else {
echo ($filetemplate);
}
echo "</textarea>\n</div>\n";
echo '<div class="mdl-align"><input type="submit" value="'.get_string('savechanges').'" /></div>';
echo '</form>';
$preview_url = lang_help_preview_url($currentfile, !$uselocal);
if ($preview_url) {
link_to_popup_window($preview_url, 'popup', get_string('preview'));
}
echo '</fieldset>';
}
if (is_readable("$altdir/$currentfile")) {
// show the content of the same help file in alternative location
echo '<fieldset><legend>'.$straltdirtitle.'</legend>';
echo "<div class='mdl-align'>\n";
echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">";
if (file_exists("$altdir/$currentfile")) {
echo htmlspecialchars(file_get_contents("$altdir/$currentfile"));
} else {
echo ($filetemplate);
}
echo "</textarea>\n</div>\n";
$preview_url = lang_help_preview_url($currentfile, $uselocal);
if ($preview_url) {
link_to_popup_window($preview_url, 'popup', get_string('preview'));
}
echo '</fieldset>';
}
// show the content of the original English file either in core space or plugin space
if ($origlocation != '' && $origplugin != '') {
// non-core help file
$ensrc = "$CFG->dirroot/$origlocation/$origplugin/lang/en_utf8/help/$currentfile";
} else {
// core help file
$ensrc = "$enlangdir/$currentfile";
}
if (is_readable($ensrc)) {
echo '<fieldset><legend>'.$strlangmasterenglish;
helpbutton('langpackages', $strlangmasterenglish);
echo '</legend>';
echo "<div class='mdl-align'>\n<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">";
echo htmlspecialchars(file_get_contents($ensrc));
echo "</textarea>\n</div>\n";
$preview_url = lang_help_preview_url($currentfile, true, 'en_utf8'); // do not display en_utf8_local
if ($preview_url) {
link_to_popup_window($preview_url, 'popup', get_string('preview'));
}
echo '</fieldset>';
}
echo '</div>'; // translator box
error_reporting($CFG->debug);
}
if (false && $CFG->debugdisplay && debugging('', DEBUG_DEVELOPER) ) {
echo '<hr />';
print_heading('Debugging info');
echo '<pre class="notifytiny">';
print_r($dbg);
print_r("\n\$currentfile = $currentfile");
print_r("\n\$enlangdir = $enlangdir");
print_r("\n\$langdir = $langdir");
print_r("\n\$locallangdir = $locallangdir");
print_r("\n\$saveto = $saveto");
print_r("\n\$altdir = $altdir");
print_r("\n\$origlocation = $origlocation");
print_r("\n\$origplugin = $origplugin");
print_r("\n\$ensrc = $ensrc");
print_r("\n\$helpfiles = ");
print_r($helpfiles);
echo '</pre>';
}
} // fi $mode == 'helpfiles'
admin_externalpage_print_footer();
//////////////////////////////////////////////////////////////////////
/**
* Save language translation file.
*
* Thanks to Petri Asikainen for the original version of code
* used to save language files.
*
* @uses $CFG
* @uses $USER
* @param string $path Full pathname to the directory to use
* @param string $file File to overwrite
* @param array $strings Array of strings to write
* @param bool $local Should *_local version be saved?
* @param array $packstrings Array of default langpack strings (needed if $local)
* @return bool Created successfully?
*/
function lang_save_file($path, $file, $strings, $local, $packstrings) {
global $CFG, $USER;
if (LANG_KEEP_ORPHANS) {
// let us load the current content of the file
unset($string);
@include("$path/$file");
if (isset($string)) {
$orphans = $string;
unset($string);
} else {
$orphans = array();
}
}
// let us rewrite the file
if (!$f = @fopen("$path/$file","w")) {
return false;
}
fwrite($f, "<?PHP // \$Id\$ \n");
fwrite($f, " // $file - created with Moodle $CFG->release ($CFG->version)\n");
if ($local) {
fwrite($f, " // local modifications from $CFG->wwwroot\n");
}
fwrite($f, "\n\n");
ksort($strings);
foreach ($strings as $key => $value) {
@list($id, $stringname) = explode('XXX',$key);
$value = lang_fix_value_before_save($value);
if ($id == "string" and $value != ""){
if ((!$local) || (!isset($packstrings[$stringname])) || (lang_fix_value_from_file($packstrings[$stringname]) <> lang_fix_value_from_file($value))) {
// Either we are saving the master language pack
// or the string is not saved in packstring - fixes PHP notices about missing key
// or we are saving local language pack and the strings differ.
fwrite($f,"\$string['$stringname'] = '$value';\n");
}
if (LANG_KEEP_ORPHANS && isset($orphans[$stringname])) {
unset($orphans[$stringname]);
}
}
}
if (LANG_KEEP_ORPHANS) {
// let us add orphaned strings, i.e. already translated strings without the English referential source
foreach ($orphans as $key => $value) {
fwrite($f,"\$string['$key'] = '".lang_fix_value_before_save($value)."'; // ORPHANED\n");
}
}
fwrite($f,"\n?>\n");
fclose($f);
return true;
}
/**
* Fix value of the translated string after it is load from the file.
*
* These modifications are typically necessary to work with the same string coming from two sources.
* We need to compare the content of these sources and we want to have e.g. "This string\r\n"
* to be the same as " This string\n".
*
* @param string $value Original string from the file
* @return string Fixed value
*/
function lang_fix_value_from_file($value='') {
$value = str_replace("\r","",$value); // Bad character caused by Windows
$value = preg_replace("/\n{3,}/", "\n\n", $value); // Collapse runs of blank lines
$value = trim($value); // Delete leading/trailing white space
$value = str_replace("\\","",$value); // Delete all slashes
$value = str_replace("%%","%",$value);
$value = str_replace("&","&",$value); // Fixes MDL-9248
$value = str_replace("<","<",$value);
$value = str_replace(">",">",$value);
$value = str_replace('"',""",$value);
return $value;
}