1 <?php
2 /**
3 * block.t.php - Smarty gettext block plugin
4 *
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 *
21 * Installation: simply copy this file to the smarty plugins directory.
22 *
23 * @package smarty-gettext
24 * @version $Id: block.t.php,v 1.1 2005/07/27 17:58:56 sagi Exp $
25 * @link http://smarty-gettext.sourceforge.net/
26 * @author Sagi Bashari <sagi@boom.org.il>
27 * @copyright 2004-2005 Sagi Bashari
28 */
30 /**
31 * Replaces arguments in a string with their values.
32 * Arguments are represented by % followed by their number.
33 *
34 * @param string Source string
35 * @param mixed Arguments, can be passed in an array or through single variables.
36 * @returns string Modified string
37 */
38 function smarty_gettext_strarg($str)
39 {
40 $tr = array();
41 $p = 0;
43 for ($i=1; $i < func_num_args(); $i++) {
44 $arg = func_get_arg($i);
46 if (is_array($arg)) {
47 foreach ($arg as $aarg) {
48 $tr['%'.++$p] = $aarg;
49 }
50 } else {
51 $tr['%'.++$p] = $arg;
52 }
53 }
55 return strtr($str, $tr);
56 }
58 /**
59 * Smarty block function, provides gettext support for smarty.
60 *
61 * The block content is the text that should be translated.
62 *
63 * Any parameter that is sent to the function will be represented as %n in the translation text,
64 * where n is 1 for the first parameter. The following parameters are reserved:
65 * - escape - sets escape mode:
66 * - 'html' for HTML escaping, this is the default.
67 * - 'js' for javascript escaping.
68 * - 'url' for url escaping.
69 * - 'no'/'off'/0 - turns off escaping
70 * - plural - The plural version of the text (2nd parameter of ngettext())
71 * - count - The item count for plural mode (3rd parameter of ngettext())
72 */
73 function smarty_block_t($params, $text, &$smarty)
74 {
75 $text = stripslashes($text);
77 // set escape mode
78 if (isset($params['escape'])) {
79 $escape = $params['escape'];
80 unset($params['escape']);
81 }
83 // set plural version
84 if (isset($params['plural'])) {
85 $plural = $params['plural'];
86 unset($params['plural']);
88 // set count
89 if (isset($params['count'])) {
90 $count = $params['count'];
91 unset($params['count']);
92 }
93 }
95 // use plural if required parameters are set
96 if (isset($count) && isset($plural)) {
97 $text = ngettext($text, $plural, $count);
98 } else { // use normal
99 $text = gettext($text);
100 }
102 // run strarg if there are parameters
103 if (count($params)) {
104 $text = smarty_gettext_strarg($text, $params);
105 }
107 if (!isset($escape) || $escape == 'html') { // html escape, default
108 $text = nl2br(htmlspecialchars($text));
109 } elseif (isset($escape)) {
110 switch ($escape) {
111 case 'javascript':
112 case 'js':
113 // javascript escape
114 $text = str_replace('\'', '\\\'', stripslashes($text));
115 break;
116 case 'url':
117 // url escape
118 $text = urlencode($text);
119 break;
120 }
121 }
123 return $text;
124 }
126 ?>