PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/kotal/vendor/phptal/PHPTAL/GetTextTranslator.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 183 lines | 96 code | 18 blank | 69 comment | 14 complexity | d0c431f09c25ec7558db7840c1d8982d MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPTAL templating engine
  4. *
  5. * PHP Version 5
  6. *
  7. * @category HTML
  8. * @package PHPTAL
  9. * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
  10. * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
  11. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  12. * @version SVN: $Id$
  13. * @link http://phptal.org/
  14. */
  15. /**
  16. * PHPTAL_TranslationService gettext implementation.
  17. *
  18. * Because gettext is the most common translation library in use, this
  19. * implementation is shipped with the PHPTAL library.
  20. *
  21. * Please refer to the PHPTAL documentation for usage examples.
  22. *
  23. * @package PHPTAL
  24. * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
  25. */
  26. class PHPTAL_GetTextTranslator implements PHPTAL_TranslationService
  27. {
  28. private $_vars = array();
  29. private $_currentDomain;
  30. private $_encoding = 'UTF-8';
  31. private $_canonicalize = false;
  32. public function __construct()
  33. {
  34. if (!function_exists('gettext')) throw new PHPTAL_ConfigurationException("Gettext not installed");
  35. $this->useDomain("messages"); // PHP bug #21965
  36. }
  37. /**
  38. * set encoding that is used by template and is expected from gettext
  39. * the default is UTF-8
  40. *
  41. * @param string $enc encoding name
  42. */
  43. public function setEncoding($enc)
  44. {
  45. $this->_encoding = $enc;
  46. }
  47. /**
  48. * if true, all non-ASCII characters in keys will be converted to C<xxx> form. This impacts performance.
  49. * by default keys will be passed to gettext unmodified.
  50. *
  51. * This function is only for backwards compatibility
  52. *
  53. * @param bool $bool enable old behavior
  54. */
  55. public function setCanonicalize($bool)
  56. {
  57. $this->_canonicalize = $bool;
  58. }
  59. /**
  60. * It expects locale names as arguments.
  61. * Choses first one that works.
  62. *
  63. * setLanguage("en_US.utf8","en_US","en_GB","en")
  64. *
  65. * @return string - chosen language
  66. */
  67. public function setLanguage(/*...*/)
  68. {
  69. $langs = func_get_args();
  70. $langCode = $this->trySettingLanguages(LC_ALL, $langs);
  71. if ($langCode) return $langCode;
  72. if (defined("LC_MESSAGES")) {
  73. $langCode = $this->trySettingLanguages(LC_MESSAGES, $langs);
  74. if ($langCode) return $langCode;
  75. }
  76. throw new PHPTAL_ConfigurationException('Language(s) code(s) "'.implode(', ', $langs).'" not supported by your system');
  77. }
  78. private function trySettingLanguages($category, array $langs)
  79. {
  80. foreach ($langs as $langCode) {
  81. putenv("LANG=$langCode");
  82. putenv("LC_ALL=$langCode");
  83. putenv("LANGUAGE=$langCode");
  84. if (setlocale($category, $langCode)) {
  85. return $langCode;
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * Adds translation domain (usually it's the same as name of .po file [without extension])
  92. *
  93. * Encoding must be set before calling addDomain!
  94. */
  95. public function addDomain($domain, $path='./locale/')
  96. {
  97. bindtextdomain($domain, $path);
  98. if ($this->_encoding) {
  99. bind_textdomain_codeset($domain, $this->_encoding);
  100. }
  101. $this->useDomain($domain);
  102. }
  103. /**
  104. * Switches to one of the domains previously set via addDomain()
  105. *
  106. * @param string $domain name of translation domain to be used.
  107. *
  108. * @return string - old domain
  109. */
  110. public function useDomain($domain)
  111. {
  112. $old = $this->_currentDomain;
  113. $this->_currentDomain = $domain;
  114. textdomain($domain);
  115. return $old;
  116. }
  117. /**
  118. * used by generated PHP code. Don't use directly.
  119. */
  120. public function setVar($key, $value)
  121. {
  122. $this->_vars[$key] = $value;
  123. }
  124. /**
  125. * translate given key.
  126. *
  127. * @param bool $htmlencode if true, output will be HTML-escaped.
  128. */
  129. public function translate($key, $htmlencode=true)
  130. {
  131. if ($this->_canonicalize) $key = self::_canonicalizeKey($key);
  132. $value = gettext($key);
  133. if ($htmlencode) {
  134. $value = htmlspecialchars($value, ENT_QUOTES, $this->_encoding);
  135. }
  136. while (preg_match('/\${(.*?)\}/sm', $value, $m)) {
  137. list($src, $var) = $m;
  138. if (!array_key_exists($var, $this->_vars)) {
  139. throw new PHPTAL_VariableNotFoundException('Interpolation error. Translation uses ${'.$var.'}, which is not defined in the template (via i18n:name)');
  140. }
  141. $value = str_replace($src, $this->_vars[$var], $value);
  142. }
  143. return $value;
  144. }
  145. /**
  146. * For backwards compatibility only.
  147. */
  148. private static function _canonicalizeKey($key_)
  149. {
  150. $result = "";
  151. $key_ = trim($key_);
  152. $key_ = str_replace("\n", "", $key_);
  153. $key_ = str_replace("\r", "", $key_);
  154. for ($i = 0; $i<strlen($key_); $i++) {
  155. $c = $key_[$i];
  156. $o = ord($c);
  157. if ($o < 5 || $o > 127) {
  158. $result .= 'C<'.$o.'>';
  159. } else {
  160. $result .= $c;
  161. }
  162. }
  163. return $result;
  164. }
  165. }