1. TGettext.class.php
  2. /** * File_Gettext * * GNU gettext file reader and writer. * * ################################################################# * # All protected members of this class are public in its childs. # * ################################################################# * * @author Michael Wallner * @version $Revision: 9856 $ * @access public * @package System.I18N.core */
  3. class TGettext
  4. {
  5. /**
  6. * strings
  7. *
  8. * associative array with all [msgid => msgstr] entries
  9. *
  10. * @access protected
  11. * @var array
  12. */
  13. protected $strings = array();
  14. /**
  15. * meta
  16. *
  17. * associative array containing meta
  18. * information like project name or content type
  19. *
  20. * @access protected
  21. * @var array
  22. */
  23. protected $meta = array();
  24. /**
  25. * file path
  26. *
  27. * @access protected
  28. * @var string
  29. */
  30. protected $file = '';
  31. /**
  32. * Factory
  33. *
  34. * @static
  35. * @access public
  36. * @return object Returns File_Gettext_PO or File_Gettext_MO on success
  37. * or PEAR_Error on failure.
  38. * @param string $format MO or PO
  39. * @param string $file path to GNU gettext file
  40. */
  41. static function factory($format, $file = '')
  42. {
  43. $format = strToUpper($format);
  44. $filename = dirname(__FILE__).'/'.$format.'.php';
  45. if (is_file($filename) == false)
  46. throw new Exception ("Class file $file not found");
  47. include_once $filename;
  48. $class = 'TGettext_' . $format;
  49. return new $class($file);
  50. }
  51. /**
  52. * poFile2moFile
  53. *
  54. * That's a simple fake of the 'msgfmt' console command. It reads the
  55. * contents of a GNU PO file and saves them to a GNU MO file.
  56. *
  57. * @static
  58. * @access public
  59. * @return mixed Returns true on success or PEAR_Error on failure.
  60. * @param string $pofile path to GNU PO file
  61. * @param string $mofile path to GNU MO file
  62. */
  63. function poFile2moFile($pofile, $mofile)
  64. {
  65. if (!is_file($pofile)) {
  66. throw new Exception("File $pofile doesn't exist.");
  67. }
  68. include_once dirname(__FILE__).'/PO.php';
  69. $PO = new TGettext_PO($pofile);
  70. if (true !== ($e = $PO->load())) {
  71. return $e;
  72. }
  73. $MO = $PO->toMO();
  74. if (true !== ($e = $MO->save($mofile))) {
  75. return $e;
  76. }
  77. unset($PO, $MO);
  78. return true;
  79. }
  80. /**
  81. * prepare
  82. *
  83. * @static
  84. * @access protected
  85. * @return string
  86. * @param string $string
  87. * @param bool $reverse
  88. */
  89. function prepare($string, $reverse = false)
  90. {
  91. if ($reverse) {
  92. $smap = array('"', "\n", "\t", "\r");
  93. $rmap = array('\"', '\\n"' . "\n" . '"', '\\t', '\\r');
  94. return (string) str_replace($smap, $rmap, $string);
  95. } else {
  96. $string = preg_replace('/"\s+"/', '', $string);
  97. $smap = array('\\n', '\\r', '\\t', '\"');
  98. $rmap = array("\n", "\r", "\t", '"');
  99. return (string) str_replace($smap, $rmap, $string);
  100. }
  101. }
  102. /**
  103. * meta2array
  104. *
  105. * @static
  106. * @access public
  107. * @return array
  108. * @param string $meta
  109. */
  110. function meta2array($meta)
  111. {
  112. $array = array();
  113. foreach (explode("\n", $meta) as $info) {
  114. if ($info = trim($info)) {
  115. list($key, $value) = explode(':', $info, 2);
  116. $array[trim($key)] = trim($value);
  117. }
  118. }
  119. return $array;
  120. }
  121. /**
  122. * toArray
  123. *
  124. * Returns meta info and strings as an array of a structure like that:
  125. * <code>
  126. * array(
  127. * 'meta' => array(
  128. * 'Content-Type' => 'text/plain; charset=iso-8859-1',
  129. * 'Last-Translator' => 'Michael Wallner <mike@iworks.at>',
  130. * 'PO-Revision-Date' => '2004-07-21 17:03+0200',
  131. * 'Language-Team' => 'German <mail@example.com>',
  132. * ),
  133. * 'strings' => array(
  134. * 'All rights reserved' => 'Alle Rechte vorbehalten',
  135. * 'Welcome' => 'Willkommen',
  136. * // ...
  137. * )
  138. * )
  139. * </code>
  140. *
  141. * @see fromArray()
  142. * @access protected
  143. * @return array
  144. */
  145. function toArray()
  146. {
  147. return array('meta' => $this->meta, 'strings' => $this->strings);
  148. }
  149. /**
  150. * fromArray
  151. *
  152. * Assigns meta info and strings from an array of a structure like that:
  153. * <code>
  154. * array(
  155. * 'meta' => array(
  156. * 'Content-Type' => 'text/plain; charset=iso-8859-1',
  157. * 'Last-Translator' => 'Michael Wallner <mike@iworks.at>',
  158. * 'PO-Revision-Date' => date('Y-m-d H:iO'),
  159. * 'Language-Team' => 'German <mail@example.com>',
  160. * ),
  161. * 'strings' => array(
  162. * 'All rights reserved' => 'Alle Rechte vorbehalten',
  163. * 'Welcome' => 'Willkommen',
  164. * // ...
  165. * )
  166. * )
  167. * </code>
  168. *
  169. * @see toArray()
  170. * @access protected
  171. * @return bool
  172. * @param array $array
  173. */
  174. function fromArray($array)
  175. {
  176. if (!array_key_exists('strings', $array)) {
  177. if (count($array) != 2) {
  178. return false;
  179. } else {
  180. list($this->meta, $this->strings) = $array;
  181. }
  182. } else {
  183. $this->meta = @$array['meta'];
  184. $this->strings = @$array['strings'];
  185. }
  186. return true;
  187. }
  188. /**
  189. * toMO
  190. *
  191. * @access protected
  192. * @return object File_Gettext_MO
  193. */
  194. function toMO()
  195. {
  196. include_once dirname(__FILE__).'/MO.php';
  197. $MO = new TGettext_MO;
  198. $MO->fromArray($this->toArray());
  199. return $MO;
  200. }
  201. /**
  202. * toPO
  203. *
  204. * @access protected
  205. * @return object File_Gettext_PO
  206. */
  207. function toPO()
  208. {
  209. include_once dirname(__FILE__).'/PO.php';
  210. $PO = new TGettext_PO;
  211. $PO->fromArray($this->toArray());
  212. return $PO;
  213. }
  214. }

Debug toolbar