block.t.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. */
  29. /**
  30. * Replaces arguments in a string with their values.
  31. * Arguments are represented by % followed by their number.
  32. *
  33. * @param string Source string
  34. * @param mixed Arguments, can be passed in an array or through single variables.
  35. * @returns string Modified string
  36. */
  37. function smarty_gettext_strarg($str)
  38. {
  39. $tr = array();
  40. $p = 0;
  41. for ($i=1; $i < func_num_args(); $i++) {
  42. $arg = func_get_arg($i);
  43. if (is_array($arg)) {
  44. foreach ($arg as $aarg) {
  45. $tr['%'.++$p] = $aarg;
  46. }
  47. } else {
  48. $tr['%'.++$p] = $arg;
  49. }
  50. }
  51. return strtr($str, $tr);
  52. }
  53. /**
  54. * Smarty block function, provides gettext support for smarty.
  55. *
  56. * The block content is the text that should be translated.
  57. *
  58. * Any parameter that is sent to the function will be represented as %n in the translation text,
  59. * where n is 1 for the first parameter. The following parameters are reserved:
  60. * - escape - sets escape mode:
  61. * - 'html' for HTML escaping, this is the default.
  62. * - 'js' for javascript escaping.
  63. * - 'url' for url escaping.
  64. * - 'no'/'off'/0 - turns off escaping
  65. * - plural - The plural version of the text (2nd parameter of ngettext())
  66. * - count - The item count for plural mode (3rd parameter of ngettext())
  67. */
  68. function smarty_block_t($params, $text, &$smarty)
  69. {
  70. $text = stripslashes($text);
  71. // set escape mode
  72. if (isset($params['escape'])) {
  73. $escape = $params['escape'];
  74. unset($params['escape']);
  75. }
  76. // set plural version
  77. if (isset($params['plural'])) {
  78. $plural = $params['plural'];
  79. unset($params['plural']);
  80. // set count
  81. if (isset($params['count'])) {
  82. $count = $params['count'];
  83. unset($params['count']);
  84. }
  85. }
  86. // use plural if required parameters are set
  87. if (isset($count) && isset($plural)) {
  88. $text = ngettext($text, $plural, $count);
  89. } else { // use normal
  90. $text = gettext($text);
  91. }
  92. // run strarg if there are parameters
  93. if (count($params)) {
  94. $text = smarty_gettext_strarg($text, $params);
  95. }
  96. if (!isset($escape) || $escape == 'html') { // html escape, default
  97. $text = nl2br(htmlspecialchars($text));
  98. } elseif (isset($escape)) {
  99. switch ($escape) {
  100. case 'javascript':
  101. case 'js':
  102. // javascript escape
  103. $text = str_replace('\'', '\\\'', stripslashes($text));
  104. break;
  105. case 'url':
  106. // url escape
  107. $text = urlencode($text);
  108. break;
  109. }
  110. }
  111. return $text;
  112. }
  113. ?>