rm trailing white space
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / PHPTAL / Attribute / I18N / Attributes.php
blob4c12f5e491af6ecbc2b85fa1be68f0c62f15682e
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
23 class PHPTAL_Attribute_I18N_Attributes extends PHPTAL_Attribute
25 var $_overwritten = array();
27 function activate(&$g, &$tag)
29 global $_phptal_xhtml_boolean_attributes;
30 $g->requireGettext();
32 $g->doPrintString('<', $tag->name());
34 $attributes = $tag->attributes();
36 // prepare expressions for attributes to translate
37 $exp = new PHPTAL_Expression($g, $tag, $this->expression);
38 $exp->setPolicy(_PHPTAL_ES_RECEIVER_IS_TEMP);
39 $err = $exp->prepare();
40 if (PEAR::isError($err)) { return $err; }
42 // more than one attribute to translate
43 if ($exp->countSubs() > 0) {
44 foreach ($exp->subs() as $subExpression) {
45 $err = $this->_attributeExpression($g, $tag, $subExpression);
46 if (PEAR::isError($err)) { return $err; }
48 } else {
49 $err = $this->_attributeExpression($g, $tag, $exp);
50 if (PEAR::isError($err)) { return $err; }
53 // echo non overwritten attributes
54 foreach ($attributes as $key=>$value) {
55 $test_key = strtolower($key);
56 if (!in_array($test_key, $this->_overwritten)) {
57 // beware of xhtml boolean attributes
58 if ($tag->_parser->_outputMode() == PHPTAL_XHTML
59 && in_array($test_key, $_phptal_xhtml_boolean_attributes)) {
60 $g->doPrintString(' '.$key);
61 } else {
62 $g->doPrintString(' '.$key.'="');
63 $g->doPrintString($value);
64 $g->doPrintString('"');
69 if ($tag->_isXHTMLEmptyElement()) {
70 $g->doPrintString(' />');
71 return;
74 if ($tag->hasContent()) {
75 $g->doPrintString('>');
76 } else {
77 $g->doPrintString('/>');
80 $err = $tag->generateContent($g);
81 if (PEAR::isError($err)) { return $err; }
83 if ($tag->hasContent()) {
84 $g->doPrintString('</', $tag->name(), '>');
88 function _attributeExpression(&$g, &$tag, &$subExpression)
90 $attributes = $tag->attributes();
91 $default = false;
92 $name = strtolower($subExpression->getReceiver());
94 // set default attribute value in $default variable
95 if (preg_match('/\bdefault\b/sm', $subExpression->_src)) {
96 if (!array_key_exists($name, $attributes)) {
97 $g->doAffectResult('$__default__', 'false');
98 } else {
99 $value = $attributes[$name];
100 $value = str_replace('\'', '\\\'', $value);
101 $g->doAffectResult('$__default__', '\''. $value . '\'');
103 $default = true;
106 $real = str_replace('__phptales_dd__', ':', $name);
107 $this->_overwritten[] = $real;
108 $err = $subExpression->generate();
109 if (PEAR::isError($err)) { return $err; }
111 $this->_printAttribute($g, $tag, $real, $name, $default);
114 function _printAttribute(&$g, &$tag, $realName, $varName, $default=false)
116 global $_phptal_xhtml_boolean_attributes;
118 // watch boolean attributes on XHTML mode
119 if ($tag->_parser->_outputMode() == PHPTAL_XHTML
120 && in_array($realName, $_phptal_xhtml_boolean_attributes)) {
121 $g->doIf('!PEAR::isError($'.$varName.') && $'.$varName);
122 $g->doPrintString(' '.$realName);
123 $g->endBlock();
124 } elseif ($default) {
125 $g->doIf('!PEAR::isError($'.$varName.') && false !== $'.$varName.' && null !== $'.$varName);
126 $g->doPrintString(' '. $realName . '="');
127 $code = sprintf('$__tpl__->_translate(trim($%s))', $varName);
128 $g->doPrintRes($code, true);
129 // $g->doPrintVar($varName);
130 $g->doPrintString('"');
131 $g->endBlock();
132 } else {
133 $g->doPrintString(' '. $realName . '="');
134 $code = sprintf('$__tpl__->_translate(trim($%s))', $varName);
135 $g->doPrintRes($code, true);
136 // $g->doPrintVar($varName);
137 $g->doPrintString('"');