Php/Gettext Problems

PHP/Gettext Problems

Gettext isn't overly practical for webapps.

  • As for example it doesn't honor/use Accept-Language style preferences by itself.
  • Typically incurs some caching issues on shared webhosts (mod_php SAPI).

So I sort of sometimes wish that PHP module wouldn't exist, and the convenient _() function name shortcut was available to userland implementations.

(Had my own gettext.php, which worked more reliable.)

Your options:

  1. Anway, according to a few bug reports the Windows port of gettext had some flaws with UTF-8. Maybe your version is affected again. So try bind_textdomain_codeset('default', 'ISO-8859-1'); for starters. Also, it seems to prefer the environment variables on Windows IIRC, so putenv("LC_ALL", "fr_FR"); might work better than setlocale(). Especially workable if you dl(gettext.dll) later on.

    Also give it a chance with including a charset right there LANG=en_GB.ISO-8859-1. (Since your source text is English anyway, caring about the charset isn't very relavant here; but probably a common case where gettext trips over itself.) Oh and on occasion it's UTF8 not UTF-8; also try ASCII.

  2. Alternatively circumvent gettext. Your domain idea is close, but I'd simply use a pre-defined ./locale/ subdir for languages:

    ./lang/en/locale/C/LC_MESSAGES/domain.mo

    Then just invoke bindtextdomain("default", "./lang/{$APP_LANG}/locale") without giving gettext room to interpret much. It will always look up /C/, but the correct locale directory has been injected already. But try to have a symlink from the $LANG to /C/ in there anyway.

  3. Bite in the gnu. Give up on gettext. "PhpWiki" had a custom awk conversion script. It transforms .po files into .php array scripts (yeah very oldschool), and just utilizes a __() function instead. Close. And more reliable.

Gettext not working, what's wrong?

I've been having headaches with this one too. I added this and it worked:

putenv('LANGUAGE=en_US');

Next to that I use the following:

$domain = 'woohoo';
setlocale(LC_ALL, 'en_US.utf8');
putenv('LANGUAGE=en_US');

if( ENV != 'live' ){
// reset caching nocache is a simlink to "."
bindtextdomain($domain, dirname(__FILE__) . '/../locale/nocache');
}

bindtextdomain($domain, dirname(__FILE__) . '/../locale');
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);

Also, in my setup it only started working when I added the iso code to the .po file:

msgid ""
msgstr ""
"Language: en_US\n"

Issues with gettext php

You just had to comment out one line in the php.ini file. Like this -

;extension=php_gettext.dll

PHP gettext translations not getting picked up

Alright! I figured it out by looking at PHP.net example! Domain must be the same as the file name! If domain is set to 'localhost' file must be named 'localhost.mo'!

Edit I did however observe strange behaviour.
For whatever reason PHP keeps resetting the translation every 7th refresh and the original text pops up. Observing the network tab in chrome the initial page load is above 400 B, second is 399 until the 7th is 395:

Sample Image

Does PHP reset some cache every 7th reload?

PHP Gettext problems (like non-thread-safe?)

Threads problem only apply if one uses embedded PHP (Apache's mod-php for example) and runs server that uses threads (like Apache server with worker-mpm).

So - thread safety issue does not apply to you if:

  1. you use NGINX server(it doesn't use threads.)
  2. You use Apache (with either threaded MPM or not) and PHP in fastcgi mode
  3. You use Apache with non-threaded MPM (as prefork-MPM) and PHP in mod-php mode.

So - most people with default Apache install shouldn't worry about gettext not being thread safe, as default apache's install in most distro's uses non-threaded prefork-MPM!

P.S. also - keep in mind that Apache on Windows is threaded.



Related Topics



Leave a reply



Submit