PO.php
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Gettext
*
* PHP versions 4 and 5
*
* @category FileFormats
* @package File_Gettext
* @author Michael Wallner <mike@php.net>
* @copyright 2004-2005 Michael Wallner
* @license BSD, revised
* @version CVS: $Id: PO.php,v 1.6 2006/01/07 09:45:25 mike Exp $
* @link http://pear.php.net/package/File_Gettext
*/
/**
* Requires File_Gettext
*/
require_once 'File/Gettext.php';
/**
* File_Gettext_PO
*
* GNU PO file reader and writer.
*
* @author Michael Wallner <mike@php.net>
* @version $Revision: 1.6 $
* @access public
*/
class File_Gettext_PO extends File_Gettext
{
/**
* Constructor
*
* @access public
* @return object File_Gettext_PO
* @param string path to GNU PO file
*/
function File_Gettext_PO($file = '')
{
$this->file = $file;
}
/**
* Load PO file
*
* @access public
* @return mixed Returns true on success or PEAR_Error on failure.
* @param string $file
*/
function load($file = null)
{
$this->strings = array();
if (!isset($file)) {
$file = $this->file;
}
// load file
if (!$contents = @file($file)) {
return parent::raiseError($php_errormsg . ' ' . $file);
}
$contents = implode('', $contents);
// match all msgid/msgstr entries
$matched = preg_match_all(
'/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+' .
'(msgstr\s+("([^"]|\\\\")*?"\s*)+)/',
$contents, $matches
);
unset($contents);
if (!$matched) {
return parent::raiseError('No msgid/msgstr entries found');
}
// get all msgids and msgtrs
for ($i = 0; $i < $matched; $i++) {
$msgid = preg_replace(
'/\s*msgid\s*"(.*)"\s*/s', '\\1', $matches[1][$i]);
$msgstr= preg_replace(
'/\s*msgstr\s*"(.*)"\s*/s', '\\1', $matches[4][$i]);
$this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
}
// check for meta info
if (isset($this->strings[''])) {
$this->meta = parent::meta2array($this->strings['']);
unset($this->strings['']);
}
return true;
}
/**
* Save PO file
*
* @access public
* @return mixed Returns true on success or PEAR_Error on failure.
* @param string $file
*/
function save($file = null)
{
if (!isset($file)) {
$file = $this->file;
}
// open PO file
if (!is_resource($fh = @fopen($file, 'w'))) {
return parent::raiseError($php_errormsg . ' ' . $file);
}
// lock PO file exclusively
if (!@flock($fh, LOCK_EX)) {
@fclose($fh);
return parent::raiseError($php_errmsg . ' ' . $file);
}
// write meta info
if (count($this->meta)) {
$meta = 'msgid ""' . "\nmsgstr " . '""' . "\n";
foreach ($this->meta as $k => $v) {
$meta .= '"' . $k . ': ' . $v . '\n"' . "\n";
}
fwrite($fh, $meta . "\n");
}
// write strings
foreach ($this->strings as $o => $t) {
fwrite($fh,
'msgid "' . parent::prepare($o, true) . '"' . "\n" .
'msgstr "' . parent::prepare($t, true) . '"' . "\n\n"
);
}
//done
@flock($fh, LOCK_UN);
@fclose($fh);
return true;
}
}
?>