admin管理员组

文章数量:1315079

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 ) . "\r\n";

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.

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 ) . "\r\n";

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.

Share Improve this question edited Feb 16, 2014 at 9:25 terraling asked Feb 14, 2014 at 18:15 terralingterraling 1653 silver badges10 bronze badges 4
  • I am still not sure I understand your setup: you provide different languages under the same address? – fuxia Commented Feb 16, 2014 at 10:46
  • Yes, it is a multi-lingual site (currently bilingual, others to be added). In wp-config.php WPLANG is set for each user (first time via browser language check, subsequently via a cookie). – terraling Commented Feb 16, 2014 at 16:14
  • This is not a good setup. Search engines will never find the second language. I strongly recommend to use a multi-site with one separate site for each language. – fuxia Commented Feb 16, 2014 at 16:21
  • Search engines are not so much of a consideration, usability is. It's not a blog, it has a social layer using BuddyPress, will be used by a multi-lingual audience who will interact with each other, they don't want segregating into their own silos, any more than they are on sites like facebook or twitter. If I go down the multi-site route then I have to work out linking the content so that people on the "English" site see and can interact with the content on the "Spanish" site and vice versa. – terraling Commented Feb 16, 2014 at 16:28
Add a comment  | 

2 Answers 2

Reset to default 1

Add a locale filter

add_filter('locale', 'locale_filter');

and in the filter return the locale of your choice,

function locale_filter($locale) {
    return "whatever";
}

however, this needs to be done in context, your mail sending trigger will need to know the target user and get the locale from there, not very easy, although doable.

I am unsure if I understand your question entirely, but in any case, I would advise against editing WPLANG directly, and avoid messing with cookies that much. WordPress already differentiates between a site-wide language setting and a user-specific language preference:

Site-wide language setting (Settings > General)

User-specific language preference (Users > Your profile)

The latter setting is only visible if multiple site languages are installed.

Doesn't this solve the e-mail translation issues?

本文标签: Is it possible to temporarily override the language setting