gin*_*ime 5 php wordpress internationalization mustache
我正在尝试将Mustache与i18n(php,在Wordpress中)一起使用.我有基本的__功能很好地工作,就像这样
class my_i18n {
public function __trans($string) {
return __($string, 'theme-name');
}
}
class mytache {
public function __()
{
return array('my_i18n', '__trans');
}
}
Run Code Online (Sandbox Code Playgroud)
然后输出一个带有i18n字符串的模板,我可以简单地这样做
$context = new mytache;
$template = "<div>{{#__}}String to translate{{/__}}</div>";
$m = new Mustache;
echo $m->render($template, $context);
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好.但是,我希望能够使用参数翻译字符串.即相当于sprint_f(__('Account Balance: %s'), $balance);.
似乎如果我做的事情{{#__}}Account Balance: {{balance}}{{/__}}不起作用.我猜是因为内部标签首先被转换,因此无法找到该短语的翻译.
有任何想法如何使用Mustache干净利落地实现这一目标?
更新:这是最终结果片段(来自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');
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里添加了一个 i18n 示例...它非常俗气,但测试通过了。看起来这和你正在做的几乎一样。您是否可能使用过时的 Mustache 版本?该规范用于指定不同的变量插值规则,这将使该用例无法按预期工作。
| 归档时间: |
|
| 查看次数: |
2192 次 |
| 最近记录: |