| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TGettext_PO class file.
|
|
|
4 |
*
|
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
|
6 |
* it under the terms of the BSD License.
|
|
|
7 |
*
|
|
|
8 |
* Copyright(c) 2004 by Qiang Xue. All rights reserved.
|
|
|
9 |
*
|
|
|
10 |
* To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
|
|
|
11 |
* The latest version of PRADO can be obtained from:
|
|
|
12 |
* {@link http://prado.sourceforge.net/}
|
|
|
13 |
*
|
|
|
14 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
15 |
* @version $Revision: 1.2 $ $Date: 2005/01/05 03:15:14 $
|
|
|
16 |
* @package System.I18N.core
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
// +----------------------------------------------------------------------+
|
|
|
20 |
// | PEAR :: File :: Gettext :: PO |
|
|
|
21 |
// +----------------------------------------------------------------------+
|
|
|
22 |
// | This source file is subject to version 3.0 of the PHP license, |
|
|
|
23 |
// | that is available at http://www.php.net/license/3_0.txt |
|
|
|
24 |
// | If you did not receive a copy of the PHP license and are unable |
|
|
|
25 |
// | to obtain it through the world-wide-web, please send a note to |
|
|
|
26 |
// | license@php.net so we can mail you a copy immediately. |
|
|
|
27 |
// +----------------------------------------------------------------------+
|
|
|
28 |
// | Copyright (c) 2004 Michael Wallner <mike@iworks.at> |
|
|
|
29 |
// +----------------------------------------------------------------------+
|
|
|
30 |
//
|
|
|
31 |
// $Id: PO.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* File::Gettext::PO
|
|
|
35 |
*
|
|
|
36 |
* @author Michael Wallner <mike@php.net>
|
|
|
37 |
* @license PHP License
|
|
|
38 |
*/
|
|
|
39 |
|
|
|
40 |
require_once dirname(__FILE__).'/TGettext.php';
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* File_Gettext_PO
|
|
|
44 |
*
|
|
|
45 |
* GNU PO file reader and writer.
|
|
|
46 |
*
|
|
|
47 |
* @author Michael Wallner <mike@php.net>
|
|
|
48 |
* @version $Revision: 1.2 $
|
|
|
49 |
* @access public
|
|
|
50 |
* @package System.I18N.core
|
|
|
51 |
*/
|
|
|
52 |
class TGettext_PO extends TGettext
|
|
|
53 |
{
|
|
|
54 |
/**
|
|
|
55 |
* Constructor
|
|
|
56 |
*
|
|
|
57 |
* @access public
|
|
|
58 |
* @return object File_Gettext_PO
|
|
|
59 |
* @param string path to GNU PO file
|
|
|
60 |
*/
|
|
|
61 |
function TGettext_PO($file = '')
|
|
|
62 |
{
|
|
|
63 |
$this->file = $file;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Load PO file
|
|
|
68 |
*
|
|
|
69 |
* @access public
|
|
|
70 |
* @return mixed Returns true on success or PEAR_Error on failure.
|
|
|
71 |
* @param string $file
|
|
|
72 |
*/
|
|
|
73 |
function load($file = null)
|
|
|
74 |
{
|
|
|
75 |
if (!isset($file)) {
|
|
|
76 |
$file = $this->file;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// load file
|
|
|
80 |
if (!$contents = @file($file)) {
|
|
|
81 |
return false;
|
|
|
82 |
}
|
|
|
83 |
$contents = implode('', $contents);
|
|
|
84 |
|
|
|
85 |
// match all msgid/msgstr entries
|
|
|
86 |
$matched = preg_match_all(
|
|
|
87 |
'/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+' .
|
|
|
88 |
'(msgstr\s+("([^"]|\\\\")*?"\s*)+)/',
|
|
|
89 |
$contents, $matches
|
|
|
90 |
);
|
|
|
91 |
unset($contents);
|
|
|
92 |
|
|
|
93 |
if (!$matched) {
|
|
|
94 |
return false;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
// get all msgids and msgtrs
|
|
|
98 |
for ($i = 0; $i < $matched; $i++) {
|
|
|
99 |
$msgid = preg_replace(
|
|
|
100 |
'/\s*msgid\s*"(.*)"\s*/s', '\\1', $matches[1][$i]);
|
|
|
101 |
$msgstr= preg_replace(
|
|
|
102 |
'/\s*msgstr\s*"(.*)"\s*/s', '\\1', $matches[4][$i]);
|
|
|
103 |
$this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// check for meta info
|
|
|
107 |
if (isset($this->strings[''])) {
|
|
|
108 |
$this->meta = parent::meta2array($this->strings['']);
|
|
|
109 |
unset($this->strings['']);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
return true;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Save PO file
|
|
|
117 |
*
|
|
|
118 |
* @access public
|
|
|
119 |
* @return mixed Returns true on success or PEAR_Error on failure.
|
|
|
120 |
* @param string $file
|
|
|
121 |
*/
|
|
|
122 |
function save($file = null)
|
|
|
123 |
{
|
|
|
124 |
if (!isset($file)) {
|
|
|
125 |
$file = $this->file;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// open PO file
|
|
|
129 |
if (!is_resource($fh = @fopen($file, 'w'))) {
|
|
|
130 |
return false;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
// lock PO file exclusively
|
|
|
134 |
if (!flock($fh, LOCK_EX)) {
|
|
|
135 |
fclose($fh);
|
|
|
136 |
return false;
|
|
|
137 |
}
|
|
|
138 |
// write meta info
|
|
|
139 |
if (count($this->meta)) {
|
|
|
140 |
$meta = 'msgid ""' . "\nmsgstr " . '""' . "\n";
|
|
|
141 |
foreach ($this->meta as $k => $v) {
|
|
|
142 |
$meta .= '"' . $k . ': ' . $v . '\n"' . "\n";
|
|
|
143 |
}
|
|
|
144 |
fwrite($fh, $meta . "\n");
|
|
|
145 |
}
|
|
|
146 |
// write strings
|
|
|
147 |
foreach ($this->strings as $o => $t) {
|
|
|
148 |
fwrite($fh,
|
|
|
149 |
'msgid "' . parent::prepare($o, true) . '"' . "\n" .
|
|
|
150 |
'msgstr "' . parent::prepare($t, true) . '"' . "\n\n"
|
|
|
151 |
);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
//done
|
|
|
155 |
@flock($fh, LOCK_UN);
|
|
|
156 |
@fclose($fh);
|
|
|
157 |
chmod($file,PRADO_CHMOD);
|
|
|
158 |
return true;
|
|
|
159 |
}
|
|
|
160 |
}
|