PHP4: undefined index: messages

Bug #554199 reported by ryanhowdy
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
php-gettext
Fix Released
High
Данило Шеган

Bug Description

version 1.0.9 of php-gettext

php version 4.4.9 w/ gettext support enabled

I've had several users report an issue with an undefined index 'messages' on line 190 in the gettext.inc file

line 188: // Allow locale to be changed on the go for one translation domain.
line 189: global $text_domains, $default_domain;
line 190: unset($text_domains[$default_domain]->l10n);
line 191: return $CURRENTLOCALE;

Revision history for this message
Данило Шеган (danilo) wrote :

It seems no textdomain call was executed (which sets $default_domain). I've added a nicer error message here.

FWIW, the reason why you are probably getting this is because you are not using all emulated calls: all php-gettext calls will fallback on compiled in gettext support whenever possible (i.e. you might still need emulation when locales are not existant on the system you are running on), but php-gettext needs to be fully initialized.

Can you please check if you are just setting the default domain with textdomain() (built-in gettext) call, or are you using one that includes the emulation layer?

Changed in php-gettext:
status: New → Incomplete
Revision history for this message
ryanhowdy (haudenschilt) wrote :

I'm assuming that I have this setup incorrectly. My goal is to use php-gettext and never use the built in gettext, but now I'm thinking that's not exactly what I'm doing. Here is the code.

// Setup php-gettext
if (isset($_SESSION['language'])) {
    T_setlocale(LC_MESSAGES, $_SESSION['language']);
} else {
    $lang = getLanguage();
    T_setlocale(LC_MESSAGES, $lang);
}
bindtextdomain('messages', './language');
if (function_exists('bind_textdomain_codeset')) {
  bind_textdomain_codeset('messages', 'UTF-8');
}
textdomain('messages');

Revision history for this message
Данило Шеган (danilo) wrote :

You should simply use T_bindtextdomain, T_bindtextdomain_codeset and T_textdomain in those cases: they will give you exactly what you "need".

FWIW, I still suggest you use built-in gettext support (using T_* functions) simply because they are an order of magnitude (if not more) faster than PHP code (they are also heavily optimized C code, they mmap the entire MO file, etc.). Using php-gettext on top of it simply ensures that you won't have to worry about problems with system installation and will not be limited by it.

If you want to not fall-back on non-emulated gettext, simply use _* functions (without "T" up front). If gettext is not even compiled in, then php-gettext will still provide standard gettext functions through emulation, but since it's impossible to override them in PHP (at least not that I know of), when they are defined you have to use alternative names.

Changed in php-gettext:
status: Incomplete → Invalid
Revision history for this message
Данило Шеган (danilo) wrote :

Note that for emulation to work you'll also have to use T_*gettext() functions instead of simply using *gettext() functions throughout your code.

Revision history for this message
ryanhowdy (haudenschilt) wrote :

Currently I'm using _('output text') functions to display the text. Should I change this to T_('output text')?

I want to use the fallback solution as you suggested.

Revision history for this message
Данило Шеган (danilo) wrote :

Yeah, see examples/pigs_fallback.php to see how it all works.

Revision history for this message
ryanhowdy (haudenschilt) wrote :

This still doesn't seem to be working for php 4.

I've setup php-gettext like this:

    T_setlocale(LC_MESSAGES, 'en_US');
    T_bindtextdomain('messages', './language');
    T_bind_textdomain_codeset('messages', 'UTF-8');
    T_textdomain('messages');

And I'm using the following to output the text:

    T_('This is some text');

This works fine for php 5 using gettext support, but not for php 4 with gettext.

ryanhowdy (haudenschilt)
Changed in php-gettext:
status: Invalid → New
Revision history for this message
Данило Шеган (danilo) wrote :

I'll have to find a PHP4 installation to be able to debug this. FWIW, it should all just work like that.

Revision history for this message
Данило Шеган (danilo) wrote :

Btw, can you confirm that pigs_fallback.php example behaves the same way: i.e. works on PHP5, doesn't on PHP4.

Revision history for this message
ryanhowdy (haudenschilt) wrote :

Yes, this error occurs for the pigs_fallback.php and the pigs_dropin.php examples, for php4 only.

Notice: Undefined index: messages in /examples/gettext.inc on line 232

Changed in php-gettext:
status: New → Triaged
importance: Undecided → High
summary: - undefined index: messages
+ PHP4: undefined index: messages
Revision history for this message
Данило Шеган (danilo) wrote :

Last release (1.0.11) makes the "unset" conditional on the array key existing (it was fixed long time ago in the lp:php-gettext branch, but I've only now released it).

Changed in php-gettext:
status: Triaged → Fix Released
assignee: nobody → Данило Шеган (danilo)
milestone: none → 1.0.11
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.