Micah Stetson
2010-03-08 21:03:35 UTC
This was even easier than I anticipated. Given a function
translateOne() which finds the translation of its argument string, the
following PHP functions implement the translation system recently
discussed under the subject "Proposal: Transform Blocks":
// Find, translate, and replace occurrences of {#trans}...{#end}
// in $str. The text to be translated is between the delimiters
// and optionally surrounded by white space. Alternative
// metacharacters may be specified if, say, [#trans] is more
// convenient than {#trans}.
function translate($str, $metachars='{}') {
$meta = JsonTemplateModule::SplitMeta($metachars);
$start = preg_quote($meta[0].'#trans'.$meta[1]);
$end = preg_quote($meta[0].'#end'.$meta[1]);
// Non-greedy matching is necessary so that
// {#trans}one{#end}{#trans}two{#end} is seen
// as two separate translations.
$trans_re = '/'.$start.'\s*(.*?)\s*'.$end.'/';
return preg_replace_callback($trans_re, array($this,
'_translateCallback'), $str);
}
function _translateCallback($matches) {
return translateOne($matches[1]);
}
I hope somebody finds this useful,
Micah
translateOne() which finds the translation of its argument string, the
following PHP functions implement the translation system recently
discussed under the subject "Proposal: Transform Blocks":
// Find, translate, and replace occurrences of {#trans}...{#end}
// in $str. The text to be translated is between the delimiters
// and optionally surrounded by white space. Alternative
// metacharacters may be specified if, say, [#trans] is more
// convenient than {#trans}.
function translate($str, $metachars='{}') {
$meta = JsonTemplateModule::SplitMeta($metachars);
$start = preg_quote($meta[0].'#trans'.$meta[1]);
$end = preg_quote($meta[0].'#end'.$meta[1]);
// Non-greedy matching is necessary so that
// {#trans}one{#end}{#trans}two{#end} is seen
// as two separate translations.
$trans_re = '/'.$start.'\s*(.*?)\s*'.$end.'/';
return preg_replace_callback($trans_re, array($this,
'_translateCallback'), $str);
}
function _translateCallback($matches) {
return translateOne($matches[1]);
}
I hope somebody finds this useful,
Micah
--
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
You received this message because you are subscribed to the Google Groups "JSON Template" group.
To post to this group, send email to json-template-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To unsubscribe from this group, send email to json-template+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.