Skip Navigation

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

Problem:
You are trying to use a variable within functions.php in a custom shortcode for your WordPress site, but the text within the variable is not being recognized by WPML for translation.
Solution:
The strings you are attempting to translate are not following the WordPress localization standards, which is why WPML cannot register them for translation. To resolve this, you need to modify your code to adhere to these standards. Here is how you can update your code:

_e( 'Congratulations! Shipping is on us :', 'my-theme' )

_n('Free delivery on orders of %s or more.', wc_price( $min_amount ), 'my-theme' )

After updating, ensure to perform a full site backup. Then, scan the theme for the strings to translate by following the instructions here: Scanning the theme or plugin.

If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. We also advise 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. For further assistance, please visit our support forum 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 2 voices.

Last updated by Bigul 3 weeks, 1 day ago.

Assisted by: Bigul.

Author Posts
August 29, 2024 at 2:19 pm #16120558

bW-2

Background of the issue:
I am trying to use a variable within functions.php in a custom shortcode for my WordPress site hidden link. The text is not a simple string; it is a variable. Here is the code I am working with:

add_shortcode( 'bw-shipping-difference', 'bw_get_shipping_difference' );
function bw_get_shipping_difference( $atts ) {
if ( ! WC()->cart ) {
return 'N/A';
}

$min_amount = 399; // YOUR FREE SHIPPING THRESHOLD

// if cart is empty, return nothing
if ( WC()->cart->is_empty()) {
return 'Free delivery on orders of ' . wc_price( $min_amount ) . ' or more.';
}

$current = WC()->cart->subtotal;

if ( $current < $min_amount ) {
$added_text = 'Only ' . wc_price( $min_amount - $current ) . ' away from free shipping!';
} else {
$added_text = 'Congratulations! Shipping is on us :)';
}

return $added_text;
}

Symptoms:
The text within the variable is not being recognized by WPML for translation.

Questions:
How must the code be updated to allow WPML to see the strings and translate them?

August 29, 2024 at 5:46 pm #16121606

Bigul
Supporter

Languages: English (English )

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

Hello,

Welcome to the WPML support forum. I will do my best to help you to resolve the issue.

The strings you are trying to translate are not following the WordPress localization standards. So it will not be possible for WordPress and WPML to register the strings for translation. Please refer to the following documentation for more details.

https://developer.wordpress.org/themes/functionality/internationalization/

https://developer.wordpress.org/apis/internationalization/

https://wpml.org/documentation/support/translating-the-theme-you-created/

Therefore you have to change the code like the following (as an example). Please try it after a full site backup and let us know your feedback.

_e( 'Congratulations! Shipping is on us :', 'my-theme' )

_n('Free delivery on orders of %s or more.', wc_price( $min_amount ) , 'my-theme' )

https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#scan-the-theme-or-plugin (after changing the codes you have to scan the theme for the strings to translate)

--
Thanks!

Bigul

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.