3 * TGettext_PO class file.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the BSD License.
8 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
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/}
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
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 // +----------------------------------------------------------------------+
31 // $Id: PO.php 3187 2012-07-12 11:21:01Z ctrlaltca $
36 * @author Michael Wallner <mike@php.net>
37 * @license PHP License
40 require_once dirname(__FILE__).'/TGettext.php';
45 * GNU PO file reader and writer.
47 * @author Michael Wallner <mike@php.net>
48 * @version $Revision: 1.2 $
50 * @package System.I18N.core
52 class TGettext_PO extends TGettext
58 * @return object File_Gettext_PO
59 * @param string path to GNU PO file
61 function __construct($file = '')
70 * @return mixed Returns true on success or PEAR_Error on failure.
73 function load($file = null)
80 if (!$contents = @file($file)) {
83 $contents = implode('', $contents);
85 // match all msgid/msgstr entries
86 $matched = preg_match_all(
87 '/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+' .
88 '(msgstr\s+("([^"]|\\\\")*?"\s*)+)/',
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);
106 // check for meta info
107 if (isset($this->strings[''])) {
108 $this->meta = parent::meta2array($this->strings['']);
109 unset($this->strings['']);
119 * @return mixed Returns true on success or PEAR_Error on failure.
120 * @param string $file
122 function save($file = null)
129 if (!is_resource($fh = @fopen($file, 'w'))) {
133 // lock PO file exclusively
134 if (!flock($fh, LOCK_EX)) {
139 if (count($this->meta)) {
140 $meta = 'msgid ""' . "\nmsgstr " . '""' . "\n";
141 foreach ($this->meta as $k => $v) {
142 $meta .= '"' . $k . ': ' . $v . '\n"' . "\n";
144 fwrite($fh, $meta . "\n");
147 foreach ($this->strings as $o => $t) {
149 'msgid "' . parent::prepare($o, true) . '"' . "\n" .
150 'msgstr "' . parent::prepare($t, true) . '"' . "\n\n"
155 @flock($fh, LOCK_UN);
157 chmod($file,PRADO_CHMOD);