1

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

Elaine Marley
  • 2,143
  • 6
  • 50
  • 86

1 Answers1

6

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.

cypherabe
  • 2,562
  • 1
  • 20
  • 35
  • Maybe I have to do the switch again inside checkout.php, I'm not doing that// Yep, that was it, sometimes you just need to be pointed in the right direction :) Cheers! – Elaine Marley Apr 20 '12 at 11:33
  • How did you go about bootstraping gettext environment? I'm trying to do the same in Wordpress :) – trainoasis Apr 06 '18 at 06:26
  • this is quite an old answer... but maybe @ElaineMarley remembers the implementation. As far as I know you need `gettext` as a module on your server, so on ubuntu/debian based servers you'll need to do a `sudo apt-get install php-gettext` , write some translation files (.mo files) and set the parameters like Elaine did in the switch statements..the parameter settings should be done before any text output happens, of course. Sorry, I don't know the wordpress programm flow – cypherabe Apr 06 '18 at 09:09
  • but maybe a new question would be more fitting, if you are stuck at a specific step ;) – cypherabe Apr 06 '18 at 09:11