Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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