From: Markus G. v. K. <mar...@us...> - 2003-04-01 01:18:18
|
Hello Luis and list, if I understand the situation with 1.6cvs correctly (please correct me if this is not the case! :-) all translations done with tra() are done, before they get passed to the tpl file, they are there available via a smarty {$var} variable. Or they are not used by tpl files (eg. subject lines of email notifications, etc.) After a tpl got injected with a first set of translations from tra(), available as variables, the prefilter-{tr} enclosed strings are translated. And now, the tpl gets compiled. And since the translations are pre-filtered, a different set of compiled templates exists for each language. (and postfilters could again change the compiled templates before they are actually saved to disk). The {$var} variables are replaced with appropriate php language constructs in the compiled template files ($this->_tpl_vars[]), but the content of this _tpl_vars[] is still as returned from tra(). And only during output (like in output filtering?) the real dynamic stage of all this takes place, the _tpl_vars[] are replaced with their values; smartie's $this->_plugins['functions'] are executed, all this already done by the php engine. Assuming that the way of thoughts so far is not too off, I can conclude for a modified tra(): 1) Everything, which is available at the execution/call time of tra() can be used by tra(), eg. from tikilib.php: $smarty->assign('msg',tra('Cannot write to this file:').$fhash); $smarty->display("styles/$style_base/error.tpl"); Could be written $smarty->assign('msg',tra('Cannot write to this file: $1', $fhash)); $smarty->display("styles/$style_base/error.tpl"); because the compiled template uses $this->_tpl_vars['msg']; and only during output the final string gets used. For eg. German this could be used as a much more nicer $smarty->assign('msg',tra('Kann nicht in Datei $1 schreiben.', $fhash)); Advantages: Easier comprehension: both while writing and translating, punctuation is/can be included with translation (eg. Japanese uses brackets differently, quotation marks are totally different). Disadvantage: The number of strings would increase. Advantage of this disadvantage :-) cases where the same word in English is used, but another language would require different words, can be handled. Based on what I did a couple of weeks ago in regard to passing arguments to a translation, below is the revised code to work with the current Tiki1.6 cvs (changes for tra()). Unfortunatelly this is currently about tra() only. I have some problems of how Smarty handles function parameters, they are not parsed, but smarty creates a stack (php array) to be handled later. So no prefiltering of {tr a="param"} blocks for now, therefore no expanded version of smarty_block_tr() :-( Does anybody know how smarty can be convinced to not stack function params? What has changed in comparison to 1.6cvs? 1) Each translation (whether it comes from file, from db or is no translation but the original text) is not returned immeditatelly but only after an additional test (see 3) has been made. 2) If the additional test fails, the translation is returned, behaviour is as with the current cvs version. The impact on performance should be minimal. 3) The new if-clause checks, whether additional params have been passed to the translation routine. If there are none (as is the current situation) the behaviour does not change (see 2). 4) If there are additional params, the translated string is parsed, all occurences of a key are replaced with the provided additional function arguments. 5) The key string currently parsed for, is $1, $2, $3 etc. and each can be used multiple times. I currently see no demerrits of this key, but in the end, almost everything would be fine. Hopefull this can be usefull :-) Markus function tra($content) { global $lang_use_db; if ($lang_use_db!='y') { global $lang; if ($content) { if(isset($lang[$content])) { $ret = $lang[$content]; //return $lang[$content]; } else { $ret = $content; //return $content; } } else { return; // $ret = "", no sense in continuing here } } else { global $tikilib; global $language; $query="select tran from tiki_language where source='".addslashes($content)."' and lang='".$language."'"; $result=$tikilib->db->query($query); $res=$result->fetchRow(DB_FETCHMODE_ASSOC); if(DB::isError($result)) { $ret = $content; //return $content; } elseif(!isset($res["tran"])) { global $record_untranslated; if ($record_untranslated=='y') { $query="insert into tiki_untranslated (source,lang) values('".addslashes($content)."','".$language."')"; //No eror checking here $tikilib->db->query($query); } $ret = $content; // return $content; } else { $ret = $res["tran"]; //return $res["tran"]; } } // $ret contains some sensible value, check for more function params than only $content if(func_num_args() > 1) { // new behaviour, additional args $args = func_get_args(); // get all arguments array_shift($args); // get rid of $content, re-organize keys (0, 1, 2 etc.) foreach($args as $key => $arg) { // $key starts with 0, use 1 for search $ret = preg_replace('/\$'.++$key.'/', $arg, $ret); } } return $ret; } |