Thread: [phpwebapp-commits] CVS: web_app/parser class.WebPage.php,1.20,1.21 class.WebObjectTpl.php,1.10,1.11
Brought to you by:
dashohoxha
From: Dashamir H. <das...@us...> - 2005-11-01 13:24:42
|
Update of /cvsroot/phpwebapp/web_app/parser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1556/parser Modified Files: class.WebPage.php class.WebObjectTpl.php class.WebObject.php class.Render.php class.Parser.php Log Message: i18n and l10n of web_app messages Index: class.WebPage.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/parser/class.WebPage.php,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** class.WebPage.php 7 Jul 2005 07:08:40 -0000 1.20 --- class.WebPage.php 1 Nov 2005 13:24:25 -0000 1.21 *************** *** 50,54 **** * with msgid as key and msgstr (translation) as value. */ ! var $js_i18n_messages; /** --- 50,54 ---- * with msgid as key and msgstr (translation) as value. */ ! var $js_l10n_messages; /** *************** *** 85,89 **** $this->rs_collection = array(); ! $this->js_i18n_messages = array(); $this->messages = array(); $this->popup_windows = array(); --- 85,89 ---- $this->rs_collection = array(); ! $this->js_l10n_messages = array(); $this->messages = array(); $this->popup_windows = array(); *************** *** 121,125 **** if (!isset($tpl->id)) { ! print "Error:WebPage:addTemplate: unidentified template.\n"; $tpl->toText(); return; --- 121,126 ---- if (!isset($tpl->id)) { ! $msg = TF_("unidentified template"); ! print WebApp::error_msg("WebPage::addTemplate(): $msg.\n"); $tpl->toText(); return; *************** *** 172,176 **** if (!isset($rs->ID)) { ! print WebApp::error_msg("Error:WebPage::addRecordset(): unidentified recordset.\n"); print $rs->toHtmlTable(); return; --- 173,178 ---- if (!isset($rs->ID)) { ! $msg = TF_("unidentified recordset"); ! print WebApp::error_msg("WebPage::addRecordset(): $msg.\n"); print $rs->toHtmlTable(); return; *************** *** 210,213 **** --- 212,268 ---- /** + * Extract translatable messages (which are denoted by T_("...") + * or TF_("...") ) from a JS file, and save them in the + * associative array $this->js_l10n_messages . These messages + * (and their translations) are later appended at the end of the page + * in order to enable the translation of JS messages. + * Called by WebObjTpl::before_render(). + */ + function extract_js_l10n_messages($js_filename) + { + //match patterns like this: T_("....") or TF_("....") + $pattern = '#(TF?_)\("([^"]+)"\)#'; + $js_code = file_get_contents($js_filename); + preg_match_all($pattern, $js_code, $matches); + for ($i=0; $i < sizeof($matches[1]); $i++) + { + $f_name = $matches[1][$i]; //function name (T_ or TF_) + $msgid = $matches[2][$i]; //the string of the message + $pattern = "\\\\n(\\\\ *\n)"; //replace '\n' by newline + $msgid = ereg_replace($pattern, "\n\\1", $msgid); + + //translate; either T_($msgid) or TF_($msgid) + $msgstr = $f_name($msgid); + $this->js_l10n_messages[$msgid] = $msgstr; + } + } + + /** + * Convert the messages in $this->js_l10n_messages into + * JavaScript code that can be used to translate JS messages. + * Returns a JS code that is appended at the end of the page + * (this function is called by 'l10n-module/wbJSL10n.php'). + */ + function js_l10n_messages_to_js() + { + $arr_msg = array(); + while ( list($msgid, $msgstr) = each($this->js_l10n_messages) ) + { + //remove an extra slash and newline + $msgid = str_replace("\\\n", "", $msgid); + + //replace newlines by '\n' (otherwise JS error will occur) + $msgid = str_replace("\n", "\\n", $msgid); + $msgstr = str_replace("\n", "\\n", $msgstr); + + //add the translation in the list of messages + $arr_msg[] = "l10n.addMsg(\"$msgid\", \"$msgstr\");"; + } + $js_code = implode($arr_msg, "\n "); + + return $js_code; + } + + /** * Returns JS code that displays an alert for * each message in the messages array. Index: class.WebObjectTpl.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/parser/class.WebObjectTpl.php,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** class.WebObjectTpl.php 22 Jun 2005 12:56:28 -0000 1.10 --- class.WebObjectTpl.php 1 Nov 2005 13:24:25 -0000 1.11 *************** *** 84,88 **** function before_render() { ! global $tplVars, $l10n; //use another translation file, if such a file exists --- 84,88 ---- function before_render() { ! global $tplVars, $l10n, $webPage; //use another translation file, if such a file exists *************** *** 107,111 **** //extract translatable messages in the JS code ! $this->get_translatable_messages(); //call the function onRender of this WebObject --- 107,111 ---- //extract translatable messages in the JS code ! $this->extract_js_l10n_messages(); //call the function onRender of this WebObject *************** *** 114,139 **** /** ! * Extract translatable messages (which are denoted by T_("...") ) ! * from a JS file, and save them in the associative array ! * $webPage->js_i18n_messages ! * Called by before_render() */ ! function get_translatable_messages() { ! $fname = $this->class->path.$this->id.".js"; if (file_exists($fname)) { global $webPage; ! $pattern = '#T_\("([^"]+)"\)#'; //match patterns like this: T_("....") ! $js_code = file_get_contents($fname); ! preg_match_all($pattern, $js_code, $matches); ! for ($i=0; $i < sizeof($matches[1]); $i++) ! { ! $msgid = $matches[1][$i]; ! $pattern = "\\\\n(\\\\ *\n)"; //replace '\n' by newline ! $msgid = ereg_replace($pattern, "\n\\1", $msgid); ! $msgstr = T_($msgid); ! $webPage->js_i18n_messages[$msgid] = $msgstr; ! } } } --- 114,127 ---- /** ! * Extract translatable messages (which are denoted by T_("...") ! * or TF_("...") ) from a JS file. Called by before_render(). */ ! function extract_js_l10n_messages() { ! $fname = $this->class->path . $this->class->id . ".js"; if (file_exists($fname)) { global $webPage; ! $webPage->extract_js_l10n_messages($fname); } } Index: class.WebObject.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/parser/class.WebObject.php,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** class.WebObject.php 20 Sep 2004 13:28:06 -0000 1.11 --- class.WebObject.php 1 Nov 2005 13:24:25 -0000 1.12 *************** *** 166,170 **** break; default: ! print WebApp::warning_msg("WebObject::getSVars(): unreckognized \$type '$type'."); case UNDEFINED: $arr_vars = array_merge($session->Vars, $session->dbVars); --- 166,172 ---- break; default: ! $msg = TF_("unreckognized \$type 'v_type'"); ! $msg = str_replace('v_type', $type, $msg); ! print WebApp::warning_msg("WebObject::getSVars(): $msg."); case UNDEFINED: $arr_vars = array_merge($session->Vars, $session->dbVars); Index: class.Render.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/parser/class.Render.php,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** class.Render.php 6 Jul 2005 14:07:21 -0000 1.25 --- class.Render.php 1 Nov 2005 13:24:25 -0000 1.26 *************** *** 53,59 **** function render_string($str, $indent =true) { ! //translate all T_("xyz") strings global $l10n; ! if (ereg('T_\\("[^"]*"\\)', $str)) $str = $l10n->translate($str); //replace all {{tpl_vars}} --- 53,59 ---- function render_string($str, $indent =true) { ! //translate all T_("xyz") and TF_("xyz") strings global $l10n; ! if (ereg('TF?_\\("[^"]*"\\)', $str)) $str = $l10n->translate($str); //replace all {{tpl_vars}} *************** *** 321,324 **** --- 321,328 ---- global $tplVars, $l10n; $l10n->pushState(); + $dir = $wobj_tpl->class->path; + $id = $wobj_tpl->class->id; + $l10n->set_translation_file($dir, $id); + $tplVars->pushScope(); //create a new variable scope *************** *** 355,360 **** else //such a recordset does not exist { ! $err_msg = WebApp::error_msg("Recordset '$rs_id' is undefined."); ! $this->render_string($err_msg); $this->render_tpl($tpl); return; --- 359,365 ---- else //such a recordset does not exist { ! $msg = TF_("Recordset 'v_rs_id' is undefined."); ! $msg = str_replace('v_rs_id', $rs_id, $msg); ! $this->render_string(WebApp::error_msg($msg)); $this->render_tpl($tpl); return; *************** *** 363,368 **** if ($rs->type<>"PagedRS") { ! $err_msg = "Recordset '$rs_id' is not of type PagedRS."; ! $this->render_string(WebApp::error_msg($err_msg)); $this->render_tpl($tpl); return; --- 368,374 ---- if ($rs->type<>"PagedRS") { ! $msg = TF_("Recordset 'v_rs_id' is not of type PagedRS."); ! $msg = str_replace('v_rs_id', $rs_id, $msg); ! $this->render_string(WebApp::error_msg($msg)); $this->render_tpl($tpl); return; *************** *** 401,406 **** else //such a recordset does not exist { ! $err_msg = WebApp::error_msg("Recordset '$rs_id' is undefined."); ! $this->render_string($err_msg); $this->render_tpl($tpl); return; --- 407,413 ---- else //such a recordset does not exist { ! $msg = TF_("Recordset 'v_rs_id' is undefined."); ! $msg = str_replace('v_rs_id', $rs_id, $msg); ! $this->render_string(WebApp::error_msg($msg)); $this->render_tpl($tpl); return; Index: class.Parser.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/parser/class.Parser.php,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** class.Parser.php 24 Jun 2005 14:50:35 -0000 1.24 --- class.Parser.php 1 Nov 2005 13:24:25 -0000 1.25 *************** *** 93,98 **** if (!file_exists($filename)) { ! $msg = "Parser::parse_file(): file '$filename' does not exist."; ! $err_msg = WebApp::error_msg($msg); $this->append($err_msg); return UNDEFINED; --- 93,99 ---- if (!file_exists($filename)) { ! $msg = TF_("file 'v_filename' does not exist"); ! $msg = str_replace('v_filename', $filename, $msg); ! $err_msg = WebApp::error_msg("Parser::parse_file(): $msg."); $this->append($err_msg); return UNDEFINED; *************** *** 463,467 **** } ! /** Append $data to the last element of the content of the current_tpl. */ function append($data) { --- 464,468 ---- } ! /** Append $data to the contents of the current_tpl. */ function append($data) { *************** *** 538,544 **** function link_template($tpl) { - //set the current_tpl as the parent of $tpl - $tpl->parent = &$this->current_tpl; - //set a reference from the current_tpl to the new template; $this->append('&&'.$tpl->id.';;'); --- 539,542 ---- *************** *** 574,577 **** --- 572,576 ---- //create a new file template $tpl = new FileTpl($tpl_filename); + $tpl->parent = &$this->current_tpl; //parse the file into this FileTpl *************** *** 627,632 **** else { ! $msg = "<IfEmpty> element can be used " ! ."only inside a <Repeat> element"; $err_msg = WebApp::error_msg($msg); $this->append($err_msg); --- 626,630 ---- else { ! $msg = TF_("<IfEmpty> element can be used only inside a <Repeat> element"); $err_msg = WebApp::error_msg($msg); $this->append($err_msg); *************** *** 659,664 **** else { ! $msg = "<Separator> element can be used " ! ."only inside a <Repeat> element"; $err_msg = WebApp::error_msg($msg); $this->append($err_msg); --- 657,661 ---- else { ! $msg = TF_("<Separator> element can be used only inside a <Repeat> element"); $err_msg = WebApp::error_msg($msg); $this->append($err_msg); *************** *** 794,801 **** $id = $attribs['ID']; global $webPage; ! if ($webPage->getTemplate($id)<>UNDEFINED) { ! $msg = "Redeclaration of <WebClass ID=\"$id\">."; $warning_msg = WebApp::warning_msg($msg); $this->append($warning_msg); --- 791,811 ---- $id = $attribs['ID']; + //check that a webclass with such an id is not already loaded global $webPage; ! $tpl = $webPage->getTemplate($id); ! ! //$reinclude is true when the same webclass is included again ! $reinclude = ( $tpl!=UNDEFINED ! and $tpl->parent->id == $this->current_tpl->id ); ! ! //$redeclaration is true if this is another webclass with the same id ! $redeclaration = ( $tpl!=UNDEFINED ! and $tpl->parent->id != $this->current_tpl->id ); ! ! if ($redeclaration) { ! $msg = TF_("Redeclaration of <WebClass ID=\"v_id\"> in 'v_path'."); ! $msg = str_replace('v_id', $id, $msg); ! $msg = str_replace('v_path', $this->current_tpl->id, $msg); $warning_msg = WebApp::warning_msg($msg); $this->append($warning_msg); *************** *** 807,811 **** $tpl->parent = &$this->current_tpl; ! $tpl->before_parse(); //copy the <WebClass> element into $tpl --- 817,821 ---- $tpl->parent = &$this->current_tpl; ! if (!$reinclude) $tpl->before_parse(); //copy the <WebClass> element into $tpl *************** *** 833,838 **** { //give an error message ! $msg = "Parser: <parameter name='$name' default='$default'/> " ! . "can be used only inside a <webclass> element."; $err_msg = WebApp::error_msg($msg); $this->append($err_msg); --- 843,847 ---- { //give an error message ! $msg = TF_("<parameter/> can be used only inside a <webclass> element."); $err_msg = WebApp::error_msg($msg); $this->append($err_msg); *************** *** 858,862 **** { $webobj_elem = '<WebObject'.$this->get_attr_list($attribs).' />'; ! $msg = "Parse: <WebClass> '$class_name' is not defined."; $this->append(WebApp::error_msg($webobj_elem)); $this->append(WebApp::error_msg($msg)); --- 867,872 ---- { $webobj_elem = '<WebObject'.$this->get_attr_list($attribs).' />'; ! $msg = TF_("<WebClass> 'v_class_name' is not defined."); ! $msg = str_replace('v_class_name', $class_name, $msg); $this->append(WebApp::error_msg($webobj_elem)); $this->append(WebApp::error_msg($msg)); *************** *** 929,933 **** { default: ! $msg = "Recordset '$id': type '$type' is unknown"; print WebApp::error_msg($msg); case UNDEFINED: --- 939,945 ---- { default: ! $msg = TF_("Recordset 'v_id': type 'v_type' is unknown"); ! $msg = str_replace('v_id', $id, $msg); ! $msg = str_replace('v_type', $type, $msg); print WebApp::error_msg($msg); case UNDEFINED: |