Is it possible to temporarily override the language setting?


I have a bilingual site working no problem that tests for cookies before setting WPLANG in wp-config.php, with all the text internationalised etc.

My one problem is that notification emails are being sent in the language of the current user, not the language of the recipient. So if user1 comments in English, and user2, who uses the site in Spanish but replies in broken English, the comment notification email that user1 receives will be in Spanish.

I have a language preference stored for each user, so I intend to customise how the emails are sent so that it checks for the language of the recipient first.

So, to the question.

Here is a sample line plucked from wp_notify_postauthor() in pluggable.php which uses gettext calls to construct the email message:

$notify_message  = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "rn";

I’m hoping to continue with this gettext-based method.

But that would mean temporarily switching the locale setting immediately prior to constructing the email message, and then switching it back again immediately afterwards.

[edit] Reading through the core files I think I might better describe this as unloading the text domain prior to sending the emails and reloading afterwards, where required. To optimise I could create a text-domain subset specifically for notification messages to load and unload, rather than the text-domain for the entire site, and, of course, when sending multiple notifications, group the messages to send by language [/edit]

Is that a) meaningful, b) possible, and c) sensible?

Because the alternative is to drop gettext here and have both (and eventually, all) language versions there in the php and build the email string accordingly based on my language test.

terraling 7 years 2014-02-14T12:15:39-05:00 0 Answers 68 views 0

Leave an answer