PageRenderTime 38ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/i18n/Gettext/TGettext.class.php

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