Hi,
I'm trying to translate a formatted string as a test but can't get it to work.
The translation code (translate.class.php) is:
<?php
class Translator {
private $language = 'en';
private $lang = array();
public function __construct($language){
$this->language = $language;
}
private function findString($str) {
if (array_key_exists($str, $this->lang[$this->language])) {
echo $this->lang[$this->language][$str];
return;
}
echo $str;
}
private function splitStrings($str) {
return explode('::',trim($str));
}
public function __($str) {
if (!array_key_exists($this->language, $this->lang)) {
if (file_exists($this->language.'.txt')) {
$strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
foreach ($strings as $k => $v) {
$this->lang[$this->language][$v[0]] = $v[1];
}
return $this->findString($str);
}
else {
echo $str;
}
}
else {
return $this->findString($str);
}
}
}
class Translate extends Translator {
private $lang = 'en';
public function __($str) {
if (func_num_args() < 1) {
return false;
}
$args = func_get_args();
$str = array_shift($args);
if (count($args)) {
return vsprintf(parent::__($str, $this->lang),$args);
}
else {
return parent::__($str, $this->lang);
}
}
}
?>
The page code is
<?php
include("./includes/class/translate.class.php");
$translate= new Translate('de');
print $translate->__("Your search for '%s' was unsuccessful",'teacher');
print "<p>";
print $translate->__('Number %d of %d',11,13);
?>
and the translation file is
Your search for '%s' was unsuccessful::Suche nach '%s' nicht erfolgreich war
Number %d of %d::Nummer %d aus %d
I used :: in case I need to use = within a string
However this outputs:
Suche nach '%s' nicht erfolgreich war
Nummer %d aus %d
Can anyone see where I'm going wrong? I expect it's something extremely simple as usual
Any help greatly appreciated.