Sto provando ad usare i baffi insieme a i18n (php, all'interno di Wordpress). Ho la funzionalità di base __ lavorando bene, qualcosa di similebaffi i18n con parametri
class my_i18n {
public function __trans($string) {
return __($string, 'theme-name');
}
}
class mytache {
public function __()
{
return array('my_i18n', '__trans');
}
}
Poi per produrre un modello con una stringa i18n, posso semplicemente fare questo tutto
$context = new mytache;
$template = "<div>{{#__}}String to translate{{/__}}</div>";
$m = new Mustache;
echo $m->render($template, $context);
Finora va bene. Tuttavia, voglio essere in grado di tradurre stringhe con parametri. cioè l'equivalente di sprint_f(__('Account Balance: %s'), $balance);
.
Sembra che se faccio qualcosa come {{#__}}Account Balance: {{balance}}{{/__}}
non funziona. Sto indovinando perché il tag interno viene convertito prima e quindi la traduzione non può essere trovata per la frase.
Qualche idea su come ottenere questo risultato con Mustache?
UPDATE: ecco il frammento di risultato finale (con l'aiuto di massiccia 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');
}
}
"non funziona">. < –
Quale parola chiave hai usato per estrarre le stringhe dal modello dei baffi? –