Gettext returned via AJAX untranslated

Can gettext translate text via AJAX from php file?

This is an 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 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 string somefile.php to translate ... and "Other Text" translates correctly. But "Text for Translation" is not ... :(

Any ideas?

+4
source share
1 answer

I had the same problem because my ajax file folder was not in the root folder of the site and I used the relative paths to the bindtextdomain () function just like you did.

So, instead of a relative path:

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

I used the absolute server path:

 bindtextdomain($po_domain, "/var/www/folder/locale"); 
0
source

All Articles