Source for file LangEngine.class.php
Documentation is available at LangEngine.class.php
* ManiaLib provides a simple way to internationalize your application. The good
* news is it uses the same dictionary format as the dictionary feature implemented
* in ManiaLinks. The advantage of using the internationalization features of
* ManiaLib over classic Manialink dictionaries is the ability of making dynamic
* sentences (eg. "Hello 'login'!" where login is from a variable).
* First, make sure that the parameter "lang" is in the session. To achieve that,
* you need to use a link with "addplayerid" and then save the parameter in the
* session. If you use the MVC framework, you can add the RegisterRequestParametersFilter
* to your controller (it will look for the lang parameter in the URL and save
* it automatically in the session).
* Then, put your dictionary files in the APP_LANGS_PATH directory. You must use
* same structure as classic Manialink dictionary, but you can add placeholders
* for variables using '[1]', '[2]', etc. (without the quotes).
* <hello_world>Hello World!</hello_world>
* <hello_login>Hello [1] !</hello_login>
* <the_xx_is_yy>The [1] is [2].</the_xx_is_yy>
* Then you can use the translations in your views using the __() function:
* __("hello_world"); // returns "Hello world!"
* __("hello_login", "gou1"); // Returns "Hello gou1!"
* __("the_xx_is_yy", "car", "blue"); // Returns "The car is blue."
* __("Bla bla bla"); // Returns "Bla bla bla", ie. the word ID itself when it is not found.
* You can also internationalize dates:
* __date($timestamp); // Returns, for example: "Friday, July 3rd 2009"
* @copyright 2009-2010 NADEO
* @subpackage Internationalization
* You shouldn't have to do anything with it. Use "__()" & "__date()" instead
* @subpackage Internationalization
protected $currentLang =
"en";
protected static $instance;
* Get the translation of the given text ID
public static function getTranslation($textId, $lang=
null)
$instance =
self::getInstance();
return $instance->getTranslationPrivate($textId, $instance->currentLang);
public static function getInstance()
self::$instance =
new $class();
protected function __construct()
throw
new Exception('LANG_ENGINE_MODE_STATIC can not be used with the LangEngine');
$this->currentLang =
$session->get("lang", "en");
$session->set(__CLASS__
, (serialize($this->dico)));
* Recursive loading method
protected function loadDicoRecursive($directoryPath)
if ($handle =
opendir($directoryPath))
while (false !==
($file =
readdir($handle)))
if(is_dir($directoryPath.
$file))
$this->loadDicoRecursive($directoryPath.
$file);
elseif(substr($file, -
4)==
".xml")
$this->parseLangFile($directoryPath.
"/".
$file);
protected function parseLangFile($file)
$languages =
$dom->getElementsByTagName("language");
foreach($languages as $language)
if($language->hasAttribute("id"))
$lang =
$language->getAttribute("id");
foreach($language->childNodes as $word)
if($word->nodeType ==
XML_ELEMENT_NODE)
$this->dico[$lang][$word->nodeName] = (string)
$word->nodeValue;
* Get the transaltion of the given text ID
protected function getTranslationPrivate($textId, $lang=
"en")
if(isset
($this->dico[$lang][$textId]))
return $this->dico[$lang][$textId];
elseif(isset
($this->dico["en"][$textId]))
return $this->dico["en"][$textId];
* To use with APP_LANG_ENGINE_MODE_DYNAMIC
* echo __("hello_world");
* echo __("hello_login", $someLogin);
$str =
LangEngine::getTranslation($textId);
while(strpos($str, "[$i]")!==
false)
* To use with APP_LANG_ENGINE_MODE_DYNAMIC
* @param int Unix timestamp
date("j", $timestamp), // Day number
__( "date_ordinal_suffix", date("S", $timestamp) ), // Suffix
date("Y", $timestamp) // Year
return date("Y/M/j", $timestamp);