This thread is resolved. Here is a description of the problem and solution.
Problem:
The translation I need is for a javascript popup window which I think is generated by browser or something else. The popup window has two buttons Close and Cancel. As you will see from the code above, the text of those buttons is not defined in the script and rather the script just calls a function to show the popup.
So I need to translate the words Close and Cancel.
Solution:
Unfortunately, it is not possible to translate these strings into JavaScript. To do this, you must use custom code. You can localize these strings by using the "wp_localize_script" function: https://developer.wordpress.org/reference/functions/wp_localize_script/
This is necessary because WordPress currently only offers a localization API in PHP, not directly in JavaScript.
In the 'Example' section of the link above, you can check that it'll create a JavaScript code having an object "object_name" with some values, among these values the "some_string".
This code must be within PHP (usually, the recommended file is the functions.php of your theme). To access the JavaScript object, you will only use values that have already been defined within your PHP file.
That is, they are two pieces of code that are going to be defined in two different places. One is PHP (functions.php of your theme), so you can define the translatable string. The second part will be the JavaScript object inside the JavaScript file that you want to use it.
Relevant Documentation:
Please, check the following link with a tutorial, that can help you understand better this: https://pippinsplugins.com/use-wp_localize_script-it-is-awesome/
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 5 replies, has 3 voices.
Last updated by 3 years, 4 months ago.
Assisted by: Mateus Getulio.