php - 使用 gettext 在 ajax 调用中翻译电子邮件

标签 php javascript jquery translation gettext

我正在用 php 构建一个网站,并使用 gettext 处理翻译。到目前为止,一切都很完美,但网站有时会向用户发送一封电子邮件,但我无法翻译该电子邮件。

我使用 .mo 文件翻译网站,并在 session 中选择我将使用的语言:

    $lang=$_SESSION['lang'];
switch ($lang){
    case 'en':
        setlocale(LC_ALL, 'en_US.utf8'); 
        putenv('LC_ALL=en_US.utf8');
        bindtextdomain("estribo", "locale");
        bind_textdomain_codeset("estribo", 'UTF-8'); 
        textdomain("estribo");
    break;
    case 'es':
        putenv('LC_ALL=es_ES.utf8');
        setlocale(LC_ALL, 'es_ES.utf8');
        bindtextdomain("estribo", "locale");
        bind_textdomain_codeset("estribo", 'UTF-8'); 
        textdomain("estribo");
    break;
     }

在 locale/en_US.utf8/estribo.mo 中,我翻译了所有字符串,当我在页面中的任何地方起诉它时,它工作正常,例如:

   <a href="index.php"><?echo _("Index");?></a>

当我对变量(字符串)的内容执行相同的操作时,它会以这种方式完美地翻译,稍后我将通过邮件发送它而不是将其打印在屏幕上。

这是我的电子邮件代码 (checkout.php):

$message = _("Some text to send via email").": \n";
//a few more lines of 
$message .= _("Some more text").": \n";

mail($email, $subject, $message);

我到达 checkout.php 的方式是通过 ajax:

 function () {

    $.post('checkout.php', {
        cart: this.cart.items,
        total: this.format(this.total),
        quantity: this.quantity,
        shipping: $('select.shipping').val(),
        tipopago: $('input.tipopago').val(),
        customer: $('#checkout-form').serialize()
    }, function (result) {
            window.location.href = result;
        });
    return true;

}

一切正常,字符串在 .mo 文件中被翻译,但 $message 变量没有翻译

最佳答案

我能想到的最可能的原因:

当您通过AJAX 直接调用checkout.php 时,没有配置gettext 的环境。

我假设您在通过浏览器访问您的网站并使用 index.php 或类似文件作为入口点时会发生某种引导过程。如果您将 AJAX 调用直接指向 checkout.php,则可以省略此引导过程。

关于php - 使用 gettext 在 ajax 调用中翻译电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9225380/

相关文章:

php - 在目标 URL 中使用感叹号 + 主题标签进行 curl

jquery 屏蔽输入

javascript - 将 HTML 表格导出到 excel 在 IE 中不起作用

jquery - 在 jQuery UI 日期选择器中将今天的日期设置为默认日期

jquery - JavaScript/jquery : Cleaning up after once-executed script tags

php - 如何在 Laravel Eloquent 中使用带有子查询的内连接

php - IN array in where 子句 mysql php pdo

PHP 计数 JSON 数组

javascript - 错误 : Actions must be plain objects. 在删除按钮中使用自定义中间件进行异步操作?

javascript - CSS 全局类问题与 child