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

Problem: I would like to translate strings in JavaScript.

Solution: The strings need to be wrapped inside a getText() function like this:

document.bmiForm.meaning.value = "<?php _e( 'Normal weight.', 'themetextdomain' ); ?>"

This is useful when the variables are included inside the html. Otherwise, it is easier and safer to use the wp_localize_script() function.

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 2 replies, has 2 voices.

Last updated by davidG-36 5 years, 2 months ago.

Assigned support staff: Pierre.

Author Posts
April 24, 2015 at 7:11 pm #605959


Well, I have this javascript code in my header.php and want to know how to translate it:

<script language="JavaScript">
function calculateBmi() {
var weight = document.bmiForm.weight.value
var height = document.bmiForm.height.value
if(weight > 0 && height > 0){
var finalBmi = weight/(height/100*height/100)
document.bmiForm.bmi.value = finalBmi
if(finalBmi > 18.5 && finalBmi < 25){
document.bmiForm.meaning.value = "Normal weight."
if(finalBmi > 25 && finalBmi < 30){
document.bmiForm.meaning.value = "You have overweight."
if(finalBmi > 30 && finalBmi < 35){
document.bmiForm.meaning.value = "Overweight - I Stage."
if(finalBmi > 35 && finalBmi < 40){
document.bmiForm.meaning.value = "Overweight - II Stage."
if(finalBmi > 40) {
document.bmiForm.meaning.value = "Overweight - III Stage."
alert("Please Fill in everything correctly")

Only these strings must be translated.

"Normal weight."
"You have overweight."
"Overweight - I Stage."
"Overweight - II Stage."
"Overweight - III Stage."

April 24, 2015 at 7:42 pm #605981


Languages: English (English ) French (Français ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)


Thank you for your message!

To make the strings translatable, you must wrap them into a getText() function, like this:

document.bmiForm.meaning.value = "<?php _e( 'Normal weight.', 'themetextdomain' ); ?>"

Then, to translate the strings and provided that you have the "WPML String Translation" add-on installed please follow these steps:

1. Go under "WPML -> Theme and plugins localization"
2. Make sure that "Translate by WPML." is active
3. Scroll down to "Strings in the theme" and click on "Scan the theme for strings"
4. Once you followed the above steps go under "WPML -> String Translation". All strings available for translation should display in the table you see there.

Do you see them? If yes, all you need to do is click on "translations" next to each string you want to translate.

Let me know if this helps.


April 25, 2015 at 2:56 am #606075


It works. Big thanks, Pierre.