<?php
/**
* This class implements a parser for XML file. From XML creates a PHP
* file to be used in localization process. *
*
* @version 1.0.0
* @author Edoardo Tenani <hide@address.com>
* @license GNU Public License 2.0 or greater
* <http://opensource.org/licenses/gpl-2.0.php>
* @copyright Edoardo Tenani
*
* @package cboxmanager
* @subpackage L10n-i18n
* @category L10n
* @since 0.1.2
*/
class L10n {
/**
* The path to the XML language file to be parsed
*
* @access private
* @var string
*/
private $xml_language_file;
/**
* The path to the PHP language file to be created
*
* @access private
* @var string
*/
private $php_language_file;
/**
* The XML object of the XML language file to be parsed
*
* @access private
* @var XMLSimpleObject
*/
private $xml_language;
/**
* Constructor of the class.
*
* Initialize class vars and load xml data from xml language file.
*
* @param string $load_path the path to the directory in which are
* XML language files
* @param string $save_path the path to the directory in which save
* PHP language file
* @param string $lang the language of the file to be parsed
*
* @internal All the params refer to $_CONFIG vars about language
*
* @link http://php.net/simplexml_load_file
*/
function __construct($load_path, $save_path, $lang) {
$this->xml_language_file = $load_path."/".$lang.".xml";
$this->php_language_file = $save_path."/".$lang.".php";
$this->xml_language = simplexml_load_file($this->xml_language_file);
}
/* OBJECT FUNCTION OVERRIDE
************************************************/
/**
* When the class is destroyed this function is called.
*
* @return void
*/
public function __destruct() { }
/**
* When an inaccesible method of the class is invoked this function
* is called.
*
* @return void
*/
public function __call($name, $arguments) {
echo get_class($this)."::Error in calling object method <b>'$name'</b>"."<br>\n";
}
/**
* When an inaccesible static method of the class is invoked this
* function is called.
*
*
* @return void
*/
public static function __callStatic($name, $arguments) {
echo get_class($this)."::Error in calling object static method <b>'$name'</b>"."<br>\n";
}
/**
* When the object is converted to a string this function is used to
* format the result string.
* Values are separated by "|". If the class var is an array, values
* in it will be separated by ",", or NULL if is not set.
* The string starts and ends with "-".
*
* @access public
* @return a string with all class vars values separated by |
*/
public function __toString() {
return NULL;
}
/* SET & GET FUNCTION
************************************************/
/**
* The function set a specified class var.
* Is not case sensitive.
*
* @access public
* @param string $var the name of the var to be set
* @param mixed $value the new value to set
*
* @return true
*/
public function set($var, $value = NULL) {
$this->{$var} = $value;
return true;
}
/**
* The function get a specified class var.
* Is case sensitive.
*
* @access public
* @param string $var the name of the var to be get
*
* @return the class var
*/
public function get($var) {
return $this->{$var};
}
/* PUBLIC FUNCTION
************************************************/
/**
* Parse the XML file
*
* @return void
*
* @see L10n::to_file();
*/
public function parse() {
return $this->to_file($this->xml_language);
}
/* PRIVATE FUNCTION
************************************************/
/**
* Create a string ready to be written in PHP and escapes characters:
* This is the set of characters escapable ( all this char to be
* escaped by this parses must be written with a <code>\</code> first:
* - ' ' ( space )
* - '>'
* - '<'
* - '"'
* - '\'
* - '$'
* - '@' ( hide mail to spam crawler )
*
* @param XMLSimpleObject xml a XML Simple Object on which perform
* the substitution
* @param int page_index page number ( array index of page xml
* subobject )
* @param int text_index text number ( the array index of text
* subobject )
* @return a string with: $.$page[$name] = $text, where $page is the
* page name, $name is the text name, $text is the text value
*
* @link http://php.net/substr
* @link http://php.net/strpos
* @link http://php.net/str_replace
* @link http://php.net/trim
*/
private function string_ready($xml, $page_index, $text_index) {
$page = substr($xml->page[$page_index]["path"], 1);
if ( $pos = strpos($page, "/") )
$page = substr($page, $pos + 1);
$pos = strpos($page, ".");
$page = substr($page, 0, $pos);
$name = $xml->page[$page_index]->text[$text_index]["name"];
$text = str_replace("@", "@",
str_replace("$", "\\$",
str_replace("\"", "\\\"",
str_replace("\\\"", """,
str_replace("\<", "<",
str_replace("\>", ">",
str_replace("\ ", " ",
( trim($xml->page[$page_index]->text[$text_index]) )
)))))));
return "$".$page."[\"".$name."\"] = \"".$text."\";\n";
}
/**
* Create the PHP text file
*
* @param XMLSimpleObject lang the xml of the xml language file
* @return void
*
* @link http://php.net/fopen
* @link http://php.net/fwrite
* @link http://php.net/fclose
* @link http://php.net/count
*
* @see L10n::string_ready()
*/
private function to_file($lang) {
global $_CONFIG;
$fp = fopen($this->php_language_file, "w+");
fwrite($fp, "<?php\n");
fwrite($fp, "## ".$lang["code"]." | ".$lang["version"]." | ".date("H:m:s d/m/Y")."\n");
fwrite($fp, "## This is a L10n file ( for application version ".$lang["app_version"]." )\n");
fwrite($fp, "## This file is created by the system and must not be edited without editing\n");
fwrite($fp, "## also the xml associated language file ( see documentation about L10n ).\n");
fwrite($fp, "## Is divided in subsection, one for every page.\n");
fwrite($fp, "## To use these vars use the 'tt()' function and see doc about i18n and L10n.\n");
fwrite($fp, "##\n");
fwrite($fp, "\n");
for ( $j = 0; $j < count($lang->page); $j++ ) {
fwrite($fp, "###########################################################################\n");
fwrite($fp, "#\n");
fwrite($fp, "#\t".$lang->page[$j]["path"]."\n");
fwrite($fp, "#\n");
for ( $i = 0; $i < count($lang->page[$j]->text); $i++ ) {
fwrite($fp, $this->string_ready($lang, $j, $i));
}
fwrite($fp, "\n");
}
fwrite($fp, "\n?>\n");
return fclose($fp);
}
}
?>