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.
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - | - |
| 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - | - |
Supporter timezone: Asia/Yerevan (GMT+04:00)
Tagged: Custom Work
This topic contains 5 replies, has 0 voices.
Last updated by Christopher Amirian 1 month ago.
Assisted by: Christopher Amirian.
| Author | Posts |
|---|---|
| May 9, 2026 at 9:30 am #18024183 | |
|
tysonO |
Hello Support Team, I am experiencing a stubborn issue where custom PHP snippets (such as a Free Shipping Bar and Volume Discount logic) are not displaying their translations on the frontend of my site, particularly during WooCommerce AJAX cart updates. Environment: The Issue: However, the frontend continues to display the default language (Dutch) instead of the translated language (e.g., German). This happens most notably when WooCommerce triggers an AJAX fragment refresh (e.g., updating the cart). Troubleshooting Already Completed: 1. Caching is NOT the issue: We have completely purged Varnish, WP Super Cache, and Elementor caches. Testing with `?noCache=1` in an Incognito window still shows the wrong language. Despite the database showing the strings are translated, the WPML engine is failing to deliver the translated text to the frontend during render/AJAX. Could you please advise on what might be blocking WPML from outputting these specific translated strings? Thank you, |
| May 10, 2026 at 7:51 am #18024864 | |
|
Christopher Amirian WPML Supporter since 07/2020
Languages: English (English ) Timezone: Asia/Yerevan (GMT+04:00) |
Hi, Welcome to WPML support. We will do our best to help, but please consider that the issue is regarding a custom code, which is outside of our support scope. What can I suggest at this stage: 1. Do not use woocommerce text domain WordPress's __() / esc_html__() translate strings via .mo files for the named text domain. WPML String Translation does not intercept arbitrary __() calls and substitute its database translations into them. WPML's intercept mechanism for theme/plugin domains only finds strings that already exist in that theme/plugin's .mo file or that have been registered through the proper String Translation pathway. Borrowing the woocommerce text domain for custom code is also a small i18n violation in itself — that text domain belongs to the WooCommerce plugin, and per WordPress.org's official internationalization guidelines a string's domain must match the plugin/theme that owns it (https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/). Mixing custom strings into another plugin's domain doesn't help WPML find them and can produce inconsistent results. For custom code that you control (Free Shipping Bar, Volume Discount logic, snippets in functions.php or a child theme), use WPML's purpose-built hooks instead:
// 1) Register the string once (e.g. on init, or when the value is saved)
do_action( 'wpml_register_single_string', 'My Custom Snippets', 'free_shipping_bar_text', 'Free shipping over €50!' );
// 2) Output the translated string in your snippet
$text = apply_filters(
'wpml_translate_single_string',
'Free shipping over €50!', // original value
'My Custom Snippets', // domain (must match #1)
'free_shipping_bar_text' // name (must match #1)
);
echo esc_html( $text );
After registering, the strings will appear in WPML → String Translation under the "My Custom Snippets" domain. and then translate them. 2. AJAX language context WPML does have an option to store the context to "wp-wpml_current_language" cookie for Ajax calls, I see that you already have that option enabled, but worths the check: - Go to "WordPress Dashboard > WPML > Languages > Language filtering for AJAX operations". For more information: After enabling, please also remove the do_action('wpml_switch_language', $lang) call from your AJAX handler — it shouldn't be needed and depending on where it sits in the request lifecycle, it can mask the real cause. 3. Varnish on AJAX responses You mentioned Varnish, and that you cleared it. Worth confirming separately: clearing Varnish purges currently-cached objects, but doesn't change how Varnish keys responses. If Varnish isn't varying its cache key by the wp-wpml_current_language cookie (or by language-bearing URL/header), it will keep serving the first AJAX response it cached for any given URL across all languages, even after a purge — because the second visitor's request will look identical to Varnish. Test it on an environment that does not have Varish cache at all in a staging version and see if it works. We will not be able to delve into your custom code to know what might be the problem cause, as this is considered outside of our support scope. For more information: Thanks. |
| May 10, 2026 at 9:04 am #18024928 | |
|
tysonO |
Hi WPML Support Team, Thank you for your help and explanation earlier. I updated my code based on your instructions and used the method you recommended. The translations now appear correctly inside the WPML String Translation area, and I translated them successfully. However, the problem on the website itself is still there. Even though the translations are saved in WPML, the website keeps showing the original English/Dutch text on all language versions. I also tried to make sure this was not caused by caching: * I completely turned off Varnish cache on the server. So at this point, it does not seem to be a cache problem. It looks like WPML can save the translations, but for some reason it is not showing them on the frontend of the website. I am using separate domains for each language (.nl, .de, .fr). Could you please advise what else I should check or try? Thank you, |
| May 11, 2026 at 10:21 am #18026943 | |
|
Christopher Amirian WPML Supporter since 07/2020
Languages: English (English ) Timezone: Asia/Yerevan (GMT+04:00) |
Hello, Thank you. There is no other thing that I can suggest. I will be happy to check that section of the code you added if you can use the CODE button in the ticket reply to paste so I can see if there is anything that I can pinpoint. Thanks. |
| May 11, 2026 at 11:50 am #18027207 | |
|
tysonO |
Hi, Here is code of one of my snippets: CODE: // ─── 1. WPML String Registration ────────────────────────────────────────────── // ─── 2. Region Thresholds ───────────────────────────────────────────────────── // ─── 3. Render Function ─────────────────────────────────────────────────────── $threshold = fsb_get_threshold(); $context_class = $context ? ' fsb--' . sanitize_html_class( $context ) : ''; // FETCH TRANSLATIONS VIA WPML API ob_start(); ?> // ─── 4. Styles ──────────────────────────────────────────────────────────────── // ─── 5. Hooks ───────────────────────────────────────────────────────────────── add_filter( 'woocommerce_add_to_cart_fragments', function( $fragments ) { // ─── 6. JS AJAX Trigger ─────────────────────────────────────────────────────── jQuery.post(ajax_url, { // ─── 7. AJAX handler (Cleaned up per WPML guidelines) ───────────────────────── wp_send_json_success([ |
| May 12, 2026 at 7:49 am #18029332 | |
|
Christopher Amirian WPML Supporter since 07/2020
Languages: English (English ) Timezone: Asia/Yerevan (GMT+04:00) |
Hello, I checked the code and honestly it is correctly crafted. So the code is not the issue. I would test on a minimal environment to see if the issue persists or not. - IMPORTANT STEP! Create a backup of your website. Or better approach will be to test this on a copy/staging version of the website to avoid any disruption of a live website. Thanks. |
The topic ‘[Closed] WPML translation failing on frontend for custom AJAX snippets (Strings are registered & translated)’ is closed to new replies.