Permalink
Browse files

New release of the multilang filter:

    - 100% compatible with previous "Multilang" filter.
    - Supports a new "short" syntax to make things easier. Simply use:
        <lang lang="XX">
    - It needs less resources and executes faster.
    - It Allows any type of content to be used. No restrictions at all!
  • Loading branch information...
stronk7
stronk7 committed Nov 7, 2004
1 parent 742960a commit f94c9601f0ce6ccc4cc4eb6831da800b7dff13e7
Showing with 67 additions and 188 deletions.
  1. +14 −50 filter/multilang/README.txt
  2. +53 −138 filter/multilang/filter.php
@@ -1,50 +1,14 @@
-///////////////////////////////////////////////////////////////////////////
-// This program is part of Moodle - Modular Object-Oriented Dynamic //
-// Learning Environment - http://moodle.com //
-// //
-// Multilingual Filter //
-// $Id$ //
-// //
-///////////////////////////////////////////////////////////////////////////
-
-This filter implements an XML syntax to provide
-fully multilingual website. No only the Moodle interface
-can be translated but also the content!
-
-To activate this filter, add a line like this to your
-config.php:
-
- $CFG->textfilter1 = 'filter/multilang/multilang.php';
-
-Syntax to display a multilingual content:
-
- <lang lang="en" format="auto">
- Introduction
- </lang>
-
- <lang lang="es" format="auto">
- Introducci�n
- </lang>
-
- <lang lang="de" format="auto">
- Einleitung
- </lang>
-
-
-///////////////////////////////////////////////////////////////////////////
-// //
-// Copyright (C) 2004 Ga�tan Frenoy <gaetan � frenoy.net> //
-// //
-// This program is free software; you can redistribute it and/or modify //
-// it under the terms of the GNU General Public License as published by //
-// the Free Software Foundation; either version 2 of the License, or //
-// (at your option) any later version. //
-// //
-// This program is distributed in the hope that it will be useful, //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
-// GNU General Public License for more details: //
-// //
-// http://www.gnu.org/copyleft/gpl.html //
-// //
-///////////////////////////////////////////////////////////////////////////
+$Id$
+
+To Install it:
+ - Enable if from "Administration/Filters".
+
+To Use it:
+ - Create your contents into multiple languages.
+ - Enclose every language content between:
+ <lang lang="XX>your_content_here</lang>
+ - Test it (by changing your language).
+
+Ciao, Eloy :-)
+stronk7@moodle.org
+2004-11-07
View
@@ -1,12 +1,12 @@
-<?php
+<?php //$Id$
+
///////////////////////////////////////////////////////////////////////////
// //
// This program is part of Moodle - Modular Object-Oriented Dynamic //
-// Learning Environment - http://moodle.com //
-// //
-// $Id$ //
+// Learning Environment - http://moodle.org //
// //
// Copyright (C) 2004 Gaëtan Frenoy <gaetan à frenoy.net> //
+// Eloy Lafuente <stronk7@moodle.org> //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
@@ -21,36 +21,26 @@
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
-// //
-// To activate this filter, add a line like this to your //
-// list of filters in your Filter configuration: //
-// //
-// filter/multilang/filter.php //
-// //
-// See README.txt for more information about this module //
-// //
-///////////////////////////////////////////////////////////////////////////
-
-/// Given XML multilinguage text, return relevant text according to
-/// current language. i.e.=
-/// - 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 everything.
-///
-/// $text is raw unmodified user text
+// Given XML multilinguage text, return relevant text according to
+// current language. i.e.=
+// - 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.
+//
+// This is an improved version of the original multilang filter by Gaëtan 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:
+// <lang lang="XX">
+// - Needs less resources and executes faster.
+// - Allows any type of content to be used. No restrictions at all!
function multilang_filter($courseid, $text) {
global $CFG;
-/// Make sure XML is enabled in this PHP
- if (!function_exists('xml_parser_create')) {
- return $text;
- }
-
/// Do a quick check using stripos to avoid unnecessary work
if (stripos($text, '<lang') === false) {
return $text;
@@ -60,121 +50,46 @@ function multilang_filter($courseid, $text) {
$CFG->currenttextiscacheable = false;
/// Get current language
- $currentlanguage = current_language();
-
-/// Strip all spaces between tags
- $text = eregi_replace(">"."[[:space:]]+"."<","><", $text);
- $text = eregi_replace("&", "&amp;", $text);
-
-/// Parse XML multilingual file
- $xml = new XMLParser($text);
- $text = $xml->texts['en'];
- foreach ($xml->texts as $lang => $lang_text) {
- if ($lang == $currentlanguage) $text = $lang_text;
- }
-
- return $text;
-}
-
-
+ $currentlang = current_language();
-/// XML Parser for Multilingual text
-/// Search for "<LANG>" tags and stores inner xml into $this->texts[lang]
-///
-class XMLParser {
- /// Currently parsed language
- var $current_lang = NULL;
- /// Currently parsed format
- var $current_format = NULL;
- /// Currently parsed text
- var $current_text = NULL;
- /// List of parsed texts so far
- var $texts = NULL;
-
-/// Constructor
- function XMLParser($data) {
- /// Init member variables
- $this->current_lang = NULL;
- $this->current_format = NULL;
- $this->current_text = '';
- $this->texts = array();
-
- /// Default text for default language is input data
- $this->texts['en'] = $data;
-
- /// Create parser
- $xml_parser = xml_parser_create('');
- xml_set_object($xml_parser, &$this);
- xml_set_element_handler($xml_parser, 'startElement', 'endElement');
- xml_set_character_data_handler($xml_parser, 'characterData');
- /// Parse date (embed data around dummy tag so parser will receive
- /// a complete XML document
- if (!xml_parse($xml_parser, '<moodle_xml_text_20040116>'.$data.'</moodle_xml_text_20040116>', true))
- {
- /*
- die(sprintf("XML error: %s at line %d",
- xml_error_string(xml_get_error_code($xml_parser)),
- xml_get_current_line_number($xml_parser)));
- */
+/// Get parent language
+ $langfile = "$CFG->dirroot/lang/$currentlang/moodle.php";
+ if ($result = get_string_from_file("parentlanguage", "$langfile", "\$parentlang")) {
+ eval($result);
}
- /// Free resource
- xml_parser_free($xml_parser);
- }
-/// Callback on start of an XML element
- function startElement($parser, $tag, $attributeList) {
- if ($tag == 'LANG' && is_null($this->current_lang))
- {
- // <LANG> tag found, initialise current member vars
- // default language is 'en'
- $this->current_lang = array_key_exists("LANG", $attributeList)?strtolower($attributeList['LANG']):'en';
- // default format is 'auto'
- $this->current_format = array_key_exists('FORMAT', $attributeList)?strtolower($attributeList['FORMAT']):'auto';
- // init inner xml
- $this->current_text = '';
- }
- elseif (!is_null($this->current_lang))
- {
- // If a language has been found already, process tag and attributes
- // and add it to inner xml for current language
- $this->current_text .= "<{$tag}";
- foreach ($attributeList as $key => $val) {
- $this->current_text .= " {$key}=\"{$val}\"";
- }
- $this->current_text .= ">";
+/// Create an array of preffered languages
+ $preflangs = array();
+ $preflangs[] = $currentlang; /// First, the current lang
+ if (!empty($parentlang)) {
+ $preflangs[] = $parentlang; /// Then, if available, the parent lang
}
- else {
- // This code is outside any <LANG> tag, text is probably not
- // a valid multilingual format
+ if ($currentlang != 'en') {
+ $preflangs[] = 'en'; /// Finally, if not used, add the en lang
}
- }
-
-/// Callback on end of an XML element
- function endElement($parser, $tag) {
- if ($tag == 'LANG' && !is_null($this->current_lang)) {
- // <LANG> tag found while <LANG> tag was already open,
- // store inner xml and reset context
- $this->texts[$this->current_lang] = $this->current_text;
- $this->current_text = '';
- $this->current_lang = NULL;
- $this->current_format = NULL;
- }
- elseif (!is_null($this->current_lang)) {
- // If a language has been found already, process tag
- // and add it to inner xml for current language
- $this->current_text .= "</{$tag}>";
- }
- else {
- // This code is outside any <LANG> tag, text is probably not
- // a valid multilingual format
+
+/// Break the text into lang sections
+ $search = '/<lang lang="([a-zA-Z_]*)".*?>(.+?)<\/lang>/is';
+ preg_match_all($search,$text,$list_of_langs);
+
+/// Get the existing sections langs
+ $minpref = count($preflangs);
+ $bestkey = 0;
+ //Iterate
+ foreach ($list_of_langs[1] as $key => $lang) {
+ $foundkey = array_search($lang, $preflangs);
+ if ($foundkey !== false && $foundkey !== NULL && $foundkey < $minpref) {
+ $minpref = $foundkey;
+ $bestkey = $key;
+ if ($minpref == 0) {
+ continue; //The best has been found. Leave iteration.
+ }
+ }
}
- }
-
-/// Callback on character data
- function characterData($parser, $data) {
- // Process inner text and add it to current inner xml
- $this->current_text .= $data;
- }
-}
+
+ $text = trim($list_of_langs[2][$bestkey]);
+
+ return $text;
+}
?>

0 comments on commit f94c960

Please sign in to comment.