Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用gettext在ajax调用中翻译电子邮件_Php_Javascript_Jquery_Translation_Gettext - Fatal编程技术网

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

Php 使用gettext在ajax调用中翻译电子邮件,php,javascript,jquery,translation,gettext,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');

我正在用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的方式是通过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,则此引导过程可能会被忽略。

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

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


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

使用gettext,让我更新问题。使用gettext,让我更新问题。也许我必须在checkout.php内再次切换,我没有这样做//是的,就是这样,有时您只需要指向正确的方向:干杯!您是如何引导gettext环境的?我在Wordpress中也尝试着这么做:这是一个非常古老的答案。。。但也许@ElaineMarley还记得这个实现。据我所知,您需要将gettext作为服务器上的一个模块,因此在基于ubuntu/debian的服务器上,您需要执行sudo apt get install php gettext,编写一些翻译文件.mo文件,并像Elaine在switch语句中那样设置参数。当然,参数设置应该在任何文本输出发生之前完成。对不起,我不知道wordpress的程序流程,但是如果你在某个特定的步骤上遇到困难,也许一个新的问题会更合适;也许我必须在checkout.php内再次切换,我没有这样做//是的,就是这样,有时候你只需要指向正确的方向:干杯!您是如何引导gettext环境的?我在Wordpress中也尝试着这么做:这是一个非常古老的答案。。。但也许@ElaineMarley还记得这个实现。据我所知,您需要将gettext作为服务器上的一个模块,因此在基于ubuntu/debian的服务器上,您需要执行sudo apt get install php gettext,编写一些翻译文件.mo文件,并像Elaine在switch语句中那样设置参数。当然,参数设置应该在任何文本输出发生之前完成。对不起,我不知道wordpress的程序流程,但是如果你在某个特定的步骤上遇到困难,也许一个新的问题会更合适;
 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;

}