PageRenderTime 25ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-0.9.0/library/Zend/Translate/Adapter/Tmx.php

https://github.com/bhaumik25/zend-framework
PHP | 156 lines | 89 code | 16 blank | 51 comment | 9 complexity | 05ad290f954b33da3893455e7f681a1e 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-2007 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_Exception */
  24. require_once 'Zend/Translate/Exception.php';
  25. /** Zend_Translate_Adapter */
  26. require_once 'Zend/Translate/Adapter.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Translate
  30. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Translate_Adapter_Tmx extends Zend_Translate_Adapter {
  34. // Internal variables
  35. private $_file = false;
  36. private $_cleared = array();
  37. private $_tu = null;
  38. private $_tuv = null;
  39. private $_seg = null;
  40. private $_content = null;
  41. /**
  42. * Generates the tmx adapter
  43. * This adapter reads with php's xml_parser
  44. *
  45. * @param array $options Options for this adapter
  46. * @param string|Zend_Locale $locale OPTIONAL Locale/Language to set, identical with locale identifier,
  47. * see Zend_Locale for more information
  48. */
  49. public function __construct($options, $locale = null)
  50. {
  51. parent::__construct($options, $locale);
  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 boolean $option Clears the complete translation, because TMX defines all languages in one file
  60. * @throws Zend_Translation_Exception
  61. */
  62. protected function _loadTranslationData($filename, $locale, $option = null)
  63. {
  64. if ($option) {
  65. $this->_translate = array();
  66. }
  67. if (!is_readable($filename)) {
  68. throw new Zend_Translate_Exception('Translation file \'' . $filename . '\' is not readable.');
  69. }
  70. $this->_file = xml_parser_create();
  71. xml_set_object($this->_file, $this);
  72. xml_parser_set_option($this->_file, XML_OPTION_CASE_FOLDING, 0);
  73. xml_set_element_handler($this->_file, "_startElement", "_endElement");
  74. xml_set_character_data_handler($this->_file, "_contentElement");
  75. if (!xml_parse($this->_file, file_get_contents($filename))) {
  76. throw new Zend_Translate_Exception(sprintf('XML error: %s at line %d',
  77. xml_error_string(xml_get_error_code($this->_file)),
  78. xml_get_current_line_number($this->_file)));
  79. xml_parser_free($this->_file);
  80. }
  81. }
  82. private function _startElement($file, $name, $attrib)
  83. {
  84. switch(strtolower($name)) {
  85. case 'tu':
  86. if (array_key_exists('tuid', $attrib)) {
  87. $this->_tu = $attrib['tuid'];
  88. }
  89. break;
  90. case 'tuv':
  91. if (array_key_exists('xml:lang', $attrib)) {
  92. $this->_tuv = $attrib['xml:lang'];
  93. if (!array_key_exists($this->_tuv, $this->_translate)) {
  94. $this->_translate[$this->_tuv] = array();
  95. }
  96. }
  97. break;
  98. case 'seg':
  99. $this->_seg = true;
  100. $this->_content = null;
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. private function _endElement($file, $name)
  107. {
  108. switch (strtolower($name)) {
  109. case 'tu':
  110. $this->_tu = null;
  111. break;
  112. case 'tuv':
  113. $this->_tuv = null;
  114. break;
  115. case 'seg':
  116. $this->_seg = null;
  117. if (!empty($this->_content) or !array_key_exists($this->_tu, $this->_translate[$this->_tuv])) {
  118. $this->_translate[$this->_tuv][$this->_tu] = $this->_content;
  119. }
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. private function _contentElement($file, $data)
  126. {
  127. if (($this->_seg !== null) and ($this->_tu !== null) and ($this->_tuv !== null)) {
  128. $this->_content = $data;
  129. }
  130. }
  131. /**
  132. * Returns the adapter name
  133. *
  134. * @return string
  135. */
  136. public function toString()
  137. {
  138. return "Tmx";
  139. }
  140. }