MDL-11561 admin tree improvements and bugfixing
[moodle.git] / filter / multilang / filter.php
CommitLineData
71c235bc 1<?php //$Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// This program is part of Moodle - Modular Object-Oriented Dynamic //
6// Learning Environment - http://moodle.org //
7// //
9fecfdfb 8// Copyright (C) 2004 Gaetan Frenoy <gaetan@frenoy.net> //
71c235bc 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///////////////////////////////////////////////////////////////////////////
24
25// Given XML multilinguage text, return relevant text according to
f16242ce 26// current language:
27// - look for multilang blocks in the text.
71c235bc 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.
71c235bc 30// - else, print the first language in the text.
9fecfdfb 31// Please note that English texts are not used as default anymore!
71c235bc 32//
f16242ce 33// This version is based on original multilang filter by Gaetan Frenoy,
34// rewritten by Eloy and skodak.
35//
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>
71c235bc 38
39function multilang_filter($courseid, $text) {
f16242ce 40 global $CFG;
71c235bc 41
fde4a094 42 // [pj] I don't know about you but I find this new implementation funny :P
9fecfdfb 43 // [skodak] I was laughing while rewriting it ;-)
244691e2 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?
f16242ce 46
af2fc69d 47 if (empty($text) or is_numeric($text)) {
48 return $text;
49 }
50
f16242ce 51 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
52 // new syntax
244691e2 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';
f16242ce 54 } else {
55 // old syntax
bbb5ac9a 56 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
f16242ce 57 }
af2fc69d 58
59 $result = preg_replace_callback($search, 'multilang_filter_impl', $text);
60
61 if (is_null($result)) {
62 return $text; //error during regex processing (too many nested spans?)
63 } else {
64 return $result;
65 }
fde4a094 66}
71c235bc 67
fde4a094 68function multilang_filter_impl($langblock) {
f16242ce 69 global $CFG;
70
9fecfdfb 71 $mylang = str_replace('_utf8', '', current_language());
72 static $parentcache;
73 if (!isset($parentcache)) {
74 $parentcache = array();
75 }
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];
71c235bc 81 }
fde4a094 82
244691e2 83 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
f16242ce 84
244691e2 85 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
86 //skip malformed blocks
87 return $langblock[0];
88 }
9fecfdfb 89
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];
71c235bc 94 }
95
9fecfdfb 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;
103 }
71c235bc 104}
105
106?>