root/trunk/includes/smarty_plugins/block.gettext.php
| Revision 1, 5.5 KB (checked in by anonymous, 7 years ago) |
|---|
| Line | |
|---|---|
| 1 | <?php |
| 2 | /****************************************************** Open .node *** |
| 3 | * Description: |
| 4 | * Status: Stable. |
| 5 | * Author: Alexandre Dath <alexandre@dotnode.com> |
| 6 | * $Id$ |
| 7 | * |
| 8 | * Copyright (C) 2005 Alexandre Dath <alexandre@dotnode.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software Foundation, |
| 22 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | ******************** http://opensource.ikse.net/projects/dotnode ***/ |
| 24 | |
| 25 | /** |
| 26 | * smarty-gettext.php - Gettext support for smarty |
| 27 | * |
| 28 | * ------------------------------------------------------------------------- * |
| 29 | * This library is free software; you can redistribute it and/or * |
| 30 | * modify it under the terms of the GNU Lesser General Public * |
| 31 | * License as published by the Free Software Foundation; either * |
| 32 | * version 2.1 of the License, or (at your option) any later version. * |
| 33 | * * |
| 34 | * This library is distributed in the hope that it will be useful, * |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 37 | * Lesser General Public License for more details. * |
| 38 | * * |
| 39 | * You should have received a copy of the GNU Lesser General Public * |
| 40 | * License along with this library; if not, write to the Free Software * |
| 41 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 42 | * ------------------------------------------------------------------------- * |
| 43 | * |
| 44 | * To register as a smarty block function named 't', use: |
| 45 | * $smarty->register_block('t', 'smarty_translate'); |
| 46 | * |
| 47 | * @package smarty-gettext |
| 48 | * @version $Id: smarty-gettext.php,v 1.1 2004/04/30 11:39:32 sagi Exp $ |
| 49 | * @link http://smarty-gettext.sf.net/ |
| 50 | * @author Sagi Bashari <sagi@boom.org.il> |
| 51 | * @copyright 2004 Sagi Bashari |
| 52 | */ |
| 53 | |
| 54 | /** |
| 55 | * Replace arguments in a string with their values. Arguments are represented by % followed by their number. |
| 56 | * |
| 57 | * @param string Source string |
| 58 | * @param mixed Arguments, can be passed in an array or through single variables. |
| 59 | * @returns string Modified string |
| 60 | */ |
| 61 | function strarg($str) |
| 62 | { |
| 63 | if(trim($str) == '') |
| 64 | return ''; |
| 65 | $tr = array(); |
| 66 | $p = 0; |
| 67 | |
| 68 | for ($i=1; $i < func_num_args(); $i++) { |
| 69 | $arg = func_get_arg($i); |
| 70 | |
| 71 | if (is_array($arg)) { |
| 72 | foreach ($arg as $aarg) { |
| 73 | $tr['%'.++$p] = $aarg; |
| 74 | } |
| 75 | } else { |
| 76 | $tr['%'.++$p] = $arg; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return strtr($str, $tr); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Smarty block function, provides gettext support for smarty. |
| 85 | * |
| 86 | * The block content is the text that should be translated. |
| 87 | * |
| 88 | * Any parameter that is sent to the function will be represented as %n in the translation text, |
| 89 | * where n is 1 for the first parameter. The following parameters are reserved: |
| 90 | * - escape - sets escape mode: |
| 91 | * - 'html' for HTML escaping, this is the default. |
| 92 | * - 'js' for javascript escaping. |
| 93 | * - 'no'/'off'/0 - turns off escaping |
| 94 | * - plural - The plural version of the text (2nd parameter of ngettext()) |
| 95 | * - count - The item count for plural mode (3rd parameter of ngettext()) |
| 96 | */ |
| 97 | function smarty_translate($params, $text, &$smarty) |
| 98 | { |
| 99 | global $_SESSION; |
| 100 | global $lang; |
| 101 | $text = stripslashes($text); |
| 102 | $text_orig = $text; |
| 103 | // if($text!='') error_log($text); |
| 104 | // set escape mode |
| 105 | if (isset($params['escape'])) { |
| 106 | $escape = $params['escape']; |
| 107 | unset($params['escape']); |
| 108 | } |
| 109 | |
| 110 | // set plural version |
| 111 | if (isset($params['plural'])) { |
| 112 | $plural = $params['plural']; |
| 113 | unset($params['plural']); |
| 114 | |
| 115 | // set count |
| 116 | if (isset($params['count'])) { |
| 117 | $count = $params['count']; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // use plural if required parameters are set |
| 122 | if (isset($count) && isset($plural)) { |
| 123 | $text = ngettext($text, $plural, $count); |
| 124 | } else { // use normal |
| 125 | $text = gettext($text); |
| 126 | } |
| 127 | |
| 128 | if($_SESSION['my_login'] == 'calimero' && $lang != 'en_US' && strlen($text_orig)>1 && $text_orig == $text) |
| 129 | { |
| 130 | global $dsn; |
| 131 | $dntp_msgid_data = array( |
| 132 | 'first_see' => $_SERVER['PHP_SELF'], |
| 133 | 'md5' => md5($text_orig), |
| 134 | 'msgid' => $text_orig, |
| 135 | 'date' => time() |
| 136 | ); |
| 137 | if(isset($plural)) |
| 138 | $dntp_msgid_data['msgid_plural'] = $plural; |
| 139 | |
| 140 | error_log(print_r($dntp_msgid_data, true)); |
| 141 | $db =& DB::connect($dsn); |
| 142 | $db->autoExecute('dntp_msgid', $dntp_msgid_data); |
| 143 | if($db->affectedRows>0) |
| 144 | error_log(print_r($dntp_msgid_data, true)); |
| 145 | $db->disconnect(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | // run strarg if there are parameters |
| 150 | if (count($params)) { |
| 151 | $text = strarg($text, $params); |
| 152 | } |
| 153 | |
| 154 | if (!isset($escape) || $escape == 'html') { // html escape, default |
| 155 | $text = nl2br(htmlspecialchars($text)); |
| 156 | } elseif (isset($escape) && ($escape == 'javascript' || $escape == 'js')) { // javascript escape |
| 157 | $text = str_replace('\'','\\\'',stripslashes($text)); |
| 158 | } |
| 159 | return $text; |
| 160 | } |
| 161 | |
| 162 | ?> |
Note: See TracBrowser
for help on using the browser.
