PO.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: File :: Gettext :: PO |
  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: PO.php 9856 2008-06-25 11:33:49Z fabien $
  15. /**
  16. * File::Gettext::PO
  17. *
  18. * @author Michael Wallner <mike@php.net>
  19. * @license PHP License
  20. */
  21. require_once dirname(__FILE__).'/TGettext.class.php';
  22. /**
  23. * File_Gettext_PO
  24. *
  25. * GNU PO file reader and writer.
  26. *
  27. * @author Michael Wallner <mike@php.net>
  28. * @version $Revision: 9856 $
  29. * @access public
  30. * @package System.I18N.core
  31. */
  32. class TGettext_PO extends TGettext
  33. {
  34. /**
  35. * Constructor
  36. *
  37. * @access public
  38. * @return object File_Gettext_PO
  39. * @param string path to GNU PO file
  40. */
  41. function TGettext_PO($file = '')
  42. {
  43. $this->file = $file;
  44. }
  45. /**
  46. * Load PO file
  47. *
  48. * @access public
  49. * @return mixed Returns true on success or PEAR_Error on failure.
  50. * @param string $file
  51. */
  52. function load($file = null)
  53. {
  54. if (!isset($file)) {
  55. $file = $this->file;
  56. }
  57. // load file
  58. if (!$contents = @file($file)) {
  59. return false;
  60. }
  61. $contents = implode('', $contents);
  62. // match all msgid/msgstr entries
  63. $matched = preg_match_all(
  64. '/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+' .
  65. '(msgstr\s+("([^"]|\\\\")*?"\s*)+)/',
  66. $contents, $matches
  67. );
  68. unset($contents);
  69. if (!$matched) {
  70. return false;
  71. }
  72. // get all msgids and msgtrs
  73. for ($i = 0; $i < $matched; $i++) {
  74. $msgid = preg_replace(
  75. '/\s*msgid\s*"(.*)"\s*/s', '\\1', $matches[1][$i]);
  76. $msgstr= preg_replace(
  77. '/\s*msgstr\s*"(.*)"\s*/s', '\\1', $matches[4][$i]);
  78. $this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
  79. }
  80. // check for meta info
  81. if (isset($this->strings[''])) {
  82. $this->meta = parent::meta2array($this->strings['']);
  83. unset($this->strings['']);
  84. }
  85. return true;
  86. }
  87. /**
  88. * Save PO file
  89. *
  90. * @access public
  91. * @return mixed Returns true on success or PEAR_Error on failure.
  92. * @param string $file
  93. */
  94. function save($file = null)
  95. {
  96. if (!isset($file)) {
  97. $file = $this->file;
  98. }
  99. // open PO file
  100. if (!is_resource($fh = @fopen($file, 'w'))) {
  101. return false;
  102. }
  103. // lock PO file exclusively
  104. if (!flock($fh, LOCK_EX)) {
  105. fclose($fh);
  106. return false;
  107. }
  108. // write meta info
  109. if (count($this->meta)) {
  110. $meta = 'msgid ""' . "\nmsgstr " . '""' . "\n";
  111. foreach ($this->meta as $k => $v) {
  112. $meta .= '"' . $k . ': ' . $v . '\n"' . "\n";
  113. }
  114. fwrite($fh, $meta . "\n");
  115. }
  116. // write strings
  117. foreach ($this->strings as $o => $t) {
  118. fwrite($fh,
  119. 'msgid "' . parent::prepare($o, true) . '"' . "\n" .
  120. 'msgstr "' . parent::prepare($t, true) . '"' . "\n\n"
  121. );
  122. }
  123. //done
  124. @flock($fh, LOCK_UN);
  125. @fclose($fh);
  126. return true;
  127. }
  128. }