ManiaLib 1.0b1 API Reference

Source for file LangEngine.class.php

Documentation is available at LangEngine.class.php

  1. <?php
  2. /**
  3.  * Internationalization
  4.  * 
  5.  * ManiaLib provides a simple way to internationalize your application. The good
  6.  * news is it uses the same dictionary format as the dictionary feature implemented
  7.  * in ManiaLinks. The advantage of using the internationalization features of
  8.  * ManiaLib over classic Manialink dictionaries is the ability of making dynamic
  9.  * sentences (eg. "Hello 'login'!" where login is from a variable).
  10.  * 
  11.  * How to use it?
  12.  * 
  13.  * First, make sure that the parameter "lang" is in the session. To achieve that,
  14.  * you need to use a link with "addplayerid" and then save the parameter in the
  15.  * session. If you use the MVC framework, you can add the RegisterRequestParametersFilter
  16.  * to your controller (it will look for the lang parameter in the URL and save
  17.  * it automatically in the session).
  18.  * 
  19.  * Then, put your dictionary files in the APP_LANGS_PATH directory. You must use
  20.  * same structure as classic Manialink dictionary, but you can add placeholders
  21.  * for variables using '[1]', '[2]', etc. (without the quotes).
  22.  * 
  23.  *  <code>
  24.  *  <?xml version="1.0"?>
  25.  *  <dico>
  26.  *      <language id="en">
  27.  *          <hello_world>Hello World!</hello_world>
  28.  *          <hello_login>Hello [1] !</hello_login>
  29.  *          <the_xx_is_yy>The [1] is [2].</the_xx_is_yy>
  30.  *      </language>
  31.  *  </dico>
  32.  *  </code>
  33.  *  
  34.  *  Then you can use the translations in your views using the __() function:
  35.  *  
  36.  *  <code>
  37.  *  __("hello_world"); // returns "Hello world!"
  38.  *  __("hello_login", "gou1"); // Returns "Hello gou1!"
  39.  *  __("the_xx_is_yy", "car", "blue"); // Returns "The car is blue."
  40.  *  __("Bla bla bla"); // Returns "Bla bla bla", ie. the word ID itself when it is not found.
  41.  *  </code>
  42.  *  
  43.  *  You can also internationalize dates:
  44.  *  
  45.  *  <code>
  46.  *  $timestamp = time();
  47.  *  __date($timestamp); // Returns, for example: "Friday, July 3rd 2009"
  48.  *  </code>
  49.  * 
  50.  * @author Maxime Raoust
  51.  * @copyright 2009-2010 NADEO
  52.  * @package ManiaLib
  53.  * @subpackage Internationalization
  54.  */
  55.  
  56. /**
  57.  * i18n core class
  58.  * You shouldn't have to do anything with it. Use "__()" & "__date()" instead
  59.  * @see __()
  60.  * @package ManiaLib
  61.  * @subpackage Internationalization
  62.  * @ignore
  63.  */
  64. class LangEngine
  65. {
  66.     protected $currentLang "en";
  67.     public $dico;
  68.     protected static $instance;
  69.     
  70.     /**
  71.      * Get the translation of the given text ID
  72.      */
  73.     public static function getTranslation($textId$lang=null)
  74.     {
  75.         $instance self::getInstance();
  76.         return $instance->getTranslationPrivate($textId$instance->currentLang);
  77.     }
  78.         
  79.     public static function getInstance()
  80.     {
  81.         if (!self::$instance)
  82.         {
  83.             $class = __CLASS__;
  84.             self::$instance new $class();
  85.         }
  86.         return self::$instance;
  87.     }
  88.  
  89.     protected function __construct()
  90.     {
  91.         if(APP_LANG_ENGINE_MODE == APP_LANG_ENGINE_MODE_STATIC)
  92.         {
  93.             throw new Exception('LANG_ENGINE_MODE_STATIC can not be used with the LangEngine');
  94.         }
  95.         
  96.         $session SessionEngine::getInstance();
  97.         $this->currentLang $session->get("lang""en");
  98.         
  99.         if(!APP_DEBUG_LEVEL && $dico $session->get(__CLASS__))
  100.         {
  101.             $this->dico unserialize(($dico));
  102.         }
  103.         else
  104.         {
  105.             $this->loadDicoRecursive(APP_FRAMEWORK_LANGS_PATH);
  106.             $this->loadDicoRecursive(APP_LANGS_PATH);
  107.             $session->set(__CLASS__(serialize($this->dico)));
  108.         }
  109.     }
  110.     
  111.     /**
  112.      * Recursive loading method
  113.      */
  114.     protected function loadDicoRecursive($directoryPath)
  115.     {
  116.         if ($handle opendir($directoryPath))
  117.         {
  118.             while (false !== ($file readdir($handle)))
  119.             {
  120.                 if(substr($file01)==".")
  121.                 {
  122.                     continue;
  123.                 }
  124.                 if(is_dir($directoryPath.$file))
  125.                 {
  126.                     $this->loadDicoRecursive($directoryPath.$file);
  127.                 }
  128.                 elseif(substr($file-4)==".xml")
  129.                 {
  130.                     $this->parseLangFile($directoryPath."/".$file);
  131.                 }
  132.             }
  133.             closedir($handle);
  134.         }
  135.     }
  136.     
  137.     /**
  138.      * Parse an XML dictonary
  139.      */
  140.     protected function parseLangFile($file)
  141.     {    
  142.         $dom new DomDocument;
  143.         $dom->load($file);
  144.         $languages $dom->getElementsByTagName("language");
  145.         foreach($languages as $language)
  146.         {
  147.             if($language->hasAttribute("id"))
  148.             {
  149.                 $lang $language->getAttribute("id");
  150.                 foreach($language->childNodes as $word)
  151.                 {
  152.                     if($word->nodeType == XML_ELEMENT_NODE)
  153.                     {
  154.                         $this->dico[$lang][$word->nodeName= (string) $word->nodeValue;
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.     }
  160.     
  161.     /**
  162.      * Get the transaltion of the given text ID
  163.      * @return string 
  164.      */
  165.     protected function getTranslationPrivate($textId$lang="en")
  166.     {
  167.         if(isset($this->dico[$lang][$textId]))
  168.         {
  169.             return $this->dico[$lang][$textId];
  170.         }    
  171.         elseif(isset($this->dico["en"][$textId]))
  172.         {
  173.             return $this->dico["en"][$textId];
  174.         }
  175.         else
  176.         {
  177.             return $textId;
  178.         }
  179.     }
  180.  
  181. }
  182.  
  183. /**
  184.  * i18n message
  185.  * 
  186.  * To use with APP_LANG_ENGINE_MODE_DYNAMIC
  187.  * 
  188.  * Example:
  189.  * <code>
  190.  * echo __("hello_world");
  191.  * echo __("hello_login", $someLogin);
  192.  * </code>
  193.  * @param string 
  194.  * @return string 
  195.  */
  196. function __($textId)
  197. {
  198.     $str LangEngine::getTranslation($textId);
  199.     $i=1;
  200.     $args func_get_args();
  201.     $search array();
  202.     $replace array();
  203.     while(strpos($str"[$i]")!==false)
  204.     {
  205.         $search["[$i]";
  206.         if(isset($args[$i]))
  207.         {
  208.             $replace[$args[$i];
  209.         }
  210.         else
  211.         {
  212.             $replace["";
  213.         }
  214.         $i++;
  215.     }
  216.     $str str_replace($search$replace$str);
  217.     return $str;
  218. }
  219.  
  220. /**
  221.  * i18n date
  222.  * 
  223.  * To use with APP_LANG_ENGINE_MODE_DYNAMIC
  224.  * 
  225.  * @param int Unix timestamp
  226.  * @return string 
  227.  */
  228. function __date($timestamp)
  229. {
  230.     if(!$timestamp)
  231.     {
  232.         return "-";
  233.     }
  234.     
  235.     $return=__("date_long"
  236.                 __strtolower(date("l"$timestamp)) ),             // Day name
  237.                 __strtolower(date("F"$timestamp)) ),             // Month name
  238.                     date("j"$timestamp),                           // Day number
  239.                 __"date_ordinal_suffix"date("S"$timestamp) ),  // Suffix
  240.                     date("Y"$timestamp)                            // Year
  241.                 );                    
  242.     
  243.     if($return=="date_long")
  244.     {
  245.         return date("Y/M/j"$timestamp);
  246.     }
  247.     else
  248.     {
  249.         return $return;
  250.     }
  251. }
  252.  
  253. ?>