我正在嘗試將鬍子和i18n(php,在Wordpress中)一起使用。我已經得到了基本的功能__很好的工作,像這樣帶參數的鬍子i18n
class my_i18n {
public function __trans($string) {
return __($string, 'theme-name');
}
}
class mytache {
public function __()
{
return array('my_i18n', '__trans');
}
}
然後輸出與國際化字符串的模板,我可以簡單地這樣做
$context = new mytache;
$template = "<div>{{#__}}String to translate{{/__}}</div>";
$m = new Mustache;
echo $m->render($template, $context);
到目前爲止,一切都很好。但是,我希望能夠使用參數翻譯字符串。即相當於sprint_f(__('Account Balance: %s'), $balance);
。
看來,如果我做了類似{{#__}}Account Balance: {{balance}}{{/__}}
的東西,它就不起作用。我猜是因爲內標籤首先被轉換,因此無法找到該短語的翻譯。
任何想法如何用鬍子乾淨地做到這一點?
更新:這是最終的結果片段(從bobthecow巨大的幫助):
class I18nMapper {
public static function translate($str) {
$matches = array();
// searching for all {{tags}} in the string
if (preg_match_all('/{{\s*.*?\s*}}/',$str, &$matches)) {
// first we remove ALL tags and replace with %s and retrieve the translated version
$result = __(preg_replace('/{{\s*.*?\s*}}/','%s', $str), 'theme-name');
// then replace %s back to {{tag}} with the matches
return vsprintf($result, $matches[0]);
}
else
return __($str, 'theme-name');
}
}
class mytache {
public function __()
{
return array('I18nMapper', 'trans');
}
}
「it does not work」>。< – 2012-01-01 22:23:08
你用什麼關鍵詞從小鬍子模板中提取字符串? – 2014-01-15 14:47:42