我有翻译功能t($var);
function t($word) {
return $this->words[$word];
}
where $this->words
是数组
$this->words = array(
'word1' => 'word',
'word2' => 'something'
);
我使用函数作为<?php echo t('word1'); ?>
输出是:word
My goal是使用通配符%s, %d, %f用变量替换它们。
Example:
$this->words = array(
'word1' => 'word',
'word2' => 'something',
'sentence' => 'Hello, my name is %s. I am %d years old.'
);
然后将变量解析为t()
功能。
<?php echo t('sentence', array('Mike', 99));
所以输出将是:Hello, my name is Mike. I am 99 years old.
到目前为止我的工作:
function t($word, $vars = array()) {
foreach ($vars as $key) {
if(is_string($key)){
$this->words[$word] = str_replace ('%s', $key, $this->words[$word]);
}
if(is_int($key)) {
$this->words[$word] = str_replace ('%d', $key, $this->words[$word]);
}
if(is_float($key)){
$this->words[$word] = str_replace ('%f', $key, $this->words[$word]);
}
}
return $this->words[$word];
}
但此函数不适用于每种类型的多个变量。