]> git.mjollnir.org Git - moodle.git/commitdiff
New release of the multilang filter:
authorstronk7 <stronk7>
Sun, 7 Nov 2004 17:11:57 +0000 (17:11 +0000)
committerstronk7 <stronk7>
Sun, 7 Nov 2004 17:11:57 +0000 (17:11 +0000)
    - 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!

Merged from MOODLE_14_STABLE

filter/multilang/README.txt
filter/multilang/filter.php

index 1c35e5c9fe6eaa41c5995a5c0097d0c595b2a4fc..1f4eedc198e115463c64c25e4921774a01fb197b 100644 (file)
@@ -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
index 833d8b44107b459ed54effd71998cdbbe3a1f3c9..66c81c00fd3b1860f9b4153f12530d302932fdfe 100644 (file)
-<?php\r
-///////////////////////////////////////////////////////////////////////////\r
-//                                                                       //\r
-// This program is part of Moodle - Modular Object-Oriented Dynamic      //\r
-// Learning Environment - http://moodle.com                              //\r
-//                                                                       //\r
-// $Id$ //\r
-//                                                                       //\r
-// Copyright (C) 2004  Gaëtan Frenoy <gaetan à frenoy.net>               //\r
-//                                                                       //\r
-// This program is free software; you can redistribute it and/or modify  //\r
-// it under the terms of the GNU General Public License as published by  //\r
-// the Free Software Foundation; either version 2 of the License, or     //\r
-// (at your option) any later version.                                   //\r
-//                                                                       //\r
-// This program is distributed in the hope that it will be useful,       //\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of        //\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //\r
-// GNU General Public License for more details:                          //\r
-//                                                                       //\r
-//          http://www.gnu.org/copyleft/gpl.html                         //\r
-//                                                                       //\r
-///////////////////////////////////////////////////////////////////////////\r
-//                                                                       //\r
-//  To activate this filter, add a line like this to your                //\r
-//  list of filters in your Filter configuration:                        //\r
-//                                                                       //\r
-//       filter/multilang/filter.php                                     //\r
-//                                                                       //\r
-// See README.txt for more information about this module                 //\r
-//                                                                       //\r
-///////////////////////////////////////////////////////////////////////////\r
-\r
-\r
-/// Given XML multilinguage text, return relevant text according to\r
-/// current language.  i.e.=\r
-///   - look for lang sections in the code.\r
-///   - if there exists texts in the currently active language, print them.\r
-///   - else, if there exists texts in the current parent language, print them.\r
-///   - else, if there are English texts, print them\r
-///   - else, print everything.\r
-///\r
-/// $text is raw unmodified user text\r
-\r
-function multilang_filter($courseid, $text) {\r
-\r
-    global $CFG;\r
-\r
-/// Make sure XML is enabled in this PHP\r
-    if (!function_exists('xml_parser_create')) {\r
-        return $text;\r
-    }\r
-\r
-/// Do a quick check using stripos to avoid unnecessary work\r
-    if (stripos($text, '<lang') === false) {\r
-        return $text;\r
-    }\r
-\r
-/// Flag this text as something not to cache\r
-    $CFG->currenttextiscacheable = false;\r
-\r
-/// Get current language\r
-    $currentlanguage = current_language();\r
-\r
-/// Strip all spaces between tags\r
-    $text = eregi_replace(">"."[[:space:]]+"."<","><", $text);\r
-    $text = eregi_replace("&", "&amp;", $text);\r
-\r
-/// Parse XML multilingual file\r
-    $xml = new XMLParser($text);\r
-    $text = $xml->texts['en'];\r
-    foreach ($xml->texts as $lang => $lang_text) {\r
-        if ($lang == $currentlanguage) $text = $lang_text;\r
-    }\r
-\r
-    return $text;\r
-}\r
-\r
-\r
-\r
-/// XML Parser for Multilingual text\r
-/// Search for "<LANG>" tags and stores inner xml into $this->texts[lang]\r
-///\r
-class XMLParser { \r
-  /// Currently parsed language\r
-  var $current_lang = NULL;\r
-  /// Currently parsed format\r
-  var $current_format = NULL;\r
-  /// Currently parsed text\r
-  var $current_text = NULL;\r
-  /// List of parsed texts so far\r
-  var $texts = NULL;\r
-\r
-/// Constructor\r
-  function XMLParser($data) {\r
-    /// Init member variables\r
-    $this->current_lang = NULL;\r
-    $this->current_format = NULL;\r
-    $this->current_text = '';\r
-    $this->texts = array();\r
-\r
-    /// Default text for default language is input data\r
-    $this->texts['en'] = $data;\r
-\r
-    /// Create parser\r
-    $xml_parser = xml_parser_create(''); \r
-    xml_set_object($xml_parser, &$this); \r
-    xml_set_element_handler($xml_parser, 'startElement', 'endElement'); \r
-    xml_set_character_data_handler($xml_parser, 'characterData'); \r
-    /// Parse date (embed data around dummy tag so parser will receive \r
-    /// a complete XML document\r
-    if (!xml_parse($xml_parser, '<moodle_xml_text_20040116>'.$data.'</moodle_xml_text_20040116>', true))\r
-    {\r
-      /*\r
-      die(sprintf("XML error: %s at line %d",\r
-                 xml_error_string(xml_get_error_code($xml_parser)),\r
-                 xml_get_current_line_number($xml_parser)));\r
-      */\r
-    }\r
-    /// Free resource\r
-    xml_parser_free($xml_parser); \r
-  }\r
-\r
-/// Callback on start of an XML element\r
-  function startElement($parser, $tag, $attributeList) {\r
-    if ($tag == 'LANG' && is_null($this->current_lang))\r
-    {\r
-      // <LANG> tag found, initialise current member vars\r
-      // default language is 'en'\r
-      $this->current_lang = array_key_exists("LANG", $attributeList)?strtolower($attributeList['LANG']):'en';\r
-      // default format is 'auto'\r
-      $this->current_format = array_key_exists('FORMAT', $attributeList)?strtolower($attributeList['FORMAT']):'auto';\r
-      // init inner xml\r
-      $this->current_text = '';\r
-    }\r
-    elseif (!is_null($this->current_lang))\r
-    {\r
-      // If a language has been found already, process tag and attributes\r
-      // and add it to inner xml for current language\r
-      $this->current_text .= "<{$tag}";\r
-      foreach ($attributeList as $key => $val) {\r
-       $this->current_text .= " {$key}=\"{$val}\"";\r
-      }\r
-      $this->current_text .= ">";\r
-    }\r
-    else {\r
-      // This code is outside any <LANG> tag, text is probably not\r
-      // a valid multilingual format\r
-    }\r
-  } \r
-  \r
-/// Callback on end of an XML element\r
-  function endElement($parser, $tag) { \r
-    if ($tag == 'LANG' && !is_null($this->current_lang)) {\r
-      // <LANG> tag found while <LANG> tag was already open, \r
-      // store inner xml and reset context\r
-      $this->texts[$this->current_lang] = $this->current_text;\r
-      $this->current_text = '';\r
-      $this->current_lang = NULL;\r
-      $this->current_format = NULL;\r
-    }\r
-    elseif (!is_null($this->current_lang)) {\r
-      // If a language has been found already, process tag\r
-      // and add it to inner xml for current language\r
-      $this->current_text .= "</{$tag}>";\r
-    }\r
-    else {\r
-      // This code is outside any <LANG> tag, text is probably not\r
-      // a valid multilingual format\r
-    }\r
-  } \r
-  \r
-/// Callback on character data\r
-  function characterData($parser, $data) {\r
-    // Process inner text and add it to current inner xml\r
-    $this->current_text .= $data;\r
-  } \r
-} \r
-\r
-?>\r
+<?php //$Id$
+
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// This program is part of Moodle - Modular Object-Oriented Dynamic      //
+// 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  //
+// 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                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+// 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;
+
+/// Do a quick check using stripos to avoid unnecessary work
+    if (stripos($text, '<lang') === false) {
+        return $text;
+    }
+
+/// Flag this text as something not to cache
+    $CFG->currenttextiscacheable = false;
+
+/// Get current language
+    $currentlang = current_language();
+
+/// Get parent language
+    $langfile = "$CFG->dirroot/lang/$currentlang/moodle.php";
+    if ($result = get_string_from_file("parentlanguage", "$langfile", "\$parentlang")) {
+        eval($result);
+    }
+
+/// 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
+    }
+    if ($currentlang != 'en') {
+        $preflangs[] = 'en';        /// Finally, if not used, add the en lang
+    }
+
+/// 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.
+            }
+        }
+    }
+
+    $text = trim($list_of_langs[2][$bestkey]);
+
+    return $text;
+}
+
+?>