plugins – Setting locale before retrieving gettext translations


I developed several plugins which I localized using the standard approach with __() functions and the .po or rather the .mo files. All of this works like a charm.

What I now want to be able to do is set the locale for a function call of __(), to retrieve the translation you defined on your plugin’s .mo for a translation which is different from the current locale. Example:

A) Let’s say you’re creating a user for your website in your wp admin, on a german wp admin page.

B) This user wants to have a profile in English.

C) In your wp admin, you’ve automated a mailing, such that, when a user gets created, he / she gets notified with an e-mail about that, in the language according to B).

Problem: Mail contents are translated using __() too. So, when I create a user on a german admin page, the german version of the notification mail will be sent to the user, instead of the english one. So what I’m looking for is a solution to call the translation defined via your .mo files which is different from the current locale; and only for this single function call (like, not permanently change the locale due to this). Sth like:

__( 'get-this-translation', 'of-this-text-domain', 'of-this-locale' );

instead of

__( 'get-this-translation', 'of-this-text-domain' );, which obviously always defaults to currently active locale; in the example above german.

I know that simply hardcoding the translation strings on the admin side, instead of using __() here, would solve the problem; but I’d prefer my translations to be translated all in the same way, using the same logic provided by wordpress. Hence I’d like to use an approach via the __(), if possible.

DevelJoe 2 months 2021-08-11T16:07:37-05:00 0 Answers 0 views 0

Leave an answer