Before we were using mustache
for template rendering, we had a PHP class that we used to translate text for localization. What it basically did, is that it accepted a variable number of parameters, searched for the translation for the first parameter in the database, then replaced in the variables in the translation to their respective parts. Below you will see the definition of the translating function this class used:
function translateText($text, $param1, $param2, ...., $paramX) {
// search for the $text in the database, and retrieve the translation for it
// if params are present, replace the %d strings from the translated text, with the params
}
For example, I could have inputed something like this to the function:
translateText("%0, please fetch me the bread", "Adam");
which would have been translated in Hungarian to
Add ide a kenyeret %0
Then the %0 parameter would have been replaced with my name, which was inputed for the second parameter in the function.
I tried to do something simmilar with mustache
and it's lambda
capability and reusing my old function. It looks something like this:
function($text, $helper) {
preg_match_all("/{{[#a-zA-Z0-9._-]+}}/", $text, $matches);
$translateParams = array();
foreach ($matches[0] as $iterator => $match) {
$text = str_replace($match, '%'.$iterator, $text);
$translateParams[] = $helper->render($match);
}
array_unshift($translateParams, $text);
return call_user_func_array('translateText', $translateParams);
}
For basic usage, it works nicely, but if I try to translate something inside a translation, like this:
{{# func.Translate }} Translate this, and {{# func.Translate }} translate this separately {{/ func.Translate }} {{/ func.Translate }}
i am getting errors, because my regular expression matcher, will match each inner mustache tag separately.
It matches: {{# func.Translate }}
, and {{/ func.Translate }}
instead of matching {{# func.Translate }} translate this separately {{/ func.Translate }}
.
My problem is that I am a novice when it comes to regular expressions, and I can't figure out how could I match with a single preg_match
both the simple mustache
expressions ({{variable}}
) and the more complex ones like in the example. Could anyone help me please?