block.t.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, &$repeat)
  69. {
  70. if (!$repeat) {
  71. $text = stripslashes($text);
  72. // set escape mode
  73. if (isset($params['escape'])) {
  74. $escape = $params['escape'];
  75. unset($params['escape']);
  76. }
  77. // set plural version
  78. if (isset($params['plural'])) {
  79. $plural = $params['plural'];
  80. unset($params['plural']);
  81. // set count
  82. if (isset($params['count'])) {
  83. $count = $params['count'];
  84. unset($params['count']);
  85. }
  86. }
  87. $resource = $smarty->getTemplateVars('resource');
  88. // use plural if required parameters are set
  89. if (isset($count) && isset($plural)) {
  90. if ($resource)
  91. $text = $resource->ngettext($text, $plural, $count);
  92. else
  93. $text = $count == 1 ? $text : $plural;
  94. } else { // use normal
  95. if ($resource)
  96. $text = $resource->translate($text);
  97. }
  98. // run strarg if there are parameters
  99. if (count($params)) {
  100. $text = smarty_gettext_strarg($text, $params);
  101. }
  102. if (!isset($escape) || $escape == 'html') { // html escape, default
  103. $text = nl2br(htmlspecialchars($text));
  104. } elseif (isset($escape)) {
  105. switch ($escape) {
  106. case 'javascript':
  107. case 'js':
  108. // javascript escape
  109. $text = str_replace('\'', '\\\'', stripslashes($text));
  110. break;
  111. case 'url':
  112. // url escape
  113. $text = urlencode($text);
  114. break;
  115. }
  116. }
  117. return $text;
  118. }
  119. }
  120. ?>