From 9fecfdfb832c7a936f6f0841fd4e53e14e376e13 Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 14 Aug 2006 07:46:03 +0000 Subject: [PATCH] Bug #6245 - rewritten multilang filter --- filter/multilang/README.txt | 1 - filter/multilang/filter.php | 104 ++++++++++-------------------------- 2 files changed, 28 insertions(+), 77 deletions(-) diff --git a/filter/multilang/README.txt b/filter/multilang/README.txt index 6c4b1e3a5f..5e7beffcbc 100644 --- a/filter/multilang/README.txt +++ b/filter/multilang/README.txt @@ -14,7 +14,6 @@ How it works: - for each "lang block": - if there are texts in the currently active language, print them. - else, if there exists texts in the current parent language, print them. - - else, if there are English texts, print them - else, print the first language found in the text. - text out of "lang blocks" will be showed always. diff --git a/filter/multilang/filter.php b/filter/multilang/filter.php index 3360f02c2a..35a732ce8d 100644 --- a/filter/multilang/filter.php +++ b/filter/multilang/filter.php @@ -5,7 +5,7 @@ // This program is part of Moodle - Modular Object-Oriented Dynamic // // Learning Environment - http://moodle.org // // // -// Copyright (C) 2004 Gaëtan Frenoy // +// Copyright (C) 2004 Gaetan Frenoy // // Eloy Lafuente // // // // This program is free software; you can redistribute it and/or modify // @@ -27,10 +27,10 @@ // - look for lang sections in the code. // - if there exists texts in the currently active language, print them. // - else, if there exists texts in the current parent language, print them. -// - else, if there are English texts, print them // - else, print the first language in the text. +// Please note that English texts are not used as default anymore! // -// This is an improved version of the original multilang filter by Gaëtan Frenoy. +// This is an improved version of the original multilang filter by Gaetan Frenoy. // It should be 100% compatible with the original one. Some new features are: // - Supports a new "short" syntax to make things easier. Simply use: // @@ -39,90 +39,42 @@ function multilang_filter($courseid, $text) { -/* -/// Do a quick check using stripos to avoid unnecessary work - if (stripos($text, 'currenttextiscacheable = false; - - if(empty($preflangs)) { - /// Get current languages (utf and simple) - $currentlang = current_language(); - $currentlangsimple = ''; - /// If it's a utf8 lang, use the basic one too - if (strstr($currentlang, '_utf8') !== false) { - $currentlangsimple = str_replace('_utf8', '', $currentlang); - } - - /// Get parent language of $currentlang (utf and simple) - $parentlang = get_string('parentlanguage'); - $parentlangsimple = ''; - if (substr($parentlang, 0, 1) == '[') { - $parentlang = ''; - } - /// If it's a utf8 lang, use the basic one too - if (strstr($parentlang, '_utf8') !== false) { - $parentlangsimple = str_replace('_utf8', '', $parentlang); - } - - /// Fill in the array of preffered languages - $preflangs = array(); - $preflangs[] = $currentlang; /// First, the current lang - if (!empty($currentlangsimple)) { - $preflangs[] = $currentlangsimple; /// The simple (non utf8) lang - } - if (!empty($parentlang)) { - $preflangs[] = $parentlang; /// Then, if available, the parent lang - } - if (!empty($parentlangsimple)) { - $preflangs[] = $parentlangsimple; /// And the simple (non utf8) parent lang - } - if ($currentlang != 'en' && $currentlang != 'en_utf8') { - $preflangs[] = 'en_utf8'; /// Finally, if not used, add the en langs - $preflangs[] = 'en'; - } + $mylang = str_replace('_utf8', '', current_language()); + static $parentcache; + if (!isset($parentcache)) { + $parentcache = array(); + } + if (!array_key_exists($mylang, $parentcache)) { + $parentlang = str_replace('_utf8', '', get_string('parentlanguage')); + $parentcache[$mylang] = $parentlang; + } else { + $parentlang = $parentcache[$mylang]; } - // Setup is done, now do multilang replacement on the match we 've been called for $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is'; - preg_match_all($searchtosplit, $langblock[0], $langlist); - /// Get the existing sections langs - $lang = ''; - $minpref = count($preflangs); - $bestkey = 0; - // Define the preference that will be enough if found - $stoppref = empty($CFG->unicodedb) ? 0 : 1; - //Iterate - foreach ($langlist[1] as $key => $lang) { - //Normalize: Moodle's langs are always lowercase and they use the underscore - //Should we be stricter? - $lang = strtolower(str_replace('-','_',$lang)); - $foundkey = array_search($lang, $preflangs); - if ($foundkey !== false && $foundkey !== NULL && $foundkey < $minpref) { - $minpref = $foundkey; - $bestkey = $key; - if ($minpref <= $stoppref) { - break; //The best has been found. Leave iteration. - } - } + preg_match_all($searchtosplit, $langblock[0], $rawlanglist); + + $langlist = array(); + foreach ($rawlanglist[1] as $index=>$lang) { + $lang = str_replace('_utf8', '', str_replace('-','_',strtolower($lang))); // normalize languages + $langlist[$lang] = $rawlanglist[2][$index]; } - return trim($langlist[2][$bestkey]); + if (array_key_exists($mylang, $langlist)) { + return $langlist[$mylang]; + } else if (array_key_exists($parentlang, $langlist)) { + return $langlist[$parentlang]; + } else { + $first = array_shift($langlist); + return $first; + } } ?> -- 2.39.5