Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Translate.php

Go to the documentation of this file.
00001 <?php 00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 00003 // 00004 // Copyright (c) 2003 Laurent Bedubourg 00005 // 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 // 00020 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr> 00021 // 00022 00023 class PHPTAL_Attribute_I18N_Translate extends PHPTAL_Attribute 00024 { 00025 function activate(&$g, &$tag) 00026 { 00027 $g->requireGettext(); 00028 00029 if (strlen($this->expression) == 0) { 00030 $key = "'". $this->_preparseGetTextKey($tag) ."'"; 00031 } else { 00032 $exp = new PHPTAL_Expression($g, $tag, $this->expression); 00033 $exp->setPolicy(_PHPTAL_ES_RECEIVER_IS_TEMP); 00034 $key = $g->newTemporaryVar(); 00035 $exp->setReceiver($key); 00036 $err = $exp->generate(); 00037 if (PEAR::isError($err)) { return $err; } 00038 } 00039 00040 // children may contains i18n:name attributes, 00041 // we ignore output but parse content before calling translation. 00042 $g->doOBStart(); 00043 foreach ($tag->_children as $child) { 00044 $child->generateCode($g); 00045 } 00046 $g->doOBClean(); 00047 // $code = sprintf('$__tpl__->_translate(\'%s\')', $key); 00048 $code = sprintf('$__tpl__->_translate(%s)', $key); 00049 $g->doPrintRes($code, true); 00050 } 00051 00052 function _preparseGetTextKey(&$tag) 00053 { 00054 $key = ""; 00055 foreach ($tag->_children as $child) { 00056 if ($child->isData()) { 00057 $str = preg_replace('/\s+/sm', ' ', $child->_content); 00058 $key .= trim($str) . ' '; 00059 } else { 00060 $is_i18n_name = false; 00061 // look for i18n:name 00062 foreach ($child->_surround_attributes as $att) { 00063 if (get_class($att) == strtolower("PHPTAL_Attribute_I18N_Name")) { 00064 $key .= '${' . $att->expression . '}'; 00065 $is_i18n_name = true; 00066 } 00067 } 00068 // recursive call to preparse key for non i18n:name tags 00069 if (!$is_i18n_name) { 00070 $key .= PHPTAL_Attribute_I18N_Translate::_preparseGetTextKey($child) . ' '; 00071 } 00072 } 00073 } 00074 00075 // remove every thing that has more than 1 space 00076 $key = preg_replace('/\s+/sm', ' ', $key); 00077 $key = trim($key); 00078 return $key; 00079 } 00080 } 00081 00082 ?>

Generated on Tue Jun 29 23:40:07 2004 for Mediawiki by doxygen 1.3.7