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

时间:2012-02-10 09:17:14

标签: php javascript jquery translation gettext

我正在用PHP构建一个网站,我用gettext处理翻译。到目前为止,一切都很完美,但网站在某些时候会向用户发送电子邮件,我无法收到要翻译的电子邮件。

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

    $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>

当我对变量(字符串)的内容做同样的事情时,它会完全转换为rpoblem,我稍后会通过邮件发送它而不是在屏幕上打印它。

这是我的电子邮件代码(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变量没有翻译

1 个答案:

答案 0 :(得分:5)

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

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

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