3 ///////////////////////////////////////////////////////////////////////////
5 // This program is part of Moodle - Modular Object-Oriented Dynamic //
6 // Learning Environment - http://moodle.org //
8 // Copyright (C) 2004 Gaetan Frenoy <gaetan@frenoy.net> //
9 // Eloy Lafuente <stronk7@moodle.org> //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 // Given XML multilinguage text, return relevant text according to
27 // - look for multilang blocks in the text.
28 // - if there exists texts in the currently active language, print them.
29 // - else, if there exists texts in the current parent language, print them.
30 // - else, print the first language in the text.
31 // Please note that English texts are not used as default anymore!
33 // This version is based on original multilang filter by Gaetan Frenoy,
34 // rewritten by Eloy and skodak.
36 // Following new syntax is not compatible with old one:
37 // <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>
39 function multilang_filter($courseid, $text) {
42 // [pj] I don't know about you but I find this new implementation funny :P
43 // [skodak] I was laughing while rewriting it ;-)
44 // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently)
45 // [skodak] it supports it now, though it is slower - any better idea?
47 if (empty($text) or is_numeric($text)) {
51 if (empty($CFG->filter_multilang_force_old
) and !empty($CFG->filter_multilang_converted
)) {
53 $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';
56 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
59 $result = preg_replace_callback($search, 'multilang_filter_impl', $text);
61 if (is_null($result)) {
62 return $text; //error during regex processing (too many nested spans?)
68 function multilang_filter_impl($langblock) {
71 $mylang = str_replace('_utf8', '', current_language());
73 if (!isset($parentcache)) {
74 $parentcache = array();
76 if (!array_key_exists($mylang, $parentcache)) {
77 $parentlang = str_replace('_utf8', '', get_string('parentlanguage'));
78 $parentcache[$mylang] = $parentlang;
80 $parentlang = $parentcache[$mylang];
83 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
85 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
86 //skip malformed blocks
91 foreach ($rawlanglist[1] as $index=>$lang) {
92 $lang = str_replace('_utf8', '', str_replace('-','_',strtolower($lang))); // normalize languages
93 $langlist[$lang] = $rawlanglist[2][$index];
96 if (array_key_exists($mylang, $langlist)) {
97 return $langlist[$mylang];
98 } else if (array_key_exists($parentlang, $langlist)) {
99 return $langlist[$parentlang];
101 $first = array_shift($langlist);