~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to lib/tools/po/extract.php

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 * way that Gallery embeds internationalized text, so let's tack on our
24
24
 * own copyrights.
25
25
 *
26
 
 * Copyright 2002-2005 Bharat Mediratta <bharat@menalto.com>
 
26
 * Copyright 2002-2006 Bharat Mediratta <bharat@menalto.com>
27
27
 *
28
 
 * $Id: extract.php,v 1.15 2005/09/09 06:29:59 bharat Exp $
 
28
 * $Id: extract.php,v 1.21 2006/02/27 21:44:21 mindless Exp $
29
29
 */
30
30
 
31
31
if (!empty($_SERVER['SERVER_NAME'])) {
34
34
}
35
35
 
36
36
$exts = '(class|php|inc|tpl|css|html)';
 
37
/* These are in phpdoc and don't really need translations: */
 
38
$skip = array('TEST to be displayed in different languages' => true,
 
39
              'TT <!-- abbreviation for Translation Test -->' => true);
37
40
$idEmitted = false;
 
41
$strings = array();
38
42
foreach ($_SERVER['argv'] as $moduleDir) {
39
 
    find($moduleDir);
 
43
    if (!is_dir($moduleDir)) {
 
44
        continue;
 
45
    }
 
46
    chdir($moduleDir);
 
47
    find('.');
40
48
 
41
49
    $oldStringsRaw = "$moduleDir/po/strings.raw";
42
50
    if (file_exists($oldStringsRaw)) {
51
59
if (!$idEmitted) {
52
60
    print '# $' . 'Id$' . "\n";
53
61
}
54
 
print "\n";
55
 
$strings = array_keys($strings);
56
 
print implode("\n", $strings);
57
 
print "\n";
 
62
foreach ($strings as $string => $otherFiles) {
 
63
    print $string;
 
64
    if (!empty($otherFiles)) {
 
65
        print ' /* also in: ' . implode(' ', $otherFiles) . ' */';
 
66
    }
 
67
    print "\n";
 
68
}
58
69
 
59
70
/**
60
71
 * Recursive go through subdirectories
61
72
 */
62
73
function find($dir) {
63
 
    if (is_dir($dir) && $dh = opendir($dir)) {
64
 
        $listing = array();
 
74
    if ($dh = opendir($dir)) {
 
75
        $listing = $subdirs = array();
65
76
        while (($file = readdir($dh)) !== false) {
66
77
            if ($file == '.' || $file == '..') {
67
78
                continue;
71
82
        closedir($dh);
72
83
        sort($listing);
73
84
        global $exts;
 
85
        $dir = ($dir == '.') ? '' : ($dir . '/');
74
86
        foreach ($listing as $file) {
75
 
            $filename = $dir . '/' . $file;
76
 
            $type = filetype($filename);
77
 
            if ($type == 'dir') {
78
 
                find($filename);
79
 
            }
80
 
            if (preg_match("/\." . $exts . "$/", $file)) {
 
87
            $filename = $dir . $file;
 
88
            if (is_dir($filename)) {
 
89
                $subdirs[] = $filename;
 
90
            } else if (preg_match("/\." . $exts . "$/", $file)) {
81
91
                extractStrings($filename);
82
92
            }
83
93
        }
 
94
        foreach ($subdirs as $dir) {
 
95
            find($dir);
 
96
        }
84
97
    }
85
98
}
86
99
 
88
101
 * Grab all translatable strings in a file into $strings array
89
102
 */
90
103
function extractStrings($filename) {
91
 
    global $strings;
 
104
    global $strings, $skip;
 
105
    $strings["\n/* $filename */"] = array();
 
106
    $startSize = count($strings);
 
107
    $localStrings = array();
92
108
    if (function_exists('file_get_contents')) {
93
109
        $data = file_get_contents($filename);
94
110
    } else {
97
113
        $data = $fileSize == 0 ? '' : fread($fd, $fileSize);
98
114
        fclose($fd);
99
115
    }
100
 
    #echo "$data\n";
101
116
 
102
 
    # grab phrases for translate( or i18n( or _( calls; capture string parameter enclosed
103
 
    # in single or double quotes including concatenated strings like 'one' . "two"
 
117
    /*
 
118
     * grab phrases for translate( or i18n( or _( calls; capture string parameter enclosed
 
119
     * in single or double quotes including concatenated strings like 'one' . "two"
 
120
     */
104
121
    if (preg_match_all("/(translate|i18n|_)\(\s*(((\s*\.\s*)?('((\\')?[^']*?)*[^\\\]'|\"((\")?[^\"]*?)*[^\\\]\"))+)\s*\)/s",
105
122
                       $data, $matches, PREG_SET_ORDER)) {
106
123
        foreach ($matches as $match) {
107
124
            $text = $match[2];
108
125
            $cmd = sprintf('return %s;', $text);
109
126
            $text = eval($cmd);
110
 
            $text = str_replace('"', '\\"', $text);    # escape double-quotes
 
127
            $text = str_replace('"', '\\"', $text);    /* escape double-quotes */
 
128
            if (isset($skip[$text])) {
 
129
                continue;
 
130
            }
111
131
            $string = sprintf('gettext("%s")', $text);
112
 
            $strings[$string] = TRUE;
 
132
            if (!isset($strings[$string])) {
 
133
                $strings[$string] = array();
 
134
            } else if (!isset($localStrings[$string])) {
 
135
                $strings[$string][] = $filename;
 
136
            }
 
137
            $localStrings[$string] = true;
113
138
        }
114
139
    }
115
140
 
116
 
    # grab phrases of this format: translate(array('one' => '...', 'many' => '...'))
117
 
    if (preg_match_all("/translate\(.*?array\('one'\s*=>\s*'(.*?)'.*?'many'\s*=>\s*'(.*?)'.*?\).*?\)/s",
 
141
    /* grab phrases of this format: translate(array('one' => '...', 'many' => '...')) */
 
142
    if (preg_match_all("/translate\(.*?array\(\s*'one'\s*=>\s*'(.*?)'.*?'many'\s*=>\s*'(.*?)'.*?\).*?\)/s",
118
143
                       $data, $matches, PREG_SET_ORDER)) {
119
144
        foreach ($matches as $match) {
120
145
            $one = $match[1];
121
146
            $many = $match[2];
122
 
            $one = str_replace('"', '\\"', $one);      # escape double-quotes
123
 
            $many = str_replace('"', '\\"', $many);    # escape double-quotes
 
147
            $one = str_replace('"', '\\"', $one);      /* escape double-quotes */
 
148
            $many = str_replace('"', '\\"', $many);    /* escape double-quotes */
124
149
            $string = sprintf('ngettext("%s", "%s")', $one, $many);
125
 
            $strings[$string] = TRUE;
 
150
            if (!isset($strings[$string])) {
 
151
                $strings[$string] = array();
 
152
            } else if (!isset($localStrings[$string])) {
 
153
                $strings[$string][] = $filename;
 
154
            }
 
155
            $localStrings[$string] = true;
126
156
        }
127
157
    }
128
158
 
129
 
    # grab phrases of this format: translate(array('text' => '...', ...))
130
 
    if (preg_match_all("/translate\(\s*array\('text'\s*=>\s+'(.*?[^\\\])'/s",
 
159
    /* grab phrases of this format: translate(array('text' => '...', ...)) */
 
160
    if (preg_match_all("/translate\(\s*array\(\s*'text'\s*=>\s+'(.*?[^\\\])'/s",
131
161
                       $data, $matches, PREG_SET_ORDER)) {
132
162
        foreach ($matches as $match) {
133
163
            $text = $match[1];
134
 
            $text = str_replace('"', '\\"', $text);    # escape double-quotes
 
164
            $text = str_replace('"', '\\"', $text);    /* escape double-quotes */
135
165
            $string = sprintf('gettext("%s")', $text);
136
 
            $strings[$string] = TRUE;
 
166
            if (!isset($strings[$string])) {
 
167
                $strings[$string] = array();
 
168
            } else if (!isset($localStrings[$string])) {
 
169
                $strings[$string][] = $filename;
 
170
            }
 
171
            $localStrings[$string] = true;
137
172
        }
138
173
    }
139
174
 
140
 
    # grab phrases of this format: {g->text ..... }
141
 
    if (preg_match_all("/(\{\s*g->text\s+.*?[^\\\]\})/s",
 
175
    /* grab phrases of this format: {g->text ..... } or {g->changeInDescendents ... } */
 
176
    if (preg_match_all("/(\{\s*g->(?:text|changeInDescendents)\s+.*?[^\\\]\})/s",
142
177
                       $data, $matches, PREG_SET_ORDER)) {
143
178
        foreach ($matches as $match) {
144
179
            $string = $match[1];
145
 
            $text = null;
146
 
            $one = null;
147
 
            $many = null;
 
180
            $text = $one = $many = null;
148
181
 
149
 
            # Ignore translations of the form:
150
 
            #   text=$foo
151
 
            # as we expect those to be variables containing values that
152
 
            # have been marked elsewhere with the i18n() function
 
182
            /*
 
183
             * Ignore translations of the form:
 
184
             *   text=$foo
 
185
             * as we expect those to be variables containing values that
 
186
             * have been marked elsewhere with the i18n() function
 
187
             */
153
188
            if (preg_match("/text=\\$/", $string)) {
154
189
                continue;
155
190
            }
156
191
 
157
 
            # text=.....
 
192
            /* text=..... */
158
193
            if (preg_match("/text=\"(.*?[^\\\])\"/s", $string, $matches)) {
159
194
                $text = $matches[1];
160
195
            } elseif (preg_match("/text='(.*?)'/s", $string, $matches)) {
161
196
                $text = $matches[1];
162
 
                $text = str_replace('"', '\\"', $text);    # escape double-quotes
 
197
                $text = str_replace('"', '\\"', $text);    /* escape double-quotes */
163
198
            }
164
199
 
165
 
            # one = .....
 
200
            /* one = ..... */
166
201
            if (preg_match("/\s+one=\"(.*?[^\\\])\"/s", $string, $matches)) {
167
202
                $one = $matches[1];
168
203
            } elseif (preg_match("/\s+one='(.*?)'/s", $string, $matches)) {
169
204
                $one = $matches[1];
170
 
                $one = str_replace('"', '\\"', $one);    # escape double-quotes
 
205
                $one = str_replace('"', '\\"', $one);    /* escape double-quotes */
171
206
            }
172
207
 
173
 
            # many = .....
 
208
            /* many = ..... */
174
209
            if (preg_match("/\s+many=\"(.*?[^\\\])\"/s", $string, $matches)) {
175
210
                $many = $matches[1];
176
211
            } elseif (preg_match("/\s+many='(.*?)'/s", $string, $matches)) {
177
212
                $many = $matches[1];
178
 
                $many = str_replace('"', '\\"', $many);    # escape double-quotes
179
 
            }
180
 
 
181
 
            # pick gettext() or ngettext()
 
213
                $many = str_replace('"', '\\"', $many);    /* escape double-quotes */
 
214
            }
 
215
 
 
216
            /* c-format hint for xgettext */
 
217
            if (preg_match('/c[Ff]ormat=(true|false)/s', $string, $matches)) {
 
218
                $hint = '/* xgettext:' . ($matches[1] == 'false' ? 'no-' : '') . "c-format */\n";
 
219
            } else {
 
220
                $hint = '';
 
221
            }
 
222
 
 
223
            /* pick gettext() or ngettext() */
182
224
            if ($text != null) {
183
 
                $string = sprintf('gettext("%s")', $text);
184
 
            } elseif ($one != null && $many != null) {
185
 
                $string = sprintf('ngettext("%s", "%s")', $one, $many);
 
225
                $string = $hint . sprintf('gettext("%s")', $text);
 
226
            } else if ($one != null && $many != null) {
 
227
                $string = $hint . sprintf('ngettext("%s", "%s")', $one, $many);
186
228
            } else {
187
 
                # parse error
 
229
                /* parse error */
188
230
                $stderr = fopen('php://stderr', 'w');
189
231
                $text = preg_replace("/\n/s", '\n>', $text);
190
 
                fwrite($stderr, "extract.pl parse error: $file:\n");
 
232
                fwrite($stderr, "extract.php parse error: $filename:\n");
191
233
                fwrite($stderr, "> $text\n");
192
234
                exit(1);
193
235
            }
194
236
 
195
 
            $string = str_replace('\\}', '}', $string);    # unescape right-curly-braces
196
 
            $strings[$string] = TRUE;
 
237
            $string = str_replace('\\}', '}', $string);    /* unescape right-curly-braces */
 
238
            if (!isset($strings[$string])) {
 
239
                $strings[$string] = array();
 
240
            } else if (!isset($localStrings[$string])) {
 
241
                $strings[$string][] = $filename;
 
242
            }
 
243
            $localStrings[$string] = true;
197
244
        }
198
245
    }
 
246
    if (count($strings) == $startSize) {
 
247
        unset($strings["\n/* $filename */"]);
 
248
    }
199
249
}
200
250
?>