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

发布于 2025-01-04 10:57:46 字数 1617 浏览 4 评论 0原文

我正在用 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>

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

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

I'm building a website in php and I handle the translation with gettext. So far everything is perfect but the website at some point sends an email to the user and I can't get the email to be translated.

I translate the website using .mo files and with a session I chose the language I'll be using:

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

Inside locale/en_US.utf8/estribo.mo I have all the strings translated and it works fine when I sue it anywhere in the page such as this:

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

It will translate perfectly this way the rpoblem is when I do the same with the content of a variable (string) that I will later on send it via mail instead of print it on the screen.

This is my code for the email (checkout.php):

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

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

The way I get to the checkout.php is via 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;

}

Everything works and the strings are translated in the .mo file but the $message variable is not translating

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2025-01-11 10:57:46

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

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

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

the most likely reason I can think of:

when you call checkout.php directly via AJAX, the environment for gettext is not configured.

I assume that you have some kind of bootstrap process that takes place when you visit your website via a browser and use index.php or similar as entry point. If you point the AJAX call directly to checkout.php, this bootstrap process may be omitted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文