|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 defined('MOODLE_INTERNAL') || die(); 00027 00028 // Given XML multilinguage text, return relevant text according to 00029 // current language: 00030 // - look for multilang blocks in the text. 00031 // - if there exists texts in the currently active language, print them. 00032 // - else, if there exists texts in the current parent language, print them. 00033 // - else, print the first language in the text. 00034 // Please note that English texts are not used as default anymore! 00035 // 00036 // This version is based on original multilang filter by Gaetan Frenoy, 00037 // rewritten by Eloy and skodak. 00038 // 00039 // Following new syntax is not compatible with old one: 00040 // <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span> 00041 00042 class filter_multilang extends moodle_text_filter { 00043 function filter($text, array $options = array()) { 00044 global $CFG; 00045 00046 // [pj] I don't know about you but I find this new implementation funny :P 00047 // [skodak] I was laughing while rewriting it ;-) 00048 // [nicolasconnault] Should support inverted attributes: <span class="multilang" lang="en"> (Doesn't work curently) 00049 // [skodak] it supports it now, though it is slower - any better idea? 00050 00051 if (empty($text) or is_numeric($text)) { 00052 return $text; 00053 } 00054 00055 if (empty($CFG->filter_multilang_force_old) and !empty($CFG->filter_multilang_converted)) { 00056 // new syntax 00057 $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'; 00058 } else { 00059 // old syntax 00060 $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.*?<\/(?:lang|span)>)+/is'; 00061 } 00062 00063 $result = preg_replace_callback($search, 'filter_multilang_impl', $text); 00064 00065 if (is_null($result)) { 00066 return $text; //error during regex processing (too many nested spans?) 00067 } else { 00068 return $result; 00069 } 00070 } 00071 } 00072 00073 function filter_multilang_impl($langblock) { 00074 global $CFG; 00075 00076 $mylang = current_language(); 00077 static $parentcache; 00078 if (!isset($parentcache)) { 00079 $parentcache = array(); 00080 } 00081 if (!array_key_exists($mylang, $parentcache)) { 00082 $parentlang = get_parent_language($mylang); 00083 $parentcache[$mylang] = $parentlang; 00084 } else { 00085 $parentlang = $parentcache[$mylang]; 00086 } 00087 00088 $searchtosplit = '/<(?:lang|span)[^>]+lang="([a-zA-Z0-9_-]+)"[^>]*>(.*?)<\/(?:lang|span)>/is'; 00089 00090 if (!preg_match_all($searchtosplit, $langblock[0], $rawlanglist)) { 00091 //skip malformed blocks 00092 return $langblock[0]; 00093 } 00094 00095 $langlist = array(); 00096 foreach ($rawlanglist[1] as $index=>$lang) { 00097 $lang = str_replace('-','_',strtolower($lang)); // normalize languages 00098 $langlist[$lang] = $rawlanglist[2][$index]; 00099 } 00100 00101 if (array_key_exists($mylang, $langlist)) { 00102 return $langlist[$mylang]; 00103 } else if (array_key_exists($parentlang, $langlist)) { 00104 return $langlist[$parentlang]; 00105 } else { 00106 $first = array_shift($langlist); 00107 return $first; 00108 } 00109 } 00110 00111