Skip to content

Instantly share code, notes, and snippets.

@umdevelopera
Last active June 20, 2024 18:18
Show Gist options
  • Save umdevelopera/e37c38e033ead2620557bfa44cb7155d to your computer and use it in GitHub Desktop.
Save umdevelopera/e37c38e033ead2620557bfa44cb7155d to your computer and use it in GitHub Desktop.
Example. A code used to translate labels in the Ultimate Member member directory. Uses the Polylang or WPML language switcher.
<?php
/**
* Translate labels with Polylang or WPML.
*/
function um_get_translated_labels() {
// EDIT this array of translated labels.
return array(
"first_name" => array(
'en' => 'First Name',
'es' => 'Nombre de pila',
'fr' => 'Prénom',
),
"last_name" => array(
'en' => 'Last Name',
'es' => 'Apellido',
'fr' => 'Nom de famille',
),
"role_select" => array(
'en' => 'Account Type',
'es' => 'Tipo de cuenta',
'fr' => 'Type de compte',
),
);
}
function um_translate_field_label( $label, $key ) {
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
$lang = apply_filters( 'wpml_current_language', null );
} elseif( function_exists( 'pll_current_language' ) ) {
$lang = pll_current_language();
} else {
$lang = substr( get_locale(), 0, 2 );
}
$labels = um_get_translated_labels();
if ( array_key_exists( $key, $labels ) && array_key_exists( $lang, $labels[ $key ] ) ) {
$label = $labels[ $key ][ $lang ];
}
return $label;
}
function um_translate_filter_label( $attrs, $key ) {
$attrs['label'] = um_translate_field_label( $attrs['label'], $key );
return $attrs;
}
add_filter( 'um_change_field_label', 'um_translate_field_label', 10, 2 );
add_filter( 'um_search_fields', 'um_translate_filter_label', 10, 2 );
@umdevelopera
Copy link
Author

umdevelopera commented Jun 12, 2024

There is no easy way to translate labels in the member directory. You can use the um_change_field_label hook to translate field labels and the um_search_fields hook to translate filter labels. See code example above.

Extend an array in the um_get_translated_labels function to add more fields and languages.

This code works with the Polylang or WPML multilingual plugin. You have to use another function to get the $lang variable (current language) if you use another multilingual plugin.

Directory example
translated filter fields example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment