Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gettext returned via AJAX is untranslated

Is gettext able translate text return via AJAX from a php file?

This is a rough example of what I'm trying to do

<div id="resultText"></div>
<?php echo gettext('Other text'); ?>

<script>
$(document).ready(function() {

    $.post('somefile.php', somedata, function(r) {
        $('#resultText').html(r);
    });

});
</script>

And the php file:

<?php // somefile.php

// gettext setup (from an included file)

$lang = "de_DE";
if (isset($_GET['lang'])) $lang = $_GET['lang'];
putenv("LC_ALL=$lang");
setlocale(LC_ALL, $lang);
bindtextdomain("de_DE", "locale");
bind_textdomain_codeset('de_DE', 'UTF-8');
textdomain("de_DE");

// do some logic

echo gettext('Text to be translated');

?>

POEdit picks up the somefile.php string to be translated... and 'Other text' is correctly translated. But 'Text to be translated' is not... :(

Any ideas?

like image 706
theblindfrog Avatar asked Nov 12 '22 10:11

theblindfrog


1 Answers

I had the same problem, because my ajax files folder was not in the site's root folder, and I was using relative paths on the bindtextdomain() function, just like you are.

So instead of the relative path:

bindtextdomain($po_domain, "./locale");

I used the absolute server path:

bindtextdomain($po_domain, "/var/www/folder/locale");
like image 123
riverstorm Avatar answered Nov 15 '22 05:11

riverstorm