Easier alternative string translation using Advanced Custom Fields

There are several translation plugins in the WordPress ecosystem, each of which has its strengths and weaknesses when it comes to translating and managing multilingual content. There is also the brilliant plugin Loco Translate, which I’ve been using for a very long time to translate English strings in plugins and themes. (Usually German, French and Italian.)

An occasional need arises for a client to translate an individual string in a way which is specific to their project. For example, I’m working on a Woocommerce project at the moment, in which the client is using the Request A Quote plugin. Although this plugin is great, there are a few strings which don’t quite match the client’s requirements. Instead of “Bestelldetails” (order details), the client wants to have “Anfragedetails” (request details) displayed instead.

The usual way to achieve this would be to mis-translate from English using Loco Translate. However, if there is a comprehensive translation file already provided, rewriting this file using Loco Translate will either lead to custom translations being lost during the next update, or to the file becoming stale if it’s moved to the languages/loco/ folder.

As the site is making use of Advanced Custom Fields Pro anyway, the solution I’ve chosen is to add a custom options page featuring an ACF repeater field, into which the client can add alternative translations as required. For the sake of performance, these values are saved into a Transient when the options are saved, and then loaded through the gettext and gettext_with_context filter hooks.

You can check out the (poorly-commented!) code in this Github repository.

ACF repeater field in use for alternative translations

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.