theblindfrog
theblindfrog

Reputation: 51

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?

Upvotes: 5

Views: 1023

Answers (1)

riverstorm
riverstorm

Reputation: 548

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");

Upvotes: 1

Related Questions