WPML language translation not working in wordpress ajax call

Recently, I had come across a issue which I thought very useful to share with the developers working on WordPress and using WPML for language translations.

Issue: WPML language translation not working in wordpress ajax call

Solution:

1. Make sure you are passing language variable in with ajax url.

e.g. http://www.example.com?lang=ar

2. Use the following code at the top of the script:

global $sitepress;

Now, put the below code after your query (if the query does not have to be language specific) or before the query if it has to be language dependent.

$sitepress->switch_lang($get[‘lang’], true);

You can set the language to its default value by using the following code:

$sitepress->switch_lang(null);

It has been tested and I believe it will solve your problems in getting the translated string usin get text calls like

Please share your thoughts if it was useful for you. Thanks!

Related Post