This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 9 replies, has 2 voices.

Last updated by jose-mariaG-11 1 month, 1 week ago.

Assigned support staff: Riffaz.

Author Posts
September 9, 2019 at 8:39 am #4534697

jose-mariaG-11

Hi,
I had to include a custom javascript for woocommerce product pages.
This javascript has texts, what is the best way for these texts to be translated by WPML according to the active language of the web?
Thanks for your help.
Regards,
Jose

September 9, 2019 at 1:39 pm #4537693

Riffaz
Supporter

Languages: English (English )

Timezone: Asia/Colombo (GMT+05:30)

Hi,

Thank you for contacting WPML support.

We do not provide code in support which is out of the support scope.

Anyhow, the following solution solved another users with the same issue:

Typically in WordPress you have to follow this format to use different code for different languages.

<?php
  
add_action( 'wp_head', 'my_custom_js' ); 
function my_custom_js() {
    if(ICL_LANGUAGE_CODE=='en'){?>
        <!--- your code to english--->
      
    <?php } elseif(ICL_LANGUAGE_CODE=='de'){?>
        <!---your code to german--->
  
<?php 
    } elseif(ICL_LANGUAGE_CODE=='fr'){?>
        <!---your code to french----->
  
<?php 
    } elseif(ICL_LANGUAGE_CODE=='es'){?>
        <!---your code to spanish--->
<?php 
    }
}

Set and adjust your code according to this format and add this code to your functions.php
It will work.

Let me know how it goes.

Regards,
Riffaz.

September 10, 2019 at 8:51 am #4543309

jose-mariaG-11

Hi Riffaz,

I will do tests to see if this works correctly.
I already tell you something.
Thank you very much for your help.

Regards,
Jose

September 10, 2019 at 10:13 am #4543855

jose-mariaG-11

Hi Riffaz,

I've done some tests and it doesn't work properly.
Being in the Spanish version of a woocommerce product I do an AJAX call to a php in which I take the value of the constant "ICL_LANGUAGE_CODE" and return it as a JSON, but return "en". Being in the English version of the product I do the same call and also get "en".
what is happening?

Regards,
Jose

September 10, 2019 at 12:39 pm #4545349

Riffaz
Supporter

Languages: English (English )

Timezone: Asia/Colombo (GMT+05:30)

Hi,

Might be the issue is Ajax or your code.

Please check this normal pages and compare the results.
Many users use this code with NO any issue.

Almost all the WPML users use this code to implement different google analytics code in different language pages and it works without any issue.

Providing code snippets in the forum reply is out of support scope. Though, I did it. 🙂
If we have to check your code deep then you should consider https://wpml.org/contractors/

Let me know the results after checking this with normal pages and I will help if I can do something that does not go out side of the support scope.

Regards,

September 10, 2019 at 1:54 pm #4546157

jose-mariaG-11

Hi Riffaz,
It seems to be an AJAX problem, if I do it as a shortcode it works perfectly, apparently you have not considered its use in an AJAX call.
Is there any way to save the texts of each translation using the WPML system and then retrieve them with PHP?
Thansk for your help.
Regards,
Jose

September 11, 2019 at 6:33 am #4550811

Riffaz
Supporter

Languages: English (English )

Timezone: Asia/Colombo (GMT+05:30)

Hi,

Glad you got the real issue. 🙂

Is there any way to save the texts of each translation using the WPML system and then retrieve them with PHP?
- as far as I know this is not easy.
- this will require some complex code
- and that will a loads of time
- in that case, I can not ask our developers as well.
- as of now, you can consider https://wpml.org/contractors/

Apologies for the limitation that I can help with this.

Regards,

September 11, 2019 at 8:14 am #4551335

jose-mariaG-11

Thank you!

September 12, 2019 at 7:41 am #4558983

Riffaz
Supporter

Languages: English (English )

Timezone: Asia/Colombo (GMT+05:30)

Hi,

I just noticed that you do not have to create code to show different JS code as per language.

You said that you have javascript which contains the text to translate.
So, all you have to do is just localize/make it translatable.

Please refer: https://codex.wordpress.org/Function_Reference/wp_localize_script
As you know how to code this would be very easy.

For further reading about make strings translatable : https://wpml.org/faq/getting-string-translation-to-work/#method-3-using-gettext-for-hard-coded-strings

Regards,

September 12, 2019 at 1:31 pm #4561247

jose-mariaG-11

Wonderful. It works perfectly.
Thanks Riffaz.

Regards,
Jose