Skip Navigation

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

Problem: String Translation not working when switch_to_blog() is sued.

Solution: Our development team is working on this issue and a fix will be included in the next releases. At the meantime, use the solution as provided here: https://wpml.org/forums/topic/programmatically-get-translated-admin-texts-on-a-network-install/#post-2187270

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

Last updated by davidA-29 2 years, 2 months ago.

Assigned support staff: Shekhar Bhandari.

Author Posts
May 15, 2018 at 2:06 am #2128462

davidA-29

I am trying to:
Programmatically get translated admin texts on a network install using switch_to_blog() and wpml_switch_language.

I expected to see:
Translated admin texts in the language specified in wpml_switch_language filter.

Approach:

I have added the wpml-config.xml file with my custom options to the theme root. This works fine - the options are available via string translation and appear as domain admin_texts_mytheme. These have been translated.

switch_to_blog( $subsite_id );

$locale = 'de';//hardcoded locale I am trying to get translated options for
do_action( 'wpml_switch_language', $locale );

//check language has been switched
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
error_log(print_R('$my_current_lang:'.$my_current_lang,TRUE)); //outputs 'de'

//what I use for non-WPML installs to return all my custom options
//this returns the options in default language, not the one I've switched to.
$theme_options = get_option('mytheme');



Looking in string translations, I can see the options are there (and translated) using domain 'admin_texts_mytheme' and, for example, name '[mytheme]email_text_barcodenumber_singular'.

How can I get the translated value of all options, or, if need be, each option individually?

May 16, 2018 at 11:23 am #2155786

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Thank you for contacting WPML support. I'd be happy to assist you further on this issue.

Currently receiving the strings translation value from the theme root is not supported and it's only possible via custom programming work which you are doing.

I think you are in the right path as switching to blog and retrieving the strings seems to be a way to achieve such things, we haven't tested such things till yet and since it's a custom programming work, it's beyond the scope of our support.

At this point, I would suggest you consider contacting one of our certified partners from this link: http://wpml.org/documentation/support/wpml-contractors/

Before doing so please have a look also here:
http://wpml.org/documentation/support/wpml-contractors/guidelines-for-people-looking-for-consulting-help/

You will get the custom assistance you need to get on with your project.

Thanks

May 16, 2018 at 10:17 pm #2165340

davidA-29

OK, so I am looking into various WPML functions. If I have to go the route of looking up string translations individually, so be it. Can you confirm this should bring back a translated theme option?

switch_to_blog( $subsite_id );
 
$locale = 'de';//hardcoded locale
do_action( 'wpml_switch_language', $locale );

$text = apply_filters( 'wpml_translate_single_string', 'Original Language String here', 'the_domain' ,'the_name', $locale );

The above does not output the German translation - it outputs the default language. Is this a bug?

May 16, 2018 at 10:40 pm #2165469

davidA-29

Investigating further, I see wpml_translate_single_string calls icl_translate().

icl_translate has a condition that excludes network sites when switch_to_blog is active:

if ( ! ( is_multisite() && ms_is_switched() ) || $GLOBALS['blog_id'] === end( $GLOBALS['_wp_switched_stack'] ) ) {
		/** @var WPML_String_Translation $WPML_String_Translation */
		global $WPML_String_Translation;
		$filter = $WPML_String_Translation->get_string_filter( $target_lang ? $target_lang : $WPML_String_Translation->get_current_string_language( $name ) );
		$value  = $filter ? $filter->translate_by_name_and_context( $value, $name, $context, $has_translation ) : $value;
	}

:0

So that never runs and my strings aren't translated. If I remove that condition, the code runs fine and returns my translated value. Which begs the questions - why is this condition present and why isn't it documented?

May 17, 2018 at 8:25 am #2169974

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

I can see that you are using an old version of string translation, could you please update the string translation plugin to the latest version and let me know if that helps.

NOTE:

+ Make a backup (both files and database) for your site before updating/proceeding anything (Always a good practice!). You can use a plugin for this if you like. I often use the Duplicator plugin for this purpose. See: http://wordpress.org/plugins/duplicator/

I checked the code you mentioned and it's changed in the latest version so it might help you.

Look forward to your reply.

Thanks

May 17, 2018 at 10:43 pm #2184139

davidA-29

Hi, the latest version of the string translation plugin's icl_translate function still contains

$is_requested_blog = ! ( is_multisite() && ms_is_switched() ) || $GLOBALS['blog_id'] === end( $GLOBALS['_wp_switched_stack'] );

It is specifically checking that switch_to_blog isn't being used before retrieving the translation. By hardcoding

$is_requested_blog = true;

it works fine and retrieves the translation.

May 18, 2018 at 7:25 am #2187270

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

This is to notify you that our development team is working on this issue and a fix will be include in the next releases. At the meantime, could you please replace the

$is_requested_blog = ! ( is_multisite() && ms_is_switched() ) || $GLOBALS['blog_id'] === end( $GLOBALS['_wp_switched_stack'] );

with

$is_requested_blog = ! ( is_multisite() && ms_is_switched() ) || (int)$GLOBALS['blog_id'] === (int)end( $GLOBALS['_wp_switched_stack'] );

and let me know if that helps.

Thanks

May 23, 2018 at 2:53 am #2212525

davidA-29

Thanks, using

$is_requested_blog = ! ( is_multisite() && ms_is_switched() ) || (int)$GLOBALS['blog_id'] === (int)end( $GLOBALS['_wp_switched_stack'] );

does work. Hopefully, this will be patched in the plugin soon.

Thanks for your help.