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