Jump to content
Inxentas

Translations in external AJAX scripts

Recommended Posts

Hello there! I have a question about translations in external files. I remember a topic I started about a year ago about the subject, but I can't find it at the moment. In that topic I had some issues getting translations to work in PHP includes in templates. The solution was the way these files were included. Now I have a similar problem, but it's a bit different.

So far I've been able to let external files communicate with the ProcessWire API by including the index file. Imagine that you'd make a folder named ajax in the site folder. Any AJAX script stored there can access the $pages variable. If you set the user language ($user->language) you can even get the translated fields without issues. However, I don't seem to be able to work with translated files.

Whenever I include the index file...

include_once('./../../index.php');

I can access fields without problems, they even translate properly...

$example = $pages->get('/example-page-url/')->example_field;

But when trying to access a file based translations, I get some weird inconsistent behavior. Whenever a string is surrounded by the translation method, it is found in the backend, but not translated, even when the users language has been set to the required language:

_('This string is recognized, but not translated.')

Secondly, when I happen to include another file in that file, translatable phrases are no longer recognized unless I put an extra underscore in the method name:

__('I have no idea why, but only like this does the backend recognize me.')

Phrases found in this way are also not translated, in fact they cause an error due to the method "__" not existing. Note, those are two underscores.

I am at a loss why this behaviour takes place. Does anyone know what I could be doing wrong? To summarize:

  • translations in external AJAX scripts that include the index file, are recognized by the backend but not actually translated.
  • whenever these files include other files, translation behavior get weird. The method requires an extra underscore for the phrase to be found, but logically this causes an error.

Share this post


Link to post
Share on other sites

Has this been solved? I wish to know the solution.

Share this post


Link to post
Share on other sites

As for me, I always make separate templates for the ajax scripts. Something like ajaх-handler. This way I always get the API working right. I believe it is the recommended way to deal with this. Sorry for not answering the original question directly)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...