MDL-21694 fixed regression caused by moving langs to tinymce editor plugin
[moodle.git] / lib / editor / tinymce / extra / strings.php
CommitLineData
5ca3c838 1<?php
2
8fd57583 3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * On-the-fly conversion of Moodle lang strings to TinyMCE expected JS format.
20 *
21 * @package moodlecore
22 * @subpackage editor
23 * @copyright 2009 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
5ca3c838 26
27define('NO_MOODLE_COOKIES', true);
28define('NO_UPGRADE_CHECK', true);
29
30require_once('../../../../config.php');
31
58aa4c08 32$lang = optional_param('elanguage', 'en', PARAM_SAFEDIR);
33$theme = optional_param('etheme', 'advanced', PARAM_SAFEDIR);
5ca3c838 34
1453bf77 35if ($lang === 'en') {
5ca3c838 36 //ok
1453bf77 37} else if (file_exists($CFG->langotherroot .'/'. $lang)) {
3a915b06 38 //$lang = $lang;
5ca3c838 39} else {
3a915b06 40 $lang = 'en';
5ca3c838 41}
42
43// load english defaults
44$string = array();
1453bf77 45foreach (get_langpack_en_locations() as $location) {
5ca3c838 46 if (!file_exists($location)) {
47 continue;
48 }
43d91e34 49 include($location);
5ca3c838 50}
51
52// find parent language
53if ($parent = get_parent_language($lang)) {
54 foreach (get_langpack_locations($parent) as $location) {
55 if (!file_exists($location)) {
56 continue;
57 }
43d91e34 58 include($location);
5ca3c838 59 }
60}
61
62// load wanted language
3a915b06 63if ($lang !== 'en') {
5ca3c838 64 foreach (get_langpack_locations($lang) as $location) {
65 if (!file_exists($location)) {
66 continue;
67 }
43d91e34 68 include($location);
5ca3c838 69 }
70}
71
72//process the $strings to match expected tinymce lang array stucture
f3920b67 73$result = array();
5ca3c838 74
75foreach ($string as $key=>$value) {
f3920b67 76 $parts = explode(':', $key);
77 if (count($parts) != 2) {
5ca3c838 78 // incorrect string - ignore
79 continue;
80 }
6eca5390 81 $value = str_replace("%%","%",$value); // Unescape % characters
82
f3920b67 83 $result[$parts[0]][$parts[1]] = $value;
5ca3c838 84}
85
5f81eeea 86$output = 'tinyMCE.addI18n({'.$lang.':'.json_encode($result).'});';
5ca3c838 87
88$lifetime = '10'; // TODO: increase later
89@header('Content-type: text/javascript; charset=utf-8');
90@header('Content-length: '.strlen($output));
91@header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
92@header('Cache-control: max-age='.$lifetime);
93@header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT');
94@header('Pragma: ');
95
96echo $output;
97
98
99/// ======= Functions =================
100
1453bf77
PS
101function get_langpack_en_locations() {
102 global $CFG;
103
104 $result = array();
105 $result[] = "$CFG->dirroot/lib/editor/tinymce/lang/en/editor_tinymce.php";
106 $result[] = "$CFG->langlocalroot/en_local/editor_tinymce.php";
107
108 return $result;
109}
110
5ca3c838 111function get_langpack_locations($lang) {
112 global $CFG;
113
114 $result = array();
1453bf77
PS
115 $result[] = "$CFG->langotherroot/$lang/editor_tinymce.php";
116 $result[] = "$CFG->langlocalroot/{$lang}_local/editor_tinymce.php";
5ca3c838 117
118 return $result;
119}