<?php
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kai Dorschner <the-hide@address.com>
* @copyright Copyright 2008, Kai Dorschner
* @license http://www.gnu.org/licenses/lgpl.html LGPLv3
*/
/**
* Token-based translation.
*
* Every word or sentence is saved in an XML file as a token which contains
* each translation language. With this class you can choose your token and
* specify a language which results in a distinct text. Therefor use the
* {@link translateToken()} function.
*
*/
class Translator
{
/**
* Translations Pool.
*
* Contains all tokens in each language in a 2d array.
* It's calles by using the pattern $translation[token][language].
*
* For more information look at {@link getTranslationByLanguage()}
*
* @var array Contains all translations
* @see getTranslationByLanguage()
* @access protected
*/
protected static $translation = array();
/**
* This is the base language in which all texts are being translated.
*
* @var string Contains the default/base language
* @access protected
*/
protected static $baselanguage;
/**
* Private constructor
*
* @access private
*/
private function __construct() {}
/**
* Adds new translation table.
*
* If a translation token already exists, all containing translations will be replaced.
*
* @param DomDocument $xml XML Object with translation table
* @see $translation
* @access public
*/
public static function setTranslation(DomDocument $xml)
{
$xpath = new DomXpath($xml);
foreach($xpath->query('//token/translation') as $node)
self::$translation[$node->parentNode->getAttribute('name')][$node->getAttribute('lang')] = $node;
}
public static function setTranslationFile($path)
{
if(!is_file($path))
throw new MvcException('Can\'t find translation file "' . $path . '"');
$dom = new DomDocument();
$dom->load($path);
self::setTranslation($dom);
}
public static function setTranslations(Array $xmlarray)
{
foreach($xmlarray as $xml) self::setTranslation($xml);
}
/**
* Sets the base language.
*
* Valid types are de, de-au, en, es, fr, cs,...
*
* @see $baselanguage
* @param string $baselanguage Language Token.
* @access public
*/
public static function setLanguage($baselanguage)
{
self::$baselanguage = (String)$baselanguage;
}
/**
* Returns the base language.
*
* @see $baselanguage
* @return string The base language token
* @access public
*/
public static function getLanguage()
{
return self::$baselanguage;
}
/**
* Returns a standalone token in the base language.
*
* @access public
* @return String The text in the base language
* @param String $token The token which will be translated
* @param mixed $context Translates token context sensitive
* @see $translation
* @see $baselanguage
* @see getTranslationByLanguage()
*/
public static function translateToken($token, $context = null)
{
if(count(self::$translation) <= 0)
throw new MvcException('Translation-table not set or empty.');
if(empty(self::$baselanguage))
throw new MvcException('No base language defined.');
if(self::tokenExists($token))
{
if(!is_null($context))
return sprintf(self::translateTokenByCount($token, $context), $context);
else
{
if(is_null($t = self::getTranslationByLanguage($token, self::$baselanguage)))
$t = self::getTranslationByLanguage($token, 'all');
return $t;
}
}
else
return $token;
}
/**
* Returns singular or plural of a text determined by the value of {@see $count}.
*
* Example: If you have the text value "tree(s)" and you prepend an amount
* like 1 or 2 to it you'll get something like "1 tree(s)" or "2 tree(s)".
* This function determines singular and plurals by the amount. This results
* in "1 tree" and "2 trees".
* The value inside the brackets is the plural appendix.
*
* @access public
* @return String Singular or plural text.
* @param String $text Unquantified text.
* @param Integer $count Amount to be quantified.
*/
public static function getTextByCount($text, $count)
{
preg_match('/\((.+)\)/', $text, $regs, PREG_OFFSET_CAPTURE);
$fill = '';
$x = explode('|', $regs[1][0]); // If there are different notations for singular and plural
if((Int)$count != 1)
{
$fill = $x[count($x) - 1]; // last element
}
elseif(count($x) > 1)
{
$fill = $x[0];
}
return substr_replace($text, $fill, $regs[0][1], strlen($regs[0][0]));
}
public static function translateTokenByCount($token, $count)
{
return self::getTextByCount(self::translateToken($token), $count);
}
/**
* Returns a text-token in one language.
*
* @access public
* @return String The translated text
* @param string $token The token which will be translated
* @param string $language
* @see $translation
*/
public static function getTranslationByLanguage($token, $language)
{
if(isset(self::$translation[$token][$language]))
{
return self::$translation[$token][$language]->nodeValue;
}
return null;
}
public static function tokenExists($token)
{
return array_key_exists($token, self::$translation);
}
}