Skip Navigation

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.

Tagged: 

This topic contains 5 replies, has 3 voices.

Last updated by Mateus Getulio 3 years, 4 months ago.

Assisted by: Mateus Getulio.

Author Posts
September 17, 2021 at 7:29 am #9606421

scottM-30

Tell us what you are trying to do?
String Translation for a string that is located in the child theme functions.php file

Code snippet where the string is and the string is "Remove Favorites".
-------------------
function woocommerce_after_single_product_summary_favourite(){ ?>

<?php if ( is_user_logged_in() ) {

$user_id = get_current_user_id();

$key = 'favorite_color';

$single = true;

$favorite_color = get_user_meta( $user_id, $key, $single );

$favorite_color_array = maybe_unserialize($favorite_color);

?>

<input type="hidden" name="post_id" id="post_id" value="<?php echo get_the_ID();?>">

<?php if(count($favorite_color_array) > 0){ ?>

<div class="row favourite_block_section">

<div class="col-md-12 col-sm-12 col-xs-12">

<button id="remove-favorite" class="remove-favoriter">Remove Favorites</button>

</div>

</div>

<?php } ?>

<div style="clear:both"></div>

<?php

if($favorite_color_array){ ?>

<div class="favorite_color_outer">

<?php $i=1;

foreach($favorite_color_array as $favorite_color){

if($favorite_color !=""){ ?>

<div class="favorite-items" data-toggle="tooltip" data-placement="top" title="<?php echo get_the_title($favorite_color); ?>">

<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $favorite_color ), 'single-post-thumbnail' ); ?>

<img src="<?php echo $image[0]; ?>">

<input type="checkbox" name="get_favorite[]" class="styled-checkbox get_favorite" value="<?php echo $favorite_color;?>" id="styled-checkbox-<?php echo $i;?>">

<label for="styled-checkbox-<?php echo $i;?>"><?php //echo get_the_title($favorite_color); ?></label>

</div>

<?php } $i++;

} ?>

</div>

<?php }

}

}
------------------------------

What is the link to your site?
The site link is show above....

The location of the string is on the "Remove Favorites" button located on all product ordering pages...

September 19, 2021 at 9:34 am #9614591

Mihai Apetrei
Supporter

Languages: English (English )

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

Hi there.

You will need to turn this:

<button id="remove-favorite" class="remove-favoriter">Remove Favorites</button>

into this:

<button id="remove-favorite" class="remove-favoriter"><?php _e('Remove Favorites', 'my-theme-domain'); ?></button>

I just followed the example here:
https://wpml.org/documentation/support/translating-the-theme-you-created/#translation-plus-echo

After that, re-scan your theme once again:
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#scanning-for-hard-coded-strings

And that's it 🙂 you should now be able to find the code in WPML > String Translation and translate it.

Kind regards,
Mihai Apetrei

September 22, 2021 at 8:25 am #9634785

scottM-30

Yes, your instructions did work which was for php code however there are still related strings that need to be translated but those are inside custom javascript file.

For example: if someone clicks on the "Remove Favorites" button (php code above) before selecting any favorites to remove, a popup message will appear which is originating from the custom javascript file. Unfortunately it seems WPML is not picking this up.

Here is a code snippet of javascript:

jQuery(".remove-favoriter").click(function(){
var ajaxurl = customObj.ajaxurl;
var post_id = jQuery('#post_id').val();
var selectedcolor = new Array();
jQuery(".favorite-items input[type=checkbox]:checked").each(function () {
selectedcolor.push(this.value);
});

if(selectedcolor.length == 0){
alert('Please select atleast one to remove from favourite color!');
return false;
}

if(confirm('Are you sure that you want to remove favorite color?')) {
$('#cover-spin').show();
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: {'action':'remove_all_favorite','selectedcolor':selectedcolor,'post_id':post_id},
dataType: 'json',
success: function(data) {
if(data.result == 1){
$('#cover-spin').hide();
location.reload();
}
},
error: function(data) {
console.log(data);
}
});
}
});

September 24, 2021 at 1:55 am #9650373

Mihai Apetrei
Supporter

Languages: English (English )

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

Hi there and welcome back.

I am happy to hear that the code worked 🙂

In regards to the new question, I created this custom search URL for you below so that you can take a look at existing tickets on the same topic:
hidden link..69i57j69i58.10680j0j7&sourceid=chrome&ie=UTF-8

That should help you find the solution you are looking for.

Please let me know how that goes. 🙂

Mihai

September 30, 2021 at 10:00 am #9694347

scottM-30

Hello,

Thanks for the link however I think we may be talking about two different things. 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.

Hope this make sense.

September 30, 2021 at 7:14 pm #9698875

Mateus Getulio
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

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

Hello there,

Thanks for your reply.

Mihai is not available today, so I'll continue to handle this ticket. I hope that it is alright for you.

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.

Please, check the following link with a tutorial, that can help you understand better this: hidden link

Give it a try and let us know about your results. Thanks!

Kind regards,
Mateus.