Merge branch 'MDL-51402' of git://github.com/timhunt/moodle
[moodle.git] / filter / multilang / filter.php
blobefb618f705b220e919280fe542a53bab2cefacff
1 <?php
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.
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/>.
18 /**
19 * @package filter
20 * @subpackage multilang
21 * @copyright Gaetan Frenoy <gaetan@frenoy.net>
22 * @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 // Given XML multilinguage text, return relevant text according to
29 // current language:
30 // - look for multilang blocks in the text.
31 // - if there exists texts in the currently active language, print them.
32 // - else, if there exists texts in the current parent language, print them.
33 // - else, print the first language in the text.
34 // Please note that English texts are not used as default anymore!
36 // This version is based on original multilang filter by Gaetan Frenoy,
37 // rewritten by Eloy and skodak.
39 // Following new syntax is not compatible with old one:
40 // <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>
42 class filter_multilang extends moodle_text_filter {
43 function filter($text, array $options = array()) {
44 global $CFG;
46 // [pj] I don't know about you but I find this new implementation funny :P
47 // [skodak] I was laughing while rewriting it ;-)
48 // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently)
49 // [skodak] it supports it now, though it is slower - any better idea?
51 if (empty($text) or is_numeric($text)) {
52 return $text;
55 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
56 // new syntax
57 $search = '/(<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)(\s*<span(\s+lang="[a-zA-Z0-9_-]+"|\s+class="multilang"){2}\s*>.*?<\/span>)+/is';
58 } else {
59 // old syntax
60 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
63 $result = preg_replace_callback($search, 'filter_multilang_impl', $text);
65 if (is_null($result)) {
66 return $text; //error during regex processing (too many nested spans?)
67 } else {
68 return $result;
73 function filter_multilang_impl($langblock) {
74 global $CFG;
76 $mylang = current_language();
77 static $parentcache;
78 if (!isset($parentcache)) {
79 $parentcache = array();
81 if (!array_key_exists($mylang, $parentcache)) {
82 $parentlang = get_parent_language($mylang);
83 $parentcache[$mylang] = $parentlang;
84 } else {
85 $parentlang = $parentcache[$mylang];
88 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
90 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
91 //skip malformed blocks
92 return $langblock[0];
95 $langlist = array();
96 foreach ($rawlanglist[1] as $index=>$lang) {
97 $lang = str_replace('-','_',strtolower($lang)); // normalize languages
98 $langlist[$lang] = $rawlanglist[2][$index];
101 if (array_key_exists($mylang, $langlist)) {
102 return $langlist[$mylang];
103 } else if (array_key_exists($parentlang, $langlist)) {
104 return $langlist[$parentlang];
105 } else {
106 $first = array_shift($langlist);
107 return $first;