MDL-15683, get query string instead of calling me().
[moodle-linuxchix.git] / filter / multilang / filter.php
blob7da88d6a500ec63814771ae15626fa00a2dadeca
1 <?php //$Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // This program is part of Moodle - Modular Object-Oriented Dynamic //
6 // Learning Environment - http://moodle.org //
7 // //
8 // Copyright (C) 2004 Gaetan Frenoy <gaetan@frenoy.net> //
9 // Eloy Lafuente <stronk7@moodle.org> //
10 // //
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. //
15 // //
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: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
25 // Given XML multilinguage text, return relevant text according to
26 // current language:
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) {
40 global $CFG;
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)) {
48 return $text;
51 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
52 // new syntax
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';
54 } else {
55 // old syntax
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?)
63 } else {
64 return $result;
68 function multilang_filter_impl($langblock) {
69 global $CFG;
71 $mylang = str_replace('_utf8', '', current_language());
72 static $parentcache;
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;
79 } else {
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
87 return $langblock[0];
90 $langlist = array();
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];
100 } else {
101 $first = array_shift($langlist);
102 return $first;