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 kuttnerd 2 months, 2 weeks ago.

Assigned support staff: Diego Pereira.

Author Posts
January 24, 2019 at 9:36 am #3124257

kuttnerd

I am trying to: edit a post and a translation of that post, in multiple tabs.

Only tested with custom taxonomies. Don't know about categories/tags.

Steps to reproduce the error:
1. edit a post using gutenberg editor.
open a taxonomy panel, terms are loaded in correct language.
2. open a translation of that post in a second tab.
open a taxonomy panel, terms are loaded in correct language, language of the translation.
3. switch back to the first tab (without reloading any tab)
close and open a taxonomy panel. terms are loaded in the language of the translation. not in the language of the tab.

no term is selected, which is right, because wrong translation is displayed. But I'm able to select terms in wrong language.
e.g. english post with german categories.

when reloading a page, wpml sets the current language globally to the language of reloaded page. And pages in other tabs will use the new current language and query wrong entities.

Link to a page where the issue can be seen: localhost

I expected to see: taxonomy panels should load terms in the language of their page. No matter which translation got loaded in another tab.

Instead, I got: see above.

January 24, 2019 at 4:15 pm #3126052

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

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

Hello @kuttnerd, welcome to the WPML support Forum!

Could you reproduce this issue on a clean install so we can take a look? I created a test website with a clean WordPress install. You can access it through this link:
hidden link - Through this link you will be automatically logged in. Please create some dummy content to reproduce the issue.

After that, please send us clear steps on how to see/reproduce the issue on this test install. We will run some tests.

All the best,
Diego

January 25, 2019 at 9:58 am #3128369

kuttnerd

Hi, thanks for your help.

possible to reproduce this issue on your test wp install:

1. added english category: 'red' id 13.

2. added spanish translation for 'red' category: 'roja' id 14.

3. add new post in english and save: 'test post 1 en' id 9.
the categories are displayed in correct language, english.

4. in current english edit-post-screen -> sidebar -> language -> right click on 'add translation to spanish' and 'open in new tab'.
a second tab opens: the categories in this tab are displayed in correct language, spanish.

5. switch back to tab with english post (without reload).
close/open the categories panel. the categories are now displayed in wrong language, spanish. see attached image.
inspecting select elements, 'roja' has id 14.
save the post 'test post 1 es' id 11.

-----

6. reload the tab with english edit post page.
categories in this tab are now displayed in correct language again, english.

7. switch to tab with spanish post (without reload).
close/open the categories panel. the categories are now displayed in wrong language, english.

----- let's make it more complicated:

8. open a third tab to edit categories: /wp-admin/edit-tags.php?taxonomy=category
this tab opens in english and displays english categories. good.

9. switch to tab with spanish post and reload the page.
all sidebar-panel-header are in english (eg categories instead of categorias).
the categories panel displays english categories.

10. switch to tab with english post and reload the page.
all good. english headers, english categories.

11. switch to 'edit categories' tab and choose 'show content in spanish' in admin-toolbar.

12. switch to tab with english post and reload the page.
all sidebar-panel-header are in spanish (eg categorias instead of categories).
the categories panel displays spanish categories.

13. switch to tab with spanish post and reload the page. then reload page in tab with english language...
as long as the spanish 'edit categories' tab is open in a third tab, the taxonomy-panels in editor sidebar are always in spanish (categorias/etiquetas).

14. switch to 'edit categories' tab and choose 'show content in english' in admin-toolbar.
try step 13, reload both edit-post tabs, and all is english

best greetings

January 25, 2019 at 12:04 pm #3128807

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

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

Hi there,

Thanks for reproducing the issue and the clear steps. What causes this issue is:
- WPML uses a global variable to set the active language. E.g If you are editing a spanish language, it will set the active language variable as "Spanish"
- With old WP version, this is not a issue because you need to refresh the page in order to get the content related to that page (like the categories). If you refresh the page, the language variable will be re-set, so, no issues here.
- As Gutenberg relies a lot on javascript and AJAX requests, it will load the content based on the language variable without reloading the page. As you have modified this variable by opening a different language in a new tab, it will load the content based on that language.

I will check with the Dev team if this is a bug or a expected behavior and send you a feedback soon.

PS: I was not able to reproduce the issue after the step 11. If I reload the page, the language variable will be re-set, so this issue does not occurs on my end.

Kind regards,
Diego

January 26, 2019 at 6:55 am #3131142

kuttnerd

Hi Diego,
makes sense.

This could help for the moment. But not really tested.

<?php

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Switches wpml global language for rest requests from post-edit
 * Determinates new language based on referer language
 * Hooked into rest_{$taxonomy}_query
 *
 * @link https://developer.wordpress.org/reference/functions/get_terms/
 * @param array				$prepared_args 	Array of arguments to be assed to get_terms().
 * @param WP_REST_Request	$request       	The current request.
 * @return array			$prepared_args	Returns $prepared_args unchanged.
 */
function prefix_rest_switch_language( $prepared_args, $request ) {

	// get lang from referer url
	$referer = $request->get_headers()['referer'][0];
	$parts = parse_url( $referer );
	$query = array();
	parse_str( $parts['query'], $query );

	// get out if no lang
	if ( ! array_key_exists( 'lang', $query ) )
		return $prepared_args;

	// is referer editor ? switch_language
	if ( array_key_exists( 'action', $query ) && 'edit' === $query['action'] )
		do_action( 'wpml_switch_language', $query['lang'] );

	return $prepared_args;
}

/**
 * Hooks prefix_rest_switch_language function into rest_{$taxonomy}_query
 * Hooked into register_taxonomy_args
 *
 * @link https://developer.wordpress.org/reference/hooks/register_taxonomy_args/
 * @param array				$args		 	Array of arguments for registering a taxonomy.
 * @param string			$taxonomy       Taxonomy key.
 * @param array				$object_type    Array of names of object types for the taxonomy.
 * @return array			$args			Returns $args unchanged.
 */
function prefix_call_rest_switch_language_by_tax( $args, $taxonomy, $object_type ) {
	global $wp_filter;

	// hook prefix_rest_switch_language function into rest_{$taxonomy}_query
	if ( ! array_key_exists( "rest_{$taxonomy}_query", $wp_filter ) )
		add_filter( "rest_{$taxonomy}_query", 'prefix_rest_switch_language', 10, 2 );

	return $args;
}
add_filter( 'register_taxonomy_args', 'prefix_call_rest_switch_language_by_tax', 10, 3 );

?>

greetings

January 28, 2019 at 5:03 pm #3136836

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

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

Hi there,

Thanks for the update. I escalated this issue to our 2nd tier support. They will run some tests and forward to the dev team if needed. We will send a feedback soon.

Kind regards,
Diego

May 22, 2019 at 1:36 pm #3866203

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

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

Hi there,

The issue was reviewed by our development team and the conclusion was published in this errata: https://wpml.org/errata/using-the-block-editor-with-different-languages-in-different-browser-tabs/

Kind regards,
Diego

Kind regards,
Diego

https://wpml.org/errata/using-the-block-editor-with-different-languages-in-different-browser-tabs/

July 30, 2019 at 10:56 am #4307013

kuttnerd

Hi,
If anyone runs into same troubles, I wrote some lines to fix this. Works for me.

hidden link
And hook function `rest_setup_switch_lang` into `after_setup_theme` or `plugins_loaded`.