algoliasearch-wordpress icon indicating copy to clipboard operation
algoliasearch-wordpress copied to clipboard

WPML translated facets

Open dseidl opened this issue 6 years ago • 3 comments

What did you expect to happen?

translated text for the post_type_label facet.

What happened instead?

It always shows the main language text

How can we reproduce this behavior?

setup a simple index with wpml enabled and followed the wpml instructions here: https://community.algolia.com/wordpress/wpml.html

Technical info

  • WordPress version: 4.9.8
  • Algolia Search plugin version: 2.11.2

dseidl avatar Oct 23 '18 09:10 dseidl

I have the same Issue. The post_type_label is not being translated.

Technical WordPress 5.1 Algolia 2.11.3 WPML 4.2.4.1

damircalusic avatar Mar 08 '19 10:03 damircalusic

Also got this mind numbing issue: Wordpress : 5.1 WPML: 4.2.0 Algolia 2.11.3

sondreal avatar Mar 18 '19 13:03 sondreal

I have fixed a temporary solution fix that works great.

function shared_attributes(array $attributes, WP_Post $post){
	$lang = '';

	// WPML is active, set correct language
	if(function_exists('icl_object_id')){
		global $sitepress;
		$lang = $sitepress->get_current_language();
	}

	$attributes['wpml'] = apply_filters('wpml_post_language_details', null, $post->ID);
	
	// If post post type index the following
	if('post' == $post->post_type){
		$attributes['post_type_label'] = ($lang == 'en') ? 'Articles' : 'Artikler';
	}
	
	// If service post type index the following
	if('service' == $post->post_type){
		$attributes['post_type_label'] = ($lang == 'en') ? 'Services' : 'Tjenester';
	}
}

function posts_index_settings(array $settings){
 $settings['attributesForFaceting'][] = 'wpml.locale';
  
 return $settings; 
}

add_filter('algolia_searchable_post_shared_attributes', 'shared_attributes', 10, 2); // sharing attributes
add_filter('algolia_searchable_posts_index_settings', 'posts_index_settings'); // indexing settings

As you can see we change the post_type_label attribute and when indexing the results the languages are separated from eachother in Algolia. The English version is indexed within the english labels and the other language is indexed on its language.

This works perfectly.

damircalusic avatar Mar 18 '19 13:03 damircalusic