-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgettext.php
391 lines (335 loc) · 12.2 KB
/
gettext.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/*
api: PHP
type: functions
title: gettext()
description: emulates PHP gettext extension functionality
priority: auto
category: library
This include script simulates gettext() functionality.
- It could read translation data from .mo and .po files.
- Lookup of plural forms mostly work (but not 100% compliant,
no real interpreter for Plural-Forms: expression).
- Categories/codesets are ignored.
Besides using setlocale() you should change the $_ENV["LANG"] var
to the desired language manually. Additionally all your scripts
could contain following (may also work with standard gettext):
$_ENV["LANGUAGE"] = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
That's often more user-friendly than hardwired server-side values.
*/
#-- emulate only if not present in current PHP interpreter
if (!function_exists("gettext")) {
#-- all-in-one combined implementation
# (in original API only the first parameter is present)
function gettext($msg, $msg2=NULL, $domain=NULL, $category=NULL, $plural=NULL) {
global $_GETTEXT;
#-- get default params if corresponding args are empty
if (!isset($domain)) {
$domain = $_GETTEXT['%domain'];
}
if (empty($_GETTEXT[$domain])) {
bindtextdomain($domain); // auto load from system dirs
}
#-- plural array position
if (!isset($plural)) {
$pli = 0;
}
elseif ($ph = $_GETTEXT[$domain]['%plural-c']) {
$pli = gettext___plural_guess($ph, $plural);
}
else {
$pli = ($plural != 1) ? 1 : 0; // English
}
#-- look up string
if (isset($_GETTEXT[$domain][$msg]) && ($trans = $_GETTEXT[$domain][$msg])
or ($pli) and ($trans = $_GETTEXT[$domain][$msg2]))
{
// handle plural entries
if (is_array($trans)) {
if (!isset($trans[$pli])) {
$pli = 0; // missing translation
}
$trans = $trans[$pli];
}
// only return, if something found
if (strlen($trans)) {
$msg = $trans;
}
}
#-- handle $category (???)
// recode() ...
#-- give out whatever we have
return($msg);
}
#-- return plural form array index for algorithm type
# (compacted from C expression string beforehand)
function gettext___plural_guess(&$type, $n) {
#-- guess from string with C expression and set integer shorthand
if (is_string($type)) {
if (($type == "nplurals=1;plural=0;") || !strlen($type)) {
$type = -1; // no plurals
}
elseif ($type == "nplurals=2;plural=n!=1;") {
$type = 1; // English
}
elseif ($type == "nplurals=2;plural=n>1;") {
$type = 2; // French
}
// special cases
elseif (strpos($type, "n%100!=11")) {
if (strpos($type, "n!=0")) {
$type == 21; // Latvian
}
if (strpos($type, "n%10<=4")) {
$type = 22; // a few Slavic langs (code similar to Polish below)
}
if (strpos($type, "n%10>=2")) { // Lithuanian
$type = 23;
}
$type = 0;
}
// specials, group 2
elseif (strpos($type, "n<=4")) { // Slovak
$type = 25;
}
elseif (strpos($type, "n==2")) { // Irish
$type = 31;
}
elseif (strpos($type, "n%10>=2")) { // Polish
$type = 26;
}
elseif (strpos($type, "n%100==3")) { // Slovenian
$type = 28;
}
// fallbacks
elseif (strpos($type, ";plural=n;")) {
$type = 7; // unused
}
// first at this point a tokenizer/parser/interpreter would have made sense
else {
$type = 0; // no plurals
}
}
#-- return plural index value from pre-set formulas
switch ($type) {
case -1: // no plural forms
return(0);
case 1: // English, and lots of others...
return($n != 1 ? 1 : 0);
case 2: // French, Brazilian Protuguese
return($n > 1 ? 1 : 0);
case 7: // unused
return($n);
case 21: // Latvian
return (($n%10==1) && ($n%100!=11)) ? (0) : ($n!=0 ? 1 : 2) ;
case 22: // Slavic langs
return ($n%10==1) && ($n%100!=11) ? 0 :
( ($n%10>=2) && ($n%10<=4) && ($n%100<10 || $n%100>=20) ? 1 : 2 ) ;
case 23: // Lithuanian
return ($n%10==1) && ($n%100!=11) ? 0 :
( ($n%10>=2) && ($n%100<10 || $n%100>=20) ? 1 : 2 ) ;
case 25: // Slovak
return $n==1 ? 0 : ($n>=2 && $n<=4 ? 1 : 2) ;
case 26: // Polish
return $n==1 ? 0 : ( $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2 ) ;
case 28: // Slovenian
return $n%100==1 ? 0 : ($n%100==2 || $n%100==3 || $n%100==4 ? 2 : 3) ;
case 31: // Irish
return ($n == 1) ? (0) : (($n == 2) ? 1 : 2) ;
default:
$type = -1;
} // unsupported, always return non-plural index [0]
return(0);
}
#-- wrappers around monster function above
function ngettext($msg1, $msg2, $plural) {
return gettext($msg1, $msg2, NULL, NULL, $plural);
}
function dngettext($domain, $msg1, $msg2, $plural) {
return gettext($msg1, $msg2, $domain, NULL, $plural);
}
function dcngettext($domain, $msg1, $msg2, $plural, $category) {
return gettext($msg1, $msg2, $domain, $category, $plural);
}
function dcgettext($domain, $msg, $category) {
return gettext($msg, NULL, $domain, $category);
}
function dgettext($domain, $msg) {
return gettext($msg, NULL, $domain);
}
#-- sets current translation data source
# (must have been loaded beforehand)
function textdomain($default="NULL") {
global $_GETTEXT;
$prev = isset($_GETTEXT['%domain']) ? $_GETTEXT['%domain'] : NULL;
if (isset($default)) {
$_GETTEXT['%domain'] = $default;
}
return $prev;
}
#-- loads data files
function bindtextdomain($domain, $directory="/usr/share/locale:/usr/local/share/locale:./locale") {
global $_GETTEXT;
if (isset($_GETTEXT[$domain]) && (count($_GETTEXT[$domain]) > 3)) {
return; // don't load twice
}
$_GETTEXT[$domain]['%dir'] = $directory;
$_GETTEXT['%locale'] = setlocale(LC_CTYPE, 0);
#-- allowed languages
$langs = @$_ENV['LANGUAGE'] . ',' . @$_ENV['LC_ALL'] . ','
. @$_ENV['LC_MESSAGE'] .',' . @$_ENV['LANG'] . ','
. @$_GETTEXT['%locale'] . ',' . @$_SERVER['HTTP_ACCEPT_LANGUAGE']
. ',C,en';
#-- add shortened language codes (en_UK.UTF-8 -> + en_UK, en)
foreach (explode(',', $langs) as $d) {
$d = trim($d);
// $dir2[] = $d;
$d = strtok($d, "@.-+=%:; ");
if (strlen($d)) {
$dir2[] = $d;
}
if (strpos($d, '_')) {
$dir2[] = strtok($d, '_');
}
}
#-- search for matching directory and load data file
foreach (explode(':', $directory) as $directory) {
foreach ($dir2 as $lang) {
$base_fn = "$directory/$lang/LC_MESSAGES/$domain";
#echo "GETTEXT:$lang:$base_fn\n";
#-- binary format
if (file_exists($fn = "$base_fn.mo") && ($f = fopen($fn, "rb")))
{
gettext___load_mo($f, $domain);
break 2;
}
#-- text file
elseif (function_exists("gettext___load_po")
and file_exists($fn = "$base_fn.po") && ($f = fopen($fn, "r")))
{
gettext___load_po($f, $domain);
break 2;
}
}
}//foreach
#-- extract headers
if ($head = $_GETTEXT[$domain][""]) {
foreach (explode("\n", $head) as $line) {
$header = strtok(':', $line);
$line = trim(strtok("\n"));
$_GETTEXT[$domain]['%po-header'][strtolower($header)] = $line;
}
#-- plural-forms header
if (function_exists("gettext___plural_guess")
and ($h = @$_GETTEXT[$domain]['%po-header']["plural-forms"]))
{
$h = preg_replace("/[(){}\[\]^\s*\\]+/", "", $h); // rm whitespace
gettext___plural_guess($h, 0); // pre-decode into algorithm type integer
$_GETTEXT[$domain]['%plural-c'] = $h;
}
}
#-- set as default textdomain
if (empty($_GETTEXT['%domain'])) {
textdomain($domain);
}
return($domain);
}
#-- load string data from binary .mo files (ign checksums)
function gettext___load_mo($f, $domain) {
global $_GETTEXT;
#-- read in data file completely
$data = fread($f, 1<<20);
fclose($f);
#-- extract header fields and check file magic
if ($data) {
$header = substr($data, 0, 20);
$header = unpack("L1magic/L1version/L1count/L1o_msg/L1o_trn", $header);
extract($header);
if ((dechex($magic) == "950412de") && ($version == 0)) {
#-- fetch all entries
for ($n=0; $n<$count; $n++) {
#-- msgid
$r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
$msgid = substr($data, $r["offs"], $r["len"]);
unset($msgid_plural);
if (strpos($msgid, "\000")) {
list($msgid, $msgid_plural) = explode("\000", $msgid);
}
#-- translation(s)
$r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
$msgstr = substr($data, $r["offs"], $r["len"]);
if (strpos($msgstr, "\000")) {
$msgstr = explode("\000", $msgstr);
}
#-- add
$_GETTEXT[$domain][$msgid] = $msgstr;
if (isset($msgid_plural)) {
$_GETTEXT[$domain][$msgid_plural] = &$_GETTEXT[$domain][$msgid];
}
}
}
}
}
#-- read from textual .po source file (not fully correct, and redundant
# because the original gettext/libintl doesn't support this at all)
function gettext___load_po($f, $domain) {
global $_GETTEXT;
$c_esc = array("\\n"=>"\n", "\\r"=>"\r", "\\\\"=>"\\", "\\f"=>"\f", "\\t"=>"\t", "\\"=>"");
#-- read line-wise from text file
do {
$line = trim(fgets($f));
#-- check what's in the current line
$space = strpos($line, " ");
// comment
if ($line[0] == "#") {
//continue;
}
// msgid
elseif (strncmp($line, "msgid", 5)==0) {
$msgid[] = trim(substr($line, $space+1), '"');
}
// translation
elseif (strncmp($line, "msgstr", 6)==0) {
$msgstr[] = trim(substr($line, $space+1), '"');
}
// continued (could be _id or _str)
elseif ($line[0] == '"') {
$line = trim($line, '"');
if (isset($msggstr)) {
$msgstr[count($msgstr)] .= $line;
}
else {
$msgid[count($msgid)] .= $line;
}
}
#-- append to global $_GETTEXT hash as soon as we have a complete dataset
if (isset($msgid) && isset($msgstr) && (empty($line) || ($line[0]=="#") || feof($f)) )
{
$msgid[0] = strtr($msgid[0], $c_esc);
foreach ($msgstr as $v) {
$_GETTEXT[$domain][$msgid[0]] = strtr($v, $c_esc);
}
if ($msgid[1]) {
$msgid[1] = strtr($msgid[1], $c_esc);
$_GETTEXT[$domain][$msgid[1]] = &$_GETTEXT[$domain][$msgid[0]];
}
$msgid = array();
$msgstr = array();
}
} while (!feof($f));
fclose($f);
}
#-- ignored setting (no idea what it really should do)
function bind_textdomain_codeset($domain, $codeset) {
global $_GETTEXT;
$_GETTEXT[$domain]["%codeset"] = $codeset;
return($domain);
}
}
#-- define gettexts preferred function name _ separately
if (!function_exists("_")) {
function _($str) {
return gettext($str);
}
}
?>