Notices from T/F question is you submit without answering in adaptive mode. Merged...
[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
47 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
48 // new syntax
244691e2 49 $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 50 } else {
51 // old syntax
bbb5ac9a 52 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
f16242ce 53 }
fde4a094 54 return preg_replace_callback($search, 'multilang_filter_impl', $text);
55}
71c235bc 56
fde4a094 57function multilang_filter_impl($langblock) {
f16242ce 58 global $CFG;
59
9fecfdfb 60 $mylang = str_replace('_utf8', '', current_language());
61 static $parentcache;
62 if (!isset($parentcache)) {
63 $parentcache = array();
64 }
65 if (!array_key_exists($mylang, $parentcache)) {
66 $parentlang = str_replace('_utf8', '', get_string('parentlanguage'));
67 $parentcache[$mylang] = $parentlang;
68 } else {
69 $parentlang = $parentcache[$mylang];
71c235bc 70 }
fde4a094 71
244691e2 72 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
f16242ce 73
244691e2 74 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
75 //skip malformed blocks
76 return $langblock[0];
77 }
9fecfdfb 78
79 $langlist = array();
80 foreach ($rawlanglist[1] as $index=>$lang) {
81 $lang = str_replace('_utf8', '', str_replace('-','_',strtolower($lang))); // normalize languages
82 $langlist[$lang] = $rawlanglist[2][$index];
71c235bc 83 }
84
9fecfdfb 85 if (array_key_exists($mylang, $langlist)) {
86 return $langlist[$mylang];
87 } else if (array_key_exists($parentlang, $langlist)) {
88 return $langlist[$parentlang];
89 } else {
90 $first = array_shift($langlist);
91 return $first;
92 }
71c235bc 93}
94
95?>