Re: [SMARTY] translation translation [continued]

From: Date: Fri, 12 Apr 2002 09:32:08 +0000
Subject: Re: [SMARTY] translation translation [continued]
References: 1 2 3 4 5  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-3661@lists.php.net to get a copy of this message
I have created such functionality by use of a prefilter. The sourcecode of this prefilter is attached. It uses the global array var $_select_vars to select blocks in your templates. For example: {select lang="en"} Hello world! {elseselect lang="fr"} Bonjour tout le monde! {/select} When compiling the template the global $_select_vars["lang"] should contain either 'en' or 'fr'. You might want to use smarty's compile_id to make sure different compiled templates exist for each language like this: $smarty->compile_id = serialize($_select_vars); And when using caching probably in the same manner. I found it extremely helpful to use session variables to remember the language choice. Please note that you can use this prefilter for any kind of block selection at compile time. Monte: maybe this is something which could be included as a contributed prefilter? Greetz, Ralphm On Fri, Apr 12, 2002 at 11:23:02AM +0200, Michal Prinke wrote: > Monte Ohrt wrote: > > There is no translation function. This is an example of block function > > syntax: > > > > {myfunc} > > > > {/myfunc} > > > > My point was, you can _create_ a translation function with the block > > funtionality of Smarty. > > > Okay, can you help with this? I think it shouldn't be too complicated... > > 1. In the PHP script, I have to assign a template variable containing > the current language: > > $sm = new Smarty(); > $sm->assign("lg", "en"); // English language > $sm->display("index.tpl"); > > > 2. Then, I should include some translations in my template: > > <html><body> > {translate lg="en"} > Hello world! > {/translate} > {translate lg="fr"} > Bonjour tout le monde! > {/translate} > </body></html> > > > 3. So, the block function "translate" has a very uncomplicated logic - > it should simply print the content between the tags > if its "lg" parameter matches the "lg" variable set from the outside: > > function smarty_function_translate($params, &$smarty) { > $tpl_vars = $smarty->get_template_vars(); > if ($params['lg'] == $tpl_vars['lg']) > echo($the_content_between_the_tags); // ??? > else > discard_the_content_between_the_tags(); // ??? > } > > > > *** FINALLY, MY QUESTIONS ARE: *** > How to write block functions? That is, how to operate on the content > between the opening and the closing tag? How to print it out and how > to omit it? How to implement the lines marked with "???" ? > > Thank you... > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- Groetjes, Ralphm

<?php function smarty_select_prefilter($template_source, $smarty) { $ldq = $smarty->left_delimiter; $rdq = $smarty->right_delimiter; $new_template_source = $template_source; $select_block_regexp = "!{$ldq}\s*select\s*.*?{$rdq}.*?{$ldq}\s*/select\s*{$rdq}!s"; $qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; $select_tag_regexp = "!{$ldq}\s*((?:select|elseselect|/select)\s*.*?)\s*{$rdq}\n?!s"; # check for {select}...{/select} blocks if (preg_match_all($select_block_regexp, $template_source, $match)) { # store all {select}...{/select} blocks $select_blocks = $match[0]; for ($i = 0; $i < count($select_blocks); $i++) { # store all text surrounding {select}, {elseselect} and {/select} # blocks $text_blocks = preg_split($select_tag_regexp, $select_blocks[$i]); # get and store all {select}, {elseselect} and {/select} tags preg_match_all($select_tag_regexp, $select_blocks[$i], $match); $select_tags = $match[1]; for ($j = 0; $j < count($select_tags); $j++) { $select_tag = $select_tags[$j]; # Split tag into two parts: command and the arguments. preg_match('/^( (?: ' . $qstr_regexp . ' | (?>[^"\'\s]+))+ ) (?:\s+(.*))? /xs', $select_tag, $match); $tag_command = $match[1]; $tag_args = isset($match[2]) ? $match[2] : ''; $attrs = $smarty->_parse_attrs($tag_args); # if this block is to be selected, store the text inside # this block if ($attrs and is_array($attrs)) { list($arg_name, $arg_value) = each($attrs); eval( '$match = '. '$GLOBALS["_select_vars"]["'.$arg_name.'"] == '. $arg_value.';' ); if ($match) { $select_blocks_modified[$i] = $text_blocks[$j + 1]; } } } } for ($i = 0; $i < count($select_blocks); $i++) { $new_template_source = preg_replace( $select_block_regexp, $select_blocks_modified[$i], $new_template_source, 1); } } return $new_template_source; } function init_smarty_select_prefilter(&$smarty) { $GLOBALS["_select_vars"] = array(); $smarty->register_prefilter("smarty_select_prefilter"); } ?>
« previous php.smarty.general (#3661) next »