MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[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
ccc161f8 39class multilang_filter extends moodle_text_filter {
9e3f34d1 40 function filter($text) {
41 global $CFG;
f16242ce 42
9e3f34d1 43 // [pj] I don't know about you but I find this new implementation funny :P
44 // [skodak] I was laughing while rewriting it ;-)
45 // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently)
46 // [skodak] it supports it now, though it is slower - any better idea?
af2fc69d 47
9e3f34d1 48 if (empty($text) or is_numeric($text)) {
49 return $text;
50 }
51
52 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) {
53 // new syntax
54 $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';
55 } else {
56 // old syntax
57 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is';
58 }
af2fc69d 59
9e3f34d1 60 $result = preg_replace_callback($search, 'multilang_filter_impl', $text);
af2fc69d 61
9e3f34d1 62 if (is_null($result)) {
63 return $text; //error during regex processing (too many nested spans?)
64 } else {
65 return $result;
66 }
af2fc69d 67 }
fde4a094 68}
71c235bc 69
fde4a094 70function multilang_filter_impl($langblock) {
f16242ce 71 global $CFG;
72
9fecfdfb 73 $mylang = str_replace('_utf8', '', current_language());
74 static $parentcache;
75 if (!isset($parentcache)) {
76 $parentcache = array();
77 }
78 if (!array_key_exists($mylang, $parentcache)) {
79 $parentlang = str_replace('_utf8', '', get_string('parentlanguage'));
80 $parentcache[$mylang] = $parentlang;
81 } else {
82 $parentlang = $parentcache[$mylang];
71c235bc 83 }
fde4a094 84
244691e2 85 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is';
f16242ce 86
244691e2 87 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) {
88 //skip malformed blocks
89 return $langblock[0];
90 }
9fecfdfb 91
92 $langlist = array();
93 foreach ($rawlanglist[1] as $index=>$lang) {
94 $lang = str_replace('_utf8', '', str_replace('-','_',strtolower($lang))); // normalize languages
95 $langlist[$lang] = $rawlanglist[2][$index];
71c235bc 96 }
97
9fecfdfb 98 if (array_key_exists($mylang, $langlist)) {
99 return $langlist[$mylang];
100 } else if (array_key_exists($parentlang, $langlist)) {
101 return $langlist[$parentlang];
102 } else {
103 $first = array_shift($langlist);
104 return $first;
105 }
71c235bc 106}
107
108?>