Hi @leuno
You need to create a custom field for each language and apply it to your products, so you can use in each filter set translation the needed custom fields like catgory_en, category_fr, and so on.
But if you want to use string translation you can use the ”wpc_terms_before_display” hook – https://filtereverything.pro/resources/hooks/#terms-before-display-hook with WPML function that will replace the values for the needed translation.
Best Regards – Victor
Thread Starter
leuno
(@leuno)
“You need to create a custom field for each language and apply it to your products, so you can use in each filter set translation the needed custom fields like catgory_en, category_fr, and so on.”
I need to translate and apply to all my products?! I have 250 products this is a nightmare 😐
if I use wpc_terms_before_display hook how I can work with seo? I can change also the slug?
-
This reply was modified 2 years, 4 months ago by
leuno.
Hi @leuno
You can create translations for the SEO Rules as well, the same as for Filter Sets.
So it will display different SEO Data depending on the language.
Best Regards – Victor
Thread Starter
leuno
(@leuno)
I try with this function:
add_filter('wpc_terms_before_display', 'translate_acf_fields', 10, 2);
function translate_acf_fields($terms, $filter) {
$fields_to_translate = array('tipologia', 'regione', 'cucina', 'origine', 'stagioni', 'informazioni', 'ingredienti', 'e_ora_di', 'Formaggio');
if (function_exists('get_field_object') && in_array($filter['e_name'], $fields_to_translate)) {
$newTerms = [];
foreach ($terms as $k => $term) {
$field_value = $term->name;
if ($field_value !== false) {
if (function_exists('icl_translate')) {
$current_language = apply_filters('wpml_current_language', NULL);
$field_value_translation = icl_translate('acf', $filter['e_name'], $field_value, $current_language);
$term->name = $field_value_translation;
}
}
$newTerms[$k] = $term;
}
return $newTerms;
}
return $terms;
}
But filter is always in the first language (it), I hope you can help me.
THX
Hi @leuno
In general we do not provide or debug the custom codes.
But we can provide this simple example which should send you in the right direction, just try with one custom field you need to specify its meta key instead of the meta_key_goes_here :
add_filter('wpc_terms_before_display', 'wpc_modify_term_names_based_on_language', 10, 2);
function wpc_modify_term_names_based_on_language($terms, $filter){
// Check if WPML plugin is active
if( function_exists('icl_object_id') ) {
// Get the current language
$current_language = apply_filters( 'wpml_current_language', NULL );
// Check if the filter is for the desired custom field
if ($filter['e_name'] === 'meta_key_goes_here') {
// Loop through each term
foreach ($terms as $k => $term) {
// Get the translated term name based on the current language
$translated_name = apply_filters( 'wpml_translate_single_string', $term->name, 'taxonomy', 'term_' . $term->term_id . '_name', $current_language );
// Add the translated term name and language information
$terms[$k]->name = $translated_name . ' (' . strtoupper($current_language) . ')'; // Append language code to translated term names
}
}
}
return $terms;
}
Afther you inserted it in the functions.php file and save it. Go to the filter set and hit update. Then check the filter with this meta key that you specified on different languages. It should add language code to all terms depending which language you choose.
Best Regards – Victor
Thread Starter
leuno
(@leuno)
ok I modify your script and now I have the correct language on the filter,
now I need to change the slug, I need to translate Global filter prefixes and filter slug in second language, for example:
first language:menu/ingredienti-pecorino/
second language:menu/ingredienti-pecorino-cheese (no page found)
In second language I need /ingredients-pecorino-cheese
How to make this?
Hi @leuno
This is something that we do not recommend. As there is no option to translate the slugs for the same filter in different languages.
It would require quite a lot of coding and it was never tested like that. Unfortunately, we cannot help here. But you can experiment with it.
Best Regards – Victor