PageRenderTime 73ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/trunk/library/Zend/Translate/Adapter/Tmx.php

https://github.com/jorgenils/zend-framework
PHP | 188 lines | 119 code | 18 blank | 51 comment | 15 complexity | 128c44e8f93bb608b2b986c660b93cf3 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Translate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Date.php 2498 2006-12-23 22:13:38Z thomas $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Locale */
  22. require_once 'Zend/Locale.php';
  23. /** Zend_Translate_Adapter */
  24. require_once 'Zend/Translate/Adapter.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Translate
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
  32. // Internal variables
  33. private $_file = false;
  34. private $_cleared = array();
  35. private $_tu = null;
  36. private $_tuv = null;
  37. private $_seg = null;
  38. private $_content = null;
  39. private $_defined = false;
  40. /**
  41. * Generates the tmx adapter
  42. * This adapter reads with php's xml_parser
  43. *
  44. * @param string $data Translation data
  45. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  46. * see Zend_Locale for more information
  47. * @param array $options OPTIONAL Options to set
  48. */
  49. public function __construct($data, $locale = null, array $options = array())
  50. {
  51. parent::__construct($data, $locale, $options);
  52. }
  53. /**
  54. * Load translation data (TMX file reader)
  55. *
  56. * @param string $filename TMX file to add, full path must be given for access
  57. * @param string $locale Locale has no effect for TMX because TMX defines all languages within
  58. * the source file
  59. * @param array $option OPTIONAL Options to use
  60. * @throws Zend_Translation_Exception
  61. */
  62. protected function _loadTranslationData($filename, $locale, array $options = array())
  63. {
  64. $options = $this->_options + $options;
  65. if ($options['clear']) {
  66. $this->_translate = array();
  67. }
  68. if ((in_array('defined_language', $options)) and !empty($options['defined_language'])) {
  69. $this->_defined = true;
  70. }
  71. if (!is_readable($filename)) {
  72. require_once 'Zend/Translate/Exception.php';
  73. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  74. }
  75. $encoding = $this->_findEncoding($filename);
  76. $this->_file = xml_parser_create($encoding);
  77. xml_set_object($this->_file, $this);
  78. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  79. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  80. xml_set_character_data_handler($this->_file, "_contentElement");
  81. if (!xml_parse($this->_file, file_get_contents($filename))) {
  82. $ex = sprintf('XML error: %s at line %d',
  83. xml_error_string(xml_get_error_code($this->_file)),
  84. xml_get_current_line_number($this->_file));
  85. xml_parser_free($this->_file);
  86. require_once 'Zend/Translate/Exception.php';
  87. throw new Zend_Translate_Exception($ex);
  88. }
  89. }
  90. private function _startElement($file, $name, $attrib)
  91. {
  92. if ($this->_seg !== null) {
  93. $this->_content .= "<".$name;
  94. foreach($attrib as $key => $value) {
  95. $this->_content .= " $key=\"$value\"";
  96. }
  97. $this->_content .= ">";
  98. } else {
  99. switch(strtolower($name)) {
  100. case 'tu':
  101. if (isset($attrib['tuid']) === true) {
  102. $this->_tu = $attrib['tuid'];
  103. }
  104. break;
  105. case 'tuv':
  106. if (isset($attrib['xml:lang']) === true) {
  107. $this->_tuv = $attrib['xml:lang'];
  108. if (isset($this->_translate[$this->_tuv]) === false) {
  109. $this->_translate[$this->_tuv] = array();
  110. }
  111. }
  112. break;
  113. case 'seg':
  114. $this->_seg = true;
  115. $this->_content = null;
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. }
  122. private function _endElement($file, $name)
  123. {
  124. if (($this->_seg !== null) and ($name !== 'seg')) {
  125. $this->_content .= "</".$name.">";
  126. } else {
  127. switch (strtolower($name)) {
  128. case 'tu':
  129. $this->_tu = null;
  130. break;
  131. case 'tuv':
  132. $this->_tuv = null;
  133. break;
  134. case 'seg':
  135. $this->_seg = null;
  136. if (!empty($this->_content) or (isset($this->_translate[$this->_tuv][$this->_tu]) === false)) {
  137. $this->_translate[$this->_tuv][$this->_tu] = $this->_content;
  138. }
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. }
  145. private function _contentElement($file, $data)
  146. {
  147. if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
  148. $this->_content .= $data;
  149. }
  150. }
  151. private function _findEncoding($filename)
  152. {
  153. $file = file_get_contents($filename, null, null, 0, 100);
  154. if (strpos($file, "encoding") !== false) {
  155. $encoding = substr($file, strpos($file, "encoding") + 10);
  156. $encoding = substr($encoding, 0, strpos($encoding, '"'));
  157. return $encoding;
  158. }
  159. return 'UTF-8';
  160. }
  161. /**
  162. * Returns the adapter name
  163. *
  164. * @return string
  165. */
  166. public function toString()
  167. {
  168. return "Tmx";
  169. }
  170. }