Wrong domain encoding set by _get_codeset() if using direct calls to domain based methods

Bug #1635736 reported by Pier Luigi
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
php-gettext
New
Undecided
Unassigned

Bug Description

Problem:
after initial setup, a direct call to _dgettext (or to the others domain based methods) results in data being encoded with default ini_get('mbstring.internal_encoding') instead of that set for the domain with the bind_textdomain_codeset

Ex:
$domain = 'MyGettextPoMoFileName';
_bindtextdomain($domain,"i18n"); // "i18n" is the gettext data directory name
_bind_textdomain_codeset($domain,"UTF-8");
_setlocale(LC_MESSAGES, "it_IT));

echo _dgettext($domain,"message-accented-letters-àòèé");

result in: message-accented-letters-����

The problem is that the the parameter $domain is never passed to _get_codeset() function from the call inside the _encode() function called from _dgettext() (and all others d functions).
Actual code extract (v1.0.12):

function _get_codeset($domain=null) {
    global $text_domains, $default_domain, $LC_CATEGORIES;
    if (!isset($domain)) $domain = $default_domain;
    return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
}

function _encode($text) {
  $target_encoding = _get_codeset();
  if (function_exists("mb_detect_encoding")) {
    $source_encoding = mb_detect_encoding($text);
    if ($source_encoding != $target_encoding)
      $text = mb_convert_encoding($text, $target_encoding, $source_encoding);
  }
  return $text;
}

function _dngettext($domain, $singular, $plural, $number) {
    $l10n = _get_reader($domain);
    return _encode($l10n->ngettext($singular, $plural, $number));
}

Fix:

function _get_codeset($domain=null) {
    global $text_domains, $default_domain, $LC_CATEGORIES;
    if (!isset($domain)) $domain = $default_domain;
    return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
}

function _encode($text,$domain=null) {
  $target_encoding = _get_codeset($domain);
  if (function_exists("mb_detect_encoding")) {
    $source_encoding = mb_detect_encoding($text);
    if ($source_encoding != $target_encoding)
      $text = mb_convert_encoding($text, $target_encoding, $source_encoding);
  }
  return $text;
}

function _dgettext($domain, $msgid) {
    $l10n = _get_reader($domain);
    return _encode($l10n->translate($msgid),$domain);
}

function _dngettext($domain, $singular, $plural, $number) {
    $l10n = _get_reader($domain);
    return _encode($l10n->ngettext($singular, $plural, $number),$domain);
}

and so on for _dcgettext, _dcngettext, _dcpgettext, _dnpgettext, _dcnpgettext.

Pier Luigi (picov)
description: updated
description: updated
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.