Skip Navigation

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 1 reply, has 0 voices.

Last updated by Marcel 1 week, 6 days ago.

Author Posts
January 29, 2025 at 11:51 am #16646063

Adhish

Background of the issue:
I am trying to ensure that documents printed from our document printing plugin are in the same language as the order was created. The plugin uses the WordPress function switch_to_locale(), passing the correct language code. When WPML is active, this approach does not work. I am using the wpml_switch_language filter to retrieve the translated product object, but dynamic text added through the XML file is not being translated. I need a solution that does not require modifying the URL. Here is the function I am using: public function switch_document_language( $lang, $template_type, $order_id ) { $this->locale = !empty( $lang ) ? $lang : get_locale(); if( function_exists( 'switch_to_locale' ) ) { switch_to_locale( $this->locale ); } if ( class_exists('\Polylang') && function_exists( 'switch_to_locale' ) ) { switch_to_locale( $this->locale ); } else { add_filter( 'locale', array( $this, 'plugin_locale' ), 10, 2 ); add_filter( 'plugin_locale', array( $this, 'plugin_locale' ), 10, 2 ); add_filter( 'theme_locale', array( $this, 'plugin_locale' ), 10, 2 ); $this->reload_plugin_text_domains(); } if( function_exists( 'switch_to_locale' ) ) { switch_to_locale( $this->locale ); } }

Symptoms:
When WPML is active, calling get_locale() still returns English, and get_product returns the original product instead of the translated version. Dynamic text added through the XML file is not being translated.

Questions:
How can I ensure that the document is printed in the same language as the order was created without modifying the URL?
Why does get_locale() return English when WPML is active?
How can I translate dynamic text added through the XML file using WPML?

January 29, 2025 at 1:00 pm #16646629

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+01:00)

Hi,

before your ticket is assigned to one of my colleagues, please allow me to walk you through some initial debugging steps. This will help speed up the support process.

To ensure documents print in the correct WPML language, use wpml_switch_language before retrieving order or product data and wpml_current_language instead of get_locale(). For dynamic text, ensure it's registered in WPML String Translation and use wpml_translate_string for proper translation.

Does this work for you?

Best Regards,
Marcel