PageRenderTime 97ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/popoon/components/transformers/i18n.php

https://github.com/ifeghali/planet-php
PHP | 208 lines | 110 code | 26 blank | 72 comment | 16 complexity | 5fafd939453ed512ee6069563ca17729 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | popoon |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2001,2002,2003,2004 Bitflux GmbH |
  6. // +----------------------------------------------------------------------+
  7. // | Licensed under the Apache License, Version 2.0 (the 'License'); |
  8. // | you may not use this file except in compliance with the License. |
  9. // | You may obtain a copy of the License at |
  10. // | http://www.apache.org/licenses/LICENSE-2.0 |
  11. // | Unless required by applicable law or agreed to in writing, software |
  12. // | distributed under the License is distributed on an 'AS IS' BASIS, |
  13. // | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  14. // | implied. See the License for the specific language governing |
  15. // | permissions and limitations under the License. |
  16. // +----------------------------------------------------------------------+
  17. // | Author: Christian Stocker <chregu@bitflux.ch> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: i18n.php 3294 2004-12-29 09:31:01Z chregu $
  21. /**
  22. * A translator, which tries to implement the i18n transformer from cocoon.
  23. *
  24. * See http://cocoon.apache.org/2.1/userdocs/transformers/i18n-transformer.html
  25. * for an introduction.
  26. *
  27. * If you want to use it, add the following to your sitemap
  28. *
  29. * <map:transform type="i18n" src="xml/catalog">
  30. * <map:parameter name="locale" value="{lang}"/>
  31. * <map:parameter name="driver" value="xml"/>
  32. * </map:transform>
  33. *
  34. * There are (or will be) different drivers for getting the values,
  35. * currently only a xml driver is available. See the source comments
  36. * for more details.
  37. *
  38. * A DB driver is planned.
  39. *
  40. *
  41. * @author Christian Stocker <chregu@bitflux.ch>
  42. * @version $Id: i18n.php 3294 2004-12-29 09:31:01Z chregu $
  43. * @package popoon
  44. */
  45. class popoon_components_transformers_i18n extends popoon_components_transformer {
  46. public $XmlFormat = 'DomDocument';
  47. public $name = 'i18n';
  48. function __construct ($sitemap) {
  49. parent::__construct($sitemap);
  50. if (!defined('I18NNS')) {
  51. define('I18NNS', 'http://apache.org/cocoon/i18n/2.1');
  52. }
  53. }
  54. function init($attribs) {
  55. parent::init($attribs);
  56. }
  57. function DomStart(&$xml) {
  58. $src = $this->getAttrib("src");
  59. $lang = $this->getParameterDefault("locale");
  60. setlocale(LC_ALL,$lang);
  61. $d = popoon_classes_i18n::getDriverInstance($src, $lang, $this->getParameterDefault("driver"));
  62. $ctx = new domxpath($xml);
  63. $ctx->registerNamespace("i18n",I18NNS);
  64. //check all "normal" i18n: elements
  65. $res = $ctx->query("//i18n:*");
  66. foreach($res as $node) {
  67. switch ($node->localName) {
  68. case 'text':
  69. $this->methodText($node,$d);
  70. break;
  71. case 'number':
  72. $this->methodNumber($node);
  73. break;
  74. case 'date-time':
  75. $this->methodDateTime($node);
  76. break;
  77. }
  78. }
  79. //check all i18n attributes
  80. $res = $ctx->query("//@i18n:attr");
  81. foreach($res as $node) {
  82. foreach (explode(" ",$node->value) as $attrName) {
  83. if ($key = $node->parentNode->getAttribute($attrName)) {
  84. if (!$locText = $d->getText($key)) {
  85. $locText = $key;
  86. }
  87. $node->parentNode->setAttribute($attrName,$locText);
  88. }
  89. }
  90. $node->parentNode->removeAttributeNode($node);
  91. }
  92. // check all i18n:translate elements
  93. $res = $ctx->query("//i18n:translate");
  94. foreach($res as $node) {
  95. $this->methodTranslate($node,$ctx);
  96. }
  97. }
  98. protected function methodText($text,$d) {
  99. if ($text->hasAttributeNS(I18NNS,"key")) {
  100. $key = $text->getAttributeNS(I18NNS,"key");
  101. } else {
  102. $key = $text->nodeValue;
  103. }
  104. if (!$locText = $d->getText($key)) {
  105. $locText = $key;
  106. }
  107. $text->parentNode->replaceChild($text->ownerDocument->createTextNode( $locText),$text);
  108. }
  109. //i18n:date-time
  110. /* only i18n:date-time is supported right now
  111. it uses the strftime format of PHP not the java date format, eg.
  112. pattern should be "%d:%b:%Y" and not "dd:MMM:yyyy".
  113. short/medium/long/full are also not implemented. Use
  114. %c, %x and %X as an alternative.
  115. */
  116. protected function methodDateTime($node) {
  117. $pattern = $node->getAttribute("pattern");
  118. $src = $node->getAttribute("src-pattern");
  119. $value = $node->getAttribute("value");
  120. if (!$value) {
  121. $value = time();
  122. }
  123. else if ($src && function_exists("strptime")) {
  124. $t = strptime($value,$src);
  125. $value = mktime($t['tm_hour'],$t['tm_min'],$t['tm_sec'],$t['tm_mon'],$t['tm_mday'],$t['tm_year']);
  126. } else {
  127. $value = strtotime($value);
  128. }
  129. $value = strftime($pattern,$value);
  130. $node->parentNode->replaceChild($node->ownerDocument->createTextNode($value),$node);
  131. }
  132. /* <i18n:number type="int-currency-no-unit" value="170374" />
  133. <i18n:number type="int-currency" value="170374" />
  134. and
  135. <i18n:number type="percent" value="1.2" />
  136. are not supported yet
  137. */
  138. protected function methodNumber($node) {
  139. switch ($node->getAttribute("type")) {
  140. case "currency":
  141. if ($digits = $node->getAttribute("fraction-digits")) {
  142. $value = money_format("%.${digits}n",$node->getAttribute("value"));
  143. } else {
  144. $value = money_format("%n",$node->getAttribute("value"));
  145. }
  146. break;
  147. case "printf":
  148. $value = sprintf($node->getAttribute("pattern"),$node->getAttribute("value"));
  149. }
  150. $node->parentNode->replaceChild($node->ownerDocument->createTextNode($value),$node);
  151. }
  152. /**
  153. Example:
  154. <i18n:translate>
  155. <i18n:text>Some {0} was inserted {foo}.</i18n:text>
  156. <i18n:param>text </i18n:param>
  157. <i18n:param name="foo"><i18n:text>here</i18n:text></i18n:param>
  158. </i18n:translate>
  159. {1} and {foo} would be the same in the above example..
  160. **/
  161. protected function methodTranslate($node,$ctx) {
  162. $resParam = $ctx->query("//i18n:param");
  163. $params = array();
  164. $i = 0;
  165. foreach ($resParam as $paramNode) {
  166. if ($name = $paramNode->getAttribute("name")) {
  167. $params[$name] = $paramNode->nodeValue;
  168. }
  169. $params[$i] = $paramNode->nodeValue;
  170. $i++;
  171. $node->removeChild($paramNode);
  172. }
  173. $value = $node->nodeValue;
  174. $value = preg_replace("/\{([a-zA-Z0-9_]*)\}/e","\$params['$1']",$value);
  175. $node->parentNode->replaceChild($node->ownerDocument->createTextNode($value),$node);
  176. }
  177. }
  178. ?>