How to Clear PHP's Gettext Cache Without Restart Apache Nor Change Domain

How to clear php's gettext cache without restart Apache nor change domain?

Every solution (1, 2, 3) suggests changing the domain to get rid of the cache problem, but this will create lots of out-of-date cache in memory.

So I dug into the gnu-gettext source for details on the cache strategy (bindtextdom.c:78.)

When bindtextdomain(domain, dirname) is called, it will check whether domain exists in the cache; if so, it will then check if dirname is the same with the one in the cache. If this fails, it will force a cache flush for the same domain, instead of creating a new one in memory.

The fix is incredibly simple, first create a dummy link to the locale folder where your .mo file is stored:

cd locale
ln -s . nocache

Then add one single line before bindtextdomain()

bindtextdomain('domain', './locale/nocache');
bindtextdomain('domain', './locale');

Now the cache is forced to flush every time.


Updates:

This hack may not works in some cases (Can't figure out the exact conditions.) This solution is NOT something you should use in production environment, but only for those who need to fix something while keeping httpd running!

Whenever you can, please avoid using gettext from very beginning, this is really something ancient and should be deprecated for good.

How do you use gettext on server (Apache) you can’t restart?

the post here on the gettext function has some information for making gettext work without restarting apache:

http://www.php.net/manual/en/function.gettext.php#58310

Translation files PO/MO cached by Apache

There is an existing answer for this problem of cache when using gettext for i18n internationalization.

The solution linked above will flush the cache after every requests, but as @xiaoyi said, this solution is not something you should use in production environment.

As far as I am concerned, I already used gettext but I didn't remember any problem of apache caching. I was using POEdit for editing & compile PO/MO files.

EDIT : I guess that using php-fpm causes this problem because I wasn't using php-fpm and wasn't concerned about this issue.

Good luck

Gettext caching annoyance

Every app that uses gettext always uses the cache while the app is running, the file is probably opened at start-up/first use and then stored in memory until closed down. PHP is continually running as part of apache (via mod_php) rather than starting up and closing down.

If you use php-cgi it doesn't have this problem as PHP is started by apache when needed.

Gettext was originally developed for desktop apps, not long running server side applications that the "sysadmin" doesn't have control over stopping and starting. You wouldn't expect a desktop app to change while running it.

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?

How could I parse gettext .mo files in PHP4 without relying on setlocale/locales at all?

Ok, I basically ended up writing a mo file parser based on Zend's Gettext Adapter, as far as I know gettext is pretty much reliant upon the locale, so manually parsing the .mo file would save the hassle of running into odd circumstances with locale issues with setlocale. I also plan on parsing the Zend Locale data provided in the form of xml files.

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"

Gettext working intermittently

In my case the problem is solved by restarting Apache (or php5-fpm when I use nginx on remote server) every time I make changes to the translations files. Otherwise, gettext works glitchy as described in the question.

Non-deterministic gettext behavior in PHP

There was indeed a problem in the directory, because the code with __FILE__ was in different directory and I included it and didn't realize that the __FILE__ will then give wrong path. So I hardcoded the path instead:

bindtextdomain('messages', './lang/nocache'); # http://stackoverflow.com/a/13629035 
bindtextdomain('messages', './lang');

But what is really strange is that even with the wrong directory, it sometimes worked!!! This is really treacherous.



Related Topics



Leave a reply



Submit