Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are trying to translate text from a custom JavaScript on your website, but you're unable to do so because the strings are added through JavaScript and not localized.
Solution:
To make the text in your JavaScript translatable, you should use the

wp_localize_script

function. This function allows you to pass PHP variables to JavaScript. For more details on how to implement this, please refer to the WordPress developer resource: wp_localize_script
Additionally, you can find a practical example discussed in our forum here: WPML support forum topic
If you need further customization, we recommend contacting one of our third-party contractors who specialize in custom work with WPML: WPML Contractors

Please note that this solution might be irrelevant if it's outdated or not applicable to your specific case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If this does not resolve your issue, please open a new support ticket at WPML Support Forum.

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.

Tagged: 

This topic contains 1 reply, has 0 voices.

Last updated by Mihai Apetrei 1 month, 1 week ago.

Assisted by: Mihai Apetrei.

Author Posts
January 23, 2025 at 1:34 pm #16628144

milcoP

Background of the issue:
I am trying to translate text from a custom JavaScript on my website hidden link.
it is used on this page: hidden link

Symptoms:
No specific issue or error message mentioned.

Questions:
How can I translate a custom JavaScript?

January 23, 2025 at 1:44 pm #16628264

Mihai Apetrei
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+02:00)

Hi there.

Unfortunately, it is impossible to translate these strings with String Translation because they are added through JavaScript and not localized.

WordPress currently only offers a localization API in PHP, not directly in JavaScript. To fix this and make the text translatable, you can use the "wp_localize_script" function: https://developer.wordpress.org/reference/functions/wp_localize_script/

For more details, please check the reply from my colleague here: https://wpml.org/forums/topic/string-translation-138/#post-9698875

Kindly note that custom programming work is beyond our scope of support. If the above suggestions didn't help or you're unsure about how to achieve it, we suggest contacting one of our 3rd party contractors that specialize in custom work with WPML
https://wpml.org/contractors/

I hope that you will find this answer helpful.

Mihai