1

When you develop WordPress plugins and internationalize them, the official WP docs instruct you to use gettext, by generating the according .po and .mo files.

When your .mo files are however put to use in your application under the hood, wordpress parses your translation files into an associative array and extracts the translations as such, instead of relying on the native PHP gettext functions.

I now wondered why. Probably the main reason I can think of is that Apache servers require a restart (or the killing of all running PHP processes, if run as PHP-FPM) to make use of updated translations. Yet I've also come across the PHP docs saying:

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API , you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

In server-side applications where I did not use WordPress, I always switched gettext locales, before retrieving the according localizations, using for example this, for a switch to german, as instructed here:

putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');

But why does WordPress not rely on this, and instead on array-parsed translation access? Does this has something to do with the thread-problem mentioned above (which I don't really understand), or are there even further bigger risks when using gettext in the native way with PHP?

1 Answer 1

4

When your .mo files are however put to use in your application under the hood, wordpress parses your translation files into an associative array and extracts the translations as such, instead of relying on the native PHP gettext functions.

Because it isn't installed often and not always available, so a PHP based mo reader is needed no matter what they decided.

The same was true of several other language features that depended on the standard library because it was possible in PHP 5.2 and older to turn them off.

Keep in mind that WordPress installs serve trillions of page views per hour across every possible kind of install on hugely varying machines from super computers to embedded devices. Changing server requirements isn't done lightly, and changes that sound straightforward and sensible, even obvious, can have unexpected and difficult consequences

A ticket does exist to make use of it in the case that the extension is installed:

https://core.trac.wordpress.org/ticket/17268

More recently, there have been efforts to evaluate translation performance and improve things:

https://make.wordpress.org/core/2023/07/24/i18n-performance-analysis/

The general consensus around native gettext is that it has a poor API, the low install base means very few will benefit, and of those that do install it most have very few locales installed which causes problems, particularly since they need installing at a server level, not to mention concerns around disk usage, hardlinks, file copies, etc.

Plugins do exist so if you're happy to do it all yourself you can get those benefits. Performance checks indicate it gives a big speed boost but not as big as PHP based MO parsers such as Ginger MO.

Instead the current best contender is to translate the po/mo files into PHP files so that the loading and parsing step can be skipped entirely after the first attempt. Note for future readers that this is not a finalised process and you should refer to the Make WP blogs to find progress or discover what happened. Take a look at the performant translations plugin and the performance Make blog.

2
  • Cheers! Yeah I guess without having root or at least some config skills / access, in the end it's way too painful anyways to use gettext for a nondev-audience.. But yeah this is no criticism at all against gettext, totally love it!
    – DevelJoe
    Sep 8, 2023 at 16:51
  • give the performant translations plugin a go, it needs feedback and in theory has superior performance to gettext
    – Tom J Nowell
    Sep 11, 2023 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.