Discussion:
PHP {#trans}{#end} implementation
Micah Stetson
2010-03-08 21:03:35 UTC
Permalink
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
--
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.
Andy Chu
2010-03-09 02:15:19 UTC
Permalink
Nice!
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
// 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.
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+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/json-template?hl=en.
Loading...