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:
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, soputenv("LC_ALL", "fr_FR");
might work better thansetlocale()
. 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.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.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:
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:
- you use NGINX server(it doesn't use threads.)
- You use Apache (with either threaded MPM or not) and PHP in fastcgi mode
- 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
Fix iOS Picture Orientation After Upload PHP
How to Loop Through JSON Array
PHP Array Printing Using a Loop
How to Redirect a 404 Error in a Custom 404 Page Using Codeigniter
How to Send Email from Local Wamp Server Using PHP
Using Curl in PHP with Client Certificate and Private Key in Separate Files
Remove Null, False, and '' - But Not 0 - from a PHP Array
Codeigniter Echoing [::1] Instead of Localhost
Avoiding MySQL Injections with the Zend_Db Class
Fetching One Row Only with MySQLi
Copy Large Files (Over 2 Gb) in PHP
How to Convert Multiple <Br/> Tag to a Single <Br/> Tag in PHP
How to Change the Array Key to Start from 1 Instead of 0
Sending Data Using Post in Python to PHP
Return One Value from Database with MySQL PHP Pdo
Mp4 Plays When Accessed Directly, But Not When Read Through PHP, on iOS