Skip to content

Commit a33ebbb

Browse files
committed
Copy latest php-gettext code, replacing ancient copies we had before.
1 parent e789d49 commit a33ebbb

File tree

4 files changed

+543
-107
lines changed

4 files changed

+543
-107
lines changed

externals/gettext.inc

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ LC_MESSAGES 5
3232
LC_ALL 6
3333
*/
3434

35-
3635
// LC_MESSAGES is not available if php-gettext is not loaded
3736
// while the other constants are already available from session extension.
3837
if (!defined('LC_MESSAGES')) {
@@ -168,21 +167,20 @@ function _check_locale_and_function($function=false) {
168167
function _get_codeset($domain=null) {
169168
global $text_domains, $default_domain, $LC_CATEGORIES;
170169
if (!isset($domain)) $domain = $default_domain;
171-
return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
170+
return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('default_charset');
172171
}
173172

174173
/**
175174
* Convert the given string to the encoding set by bind_textdomain_codeset.
176175
*/
177176
function _encode($text) {
177+
$target_encoding = _get_codeset();
178+
if (function_exists("mb_detect_encoding")) {
178179
$source_encoding = mb_detect_encoding($text);
179-
$target_encoding = _get_codeset();
180-
if ($source_encoding != $target_encoding) {
181-
return mb_convert_encoding($text, $target_encoding, $source_encoding);
182-
}
183-
else {
184-
return $text;
185-
}
180+
if ($source_encoding != $target_encoding)
181+
$text = mb_convert_encoding($text, $target_encoding, $source_encoding);
182+
}
183+
return $text;
186184
}
187185

188186

@@ -229,7 +227,9 @@ function _setlocale($category, $locale) {
229227
}
230228
// Allow locale to be changed on the go for one translation domain.
231229
global $text_domains, $default_domain;
232-
unset($text_domains[$default_domain]->l10n);
230+
if (array_key_exists($default_domain, $text_domains)) {
231+
unset($text_domains[$default_domain]->l10n);
232+
}
233233
return $CURRENTLOCALE;
234234
}
235235
}
@@ -288,9 +288,9 @@ function __($msgid) {
288288
/**
289289
* Plural version of gettext.
290290
*/
291-
function _ngettext($single, $plural, $number) {
291+
function _ngettext($singular, $plural, $number) {
292292
$l10n = _get_reader();
293-
return _encode($l10n->ngettext($single, $plural, $number));
293+
return _encode($l10n->ngettext($singular, $plural, $number));
294294
}
295295

296296
/**
@@ -304,9 +304,9 @@ function _dgettext($domain, $msgid) {
304304
/**
305305
* Plural version of dgettext.
306306
*/
307-
function _dngettext($domain, $single, $plural, $number) {
307+
function _dngettext($domain, $singular, $plural, $number) {
308308
$l10n = _get_reader($domain);
309-
return _encode($l10n->ngettext($single, $plural, $number));
309+
return _encode($l10n->ngettext($singular, $plural, $number));
310310
}
311311

312312
/**
@@ -319,9 +319,9 @@ function _dcgettext($domain, $msgid, $category) {
319319
/**
320320
* Plural version of dcgettext.
321321
*/
322-
function _dcngettext($domain, $single, $plural, $number, $category) {
322+
function _dcngettext($domain, $singular, $plural, $number, $category) {
323323
$l10n = _get_reader($domain, $category);
324-
return _encode($l10n->ngettext($single, $plural, $number));
324+
return _encode($l10n->ngettext($singular, $plural, $number));
325325
}
326326

327327
/**
@@ -405,29 +405,29 @@ function T_($msgid) {
405405
if (_check_locale_and_function()) return _($msgid);
406406
return __($msgid);
407407
}
408-
function T_ngettext($single, $plural, $number) {
408+
function T_ngettext($singular, $plural, $number) {
409409
if (_check_locale_and_function())
410-
return ngettext($single, $plural, $number);
411-
else return _ngettext($single, $plural, $number);
410+
return ngettext($singular, $plural, $number);
411+
else return _ngettext($singular, $plural, $number);
412412
}
413413
function T_dgettext($domain, $msgid) {
414414
if (_check_locale_and_function()) return dgettext($domain, $msgid);
415415
else return _dgettext($domain, $msgid);
416416
}
417-
function T_dngettext($domain, $single, $plural, $number) {
417+
function T_dngettext($domain, $singular, $plural, $number) {
418418
if (_check_locale_and_function())
419-
return dngettext($domain, $single, $plural, $number);
420-
else return _dngettext($domain, $single, $plural, $number);
419+
return dngettext($domain, $singular, $plural, $number);
420+
else return _dngettext($domain, $singular, $plural, $number);
421421
}
422422
function T_dcgettext($domain, $msgid, $category) {
423423
if (_check_locale_and_function())
424424
return dcgettext($domain, $msgid, $category);
425425
else return _dcgettext($domain, $msgid, $category);
426426
}
427-
function T_dcngettext($domain, $single, $plural, $number, $category) {
427+
function T_dcngettext($domain, $singular, $plural, $number, $category) {
428428
if (_check_locale_and_function())
429-
return dcngettext($domain, $single, $plural, $number, $category);
430-
else return _dcngettext($domain, $single, $plural, $number, $category);
429+
return dcngettext($domain, $singular, $plural, $number, $category);
430+
else return _dcngettext($domain, $singular, $plural, $number, $category);
431431
}
432432

433433
function T_pgettext($context, $msgid) {
@@ -451,26 +451,27 @@ function T_dcpgettext($domain, $context, $msgid, $category) {
451451
return _dcpgettext($domain, $context, $msgid, $category);
452452
}
453453

454-
function T_npgettext($context, $singular, $plural) {
454+
function T_npgettext($context, $singular, $plural, $number) {
455455
if (_check_locale_and_function('npgettext'))
456-
return npgettext($context, $single, $plural, $number);
456+
return npgettext($context, $singular, $plural, $number);
457457
else
458-
return _npgettext($context, $single, $plural, $number);
458+
return _npgettext($context, $singular, $plural, $number);
459459
}
460460

461-
function T_dnpgettext($domain, $context, $singular, $plural) {
461+
function T_dnpgettext($domain, $context, $singular, $plural, $number) {
462462
if (_check_locale_and_function('dnpgettext'))
463-
return dnpgettext($domain, $context, $single, $plural, $number);
463+
return dnpgettext($domain, $context, $singular, $plural, $number);
464464
else
465-
return _dnpgettext($domain, $context, $single, $plural, $number);
465+
return _dnpgettext($domain, $context, $singular, $plural, $number);
466466
}
467467

468-
function T_dcnpgettext($domain, $context, $singular, $plural, $category) {
468+
function T_dcnpgettext($domain, $context, $singular, $plural,
469+
$number, $category) {
469470
if (_check_locale_and_function('dcnpgettext'))
470-
return dcnpgettext($domain, $context, $single,
471+
return dcnpgettext($domain, $context, $singular,
471472
$plural, $number, $category);
472473
else
473-
return _dcnpgettext($domain, $context, $single,
474+
return _dcnpgettext($domain, $context, $singular,
474475
$plural, $number, $category);
475476
}
476477

@@ -494,39 +495,39 @@ if (!function_exists('gettext')) {
494495
function _($msgid) {
495496
return __($msgid);
496497
}
497-
function ngettext($single, $plural, $number) {
498-
return _ngettext($single, $plural, $number);
498+
function ngettext($singular, $plural, $number) {
499+
return _ngettext($singular, $plural, $number);
499500
}
500501
function dgettext($domain, $msgid) {
501502
return _dgettext($domain, $msgid);
502503
}
503-
function dngettext($domain, $single, $plural, $number) {
504-
return _dngettext($domain, $single, $plural, $number);
504+
function dngettext($domain, $singular, $plural, $number) {
505+
return _dngettext($domain, $singular, $plural, $number);
505506
}
506507
function dcgettext($domain, $msgid, $category) {
507508
return _dcgettext($domain, $msgid, $category);
508509
}
509-
function dcngettext($domain, $single, $plural, $number, $category) {
510-
return _dcngettext($domain, $single, $plural, $number, $category);
510+
function dcngettext($domain, $singular, $plural, $number, $category) {
511+
return _dcngettext($domain, $singular, $plural, $number, $category);
511512
}
512513
function pgettext($context, $msgid) {
513514
return _pgettext($context, $msgid);
514515
}
515-
function npgettext($context, $single, $plural, $number) {
516-
return _npgettext($context, $single, $plural, $number);
516+
function npgettext($context, $singular, $plural, $number) {
517+
return _npgettext($context, $singular, $plural, $number);
517518
}
518519
function dpgettext($domain, $context, $msgid) {
519520
return _dpgettext($domain, $context, $msgid);
520521
}
521-
function dnpgettext($domain, $context, $single, $plural, $number) {
522-
return _dnpgettext($domain, $context, $single, $plural, $number);
522+
function dnpgettext($domain, $context, $singular, $plural, $number) {
523+
return _dnpgettext($domain, $context, $singular, $plural, $number);
523524
}
524525
function dcpgettext($domain, $context, $msgid, $category) {
525526
return _dcpgettext($domain, $context, $msgid, $category);
526527
}
527-
function dcnpgettext($domain, $context, $single, $plural,
528+
function dcnpgettext($domain, $context, $singular, $plural,
528529
$number, $category) {
529-
return _dcnpgettext($domain, $context, $single, $plural,
530+
return _dcnpgettext($domain, $context, $singular, $plural,
530531
$number, $category);
531532
}
532533
}

externals/gettext.php

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
2222
*/
2323

24+
require('plurals.php');
25+
2426
/**
2527
* Provides a simple gettext replacement that works independently from
2628
* the system's gettext abilities.
@@ -98,7 +100,7 @@ function readintarray($count) {
98100
* @param object Reader the StreamReader object
99101
* @param boolean enable_cache Enable or disable caching of strings (default on)
100102
*/
101-
function gettext_reader($Reader, $enable_cache = true) {
103+
function __construct($Reader, $enable_cache = true) {
102104
// If there isn't a StreamReader, turn on short circuit mode.
103105
if (! $Reader || isset($Reader->error) ) {
104106
$this->short_circuit = true;
@@ -157,15 +159,11 @@ function load_tables() {
157159
$this->cache_translations = array ();
158160
/* read all strings in the cache */
159161
for ($i = 0; $i < $this->total; $i++) {
160-
$offset = $i * 2 + 2;
161-
if (!($this && $this->table_originals[$offset])) {
162-
continue;
163-
}
164-
$this->STREAM->seekto($this->table_originals[$offset]);
165-
$original = $this->STREAM->read($this->table_originals[$offset - 1]);
166-
$this->STREAM->seekto($this->table_translations[$offset]);
167-
$translation = $this->STREAM->read($this->table_translations[$offset - 1]);
168-
$this->cache_translations[$original] = $translation;
162+
$this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
163+
$original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
164+
$this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
165+
$translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
166+
$this->cache_translations[$original] = $translation;
169167
}
170168
}
171169
}
@@ -273,41 +271,6 @@ function translate($string) {
273271
}
274272
}
275273

276-
/**
277-
* Sanitize plural form expression for use in PHP eval call.
278-
*
279-
* @access private
280-
* @return string sanitized plural form expression
281-
*/
282-
function sanitize_plural_expression($expr) {
283-
// Get rid of disallowed characters.
284-
$expr = preg_replace('@[^a-zA-Z0-9_:;\(\)\?\|\&=!<>+*/\%-]@', '', $expr);
285-
286-
// Add parenthesis for tertiary '?' operator.
287-
$expr .= ';';
288-
$res = '';
289-
$p = 0;
290-
for ($i = 0; $i < strlen($expr); $i++) {
291-
$ch = $expr[$i];
292-
switch ($ch) {
293-
case '?':
294-
$res .= ' ? (';
295-
$p++;
296-
break;
297-
case ':':
298-
$res .= ') : (';
299-
break;
300-
case ';':
301-
$res .= str_repeat( ')', $p) . ';';
302-
$p = 0;
303-
break;
304-
default:
305-
$res .= $ch;
306-
}
307-
}
308-
return $res;
309-
}
310-
311274
/**
312275
* Parse full PO header and extract only plural forms line.
313276
*
@@ -334,14 +297,14 @@ function get_plural_forms() {
334297
$this->load_tables();
335298

336299
// cache header field for plural forms
337-
if (! is_string($this->pluralheader)) {
300+
if ($this->pluralheader === NULL) {
338301
if ($this->enable_cache) {
339302
$header = $this->cache_translations[""];
340303
} else {
341304
$header = $this->get_translation_string(0);
342305
}
343306
$expr = $this->extract_plural_forms_header_from_po_header($header);
344-
$this->pluralheader = $this->sanitize_plural_expression($expr);
307+
$this->pluralheader = new PluralHeader($expr);
345308
}
346309
return $this->pluralheader;
347310
}
@@ -354,16 +317,16 @@ function get_plural_forms() {
354317
* @return int array index of the right plural form
355318
*/
356319
function select_string($n) {
357-
$string = $this->get_plural_forms();
358-
$string = str_replace('nplurals',"\$total",$string);
359-
$string = str_replace("n",$n,$string);
360-
$string = str_replace('plural',"\$plural",$string);
320+
if (!is_int($n)) {
321+
throw new InvalidArgumentException(
322+
"Select_string only accepts integers: " . $n);
323+
}
324+
$plural_header = $this->get_plural_forms();
325+
$plural = $plural_header->expression->evaluate($n);
361326

362-
$total = 0;
363-
$plural = 0;
327+
if ($plural < 0) $plural = 0;
328+
if ($plural >= $plural_header->total) $plural = $plural_header->total - 1;
364329

365-
eval("$string");
366-
if ($plural >= $total) $plural = $total - 1;
367330
return $plural;
368331
}
369332

@@ -413,12 +376,23 @@ function ngettext($single, $plural, $number) {
413376

414377
function pgettext($context, $msgid) {
415378
$key = $context . chr(4) . $msgid;
416-
return $this->translate($key);
379+
$ret = $this->translate($key);
380+
if (strpos($ret, "\004") !== FALSE) {
381+
return $msgid;
382+
} else {
383+
return $ret;
384+
}
417385
}
418386

419387
function npgettext($context, $singular, $plural, $number) {
420-
$singular = $context . chr(4) . $singular;
421-
return $this->ngettext($singular, $plural, $number);
388+
$key = $context . chr(4) . $singular;
389+
$ret = $this->ngettext($key, $plural, $number);
390+
if (strpos($ret, "\004") !== FALSE) {
391+
return $singular;
392+
} else {
393+
return $ret;
394+
}
395+
422396
}
423397
}
424398

0 commit comments

Comments
 (0)