首页>文档>TranslatePress教程>以用户首选语言发送电子邮件

以用户首选语言发送电子邮件

TranslatePress 以用户首选语言显示前端内容。如果任何电子邮件是通过电子邮件操作(例如注册、下订单)触发的,则电子邮件将以用户的语言发送。但是,如果发送给用户的电子邮件是由管理员操作(或其他用户)触发的,则它可能不会以收件人首选语言发送。

TranslatePress 完全支持以首选用户的语言翻译 WooCommerce 电子邮件,但如果您希望为其他插件/主题提供支持,请使用以下代码。

如果您的插件不使用 gettext,而是使用像 Mustache 这样的自定义模板系统,那么像“Hello {{name}}”这样的字符串就不需要手动翻译,因为它们会随着每个用户而改变。

在这种情况下,我们建议您在主题和电子邮件正文中添加对短代码的支持(do_shortcode),以便用户可以使用条件语言短代码来翻译电子邮件

<?php 

$user_language = trp_get_user_language( $user_id );
trp_switch_language( $user_language );

/* Load plugin textdomain in the user's language. Do this for all plugins you intend to use.
* Make sure the localization functions ( such as __() ) applied on the strings needed are called after this point.
* If they are already called and stored in a variable then they won't be in the user's language.
*/
load_my_textdomain();

// generate localized strings
$message = __('Email content', 'my-domain');

// this text will be translated by TranslatePress as a regular string using existing manual translations (or machine translated if available)
$message .= 'User-inputted content written in the default language';

$subject = do_shortcode($subject);
$message = do_shortcode($message);

//send email using wp_mail function
wp_mail( $email, $subject, $message);

/*
* Restores previous language so that the rest of the page loading happens in the expected language,
* which is not necessarily the $user_language
*/
trp_restore_language();

// call the function to load textdomain back in the previous language
load_my_textdomain();


/*
* Example to load woocommerce textdomain in the user's language.
*/
function load_my_textdomain(){
$locale = determine_locale();

unload_textdomain( 'woocommerce' );
load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
}

当登录用户上次访问前端的任何页面时,用户的首选语言会自动存储和更新。

可以在默认的 WP Edit Profile 表单中查看和更改它。为了阻止语言自动更新,您可以选中始终使用此语言 复选框。用户仍然可以使用前端语言切换器更改其前端语言首选项。

以用户首选语言发送电子邮件-HEIWP-外贸建站
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
搜索