Score:0

Clear gettext cache of PHP-Apache Server to apply .mo updates

in flag

Although many questions on SO already relate to this, they're A) pretty old and B) I felt that it's better to ask about this here, as it seems to be related to some server-level, and not programming-level, cache.

The problem is that our server, using Apache and PHP, uses gettext with the .po and .mo files, in the native confoguration, that is using bindtextdomain with the folder structure:

translations/
    en_GB/
        LC_MESSAGES/
            mydomain.mo
    fr_FR/
        LC_MESSAGES/
            mydomain.mo

to for example load english and french translations for the mydomain text domain. Loading the translations to the server and using them for the first time with PHP's native gettext(), _(), etc. functions works, all good. The problem is that, when we update our .mo files with new translations, these updates are not applied. Several sources state that the apache server has to be restarted to flush the gettext cache:

etc.

Is it still true to the current date that gettext caches of a Apache-PHP application can exclusively be flushed by restarting the apache server? Is there no other approach to do this? Consider that we have a managed VPS-hosting, hence no root access & we're not allowed to restart Apache on our server.

I know that you could theoretically implement a logic which adds timestamps of your modifications to the domain name mydomain, but according to this post here, "changing the domain to get rid of the cache problem [...] will create lots of out-of-date cache in memory.", so I'm wondering if that approach is actually viable.

PHP is running as php-cgi on our Apache server. I've also noted now that the LC_MESSAGES folders contain a .nfs.... file which I am seemingly unable to delete (if I delete it, it simply reappears). Could this indicate that my .mo files have been reopened / accessed by some app, not closed, and thus cause some kind of unwanted translation cache?

HBruijn avatar
in flag
*"Consider that we have a managed VPS-hosting, hence no root access & we're not allowed to restart Apache on our server."* - That makes you an end-user rather the sysadmin. My recommendation would be to ask your questions to your IT-department / provider and the sysadmins who do have root access and can implement a solution/work-around if possible. ServerFault is not a suitable Q&A site for end-user support and we typically expect people to have sufficient insight and access to their environments to do and/or test & implement what is necessary.
DevelJoe avatar
in flag
I am talking with them already, and sorry my experience with server configs is weak, hence my question. But do you may know what these `.nfs...` files in my `LC_MESSAGES` are? People say that, if PHP is running as Apache Module, you will have gettext translations cached until you restart the server. Yet this does not seem to be accurate, as I have made tests with a VPS with root access. There, simply uploading the new `.mo` file works, no apache restart needed, and the code to load the text domains is exactly the same. But there are none of these `nfs` files.
HBruijn avatar
in flag
https://nfs.sourceforge.net/#faq_d2
DevelJoe avatar
in flag
Ok thanks. This sounds like some application of my server keeps running with old `.mo` files when I try to update them with new contents. I'm feeling that these `.nfs` files must be causing the issue with the wrongly retrieved gettext translations. Thanks! (And if you know any known issues in that context, feel free to let me know:))
Score:1
in flag

I've been reading on multiple places that this gettext cache should not be such of a big problem if PHP is not running as an Apache module on your server (and in my example, PHP's running as FastCGI). And indeed, the problem was actually a different one:

  • After listing all locales available on my Apache server via locale -a, I've got to see that the names of the available locales all include the encoding in their name. So instead of e.g. en_GB and fr_FR, the locales are en_GB.UTF-8 and fr_FR.UTF-8.
  • I thus adapted the folders from being the above-mentioned ones:
translations/
    en_GB/
        LC_MESSAGES/
            mydomain.mo
    fr_FR/
        LC_MESSAGES/
            mydomain.mo

To these:

translations/
    en_GB.UTF-8/
        LC_MESSAGES/
            mydomain.mo
    fr_FR.UTF-8/
        LC_MESSAGES/
            mydomain.mo

Then, everytime I Update the contents of my .mo files, I just login via ssh and run:

killall -9 php-cgi

And the new translations are properly loaded. Note that neither an Apache Restart nor root access is needed to do this. But note that the command:

killall -9 php-cgi

Had no effect before I included the encodings in the locale folder names; so it seems that the locale folder names (and the locales you specify when loading textdomains in PHP) must be in the same form as the locale names on your server, obtained via locale -a.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.