Skip Navigation

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

Problem:
The client needs to get the translated name of a product attribute/taxonomy using PHP.

Solution:
Taxonomies such as in your example have a singular name ("Materiaal"), and a general name ("Product Materiaal").

You can get the translation of either the singular name or the general name, starting with the name in the default language, using the WPML API, specifically the wpml_translate_single_string hook: https://wpml.org/wpml-hook/wpml_translate_single_string/

That takes 4 arguments:

$original_value : in your example, "Materiaal"
$domain : for taxonomies, is "WordPress"
$name : for the singular name of Materiaal it would be "taxonomy singular name: Materiaal"; for the general name of Product Materiaal it would be "taxonomy general name: Product Materiaal"
$language_code : in your example it would be "en" or "de"

Here's a simple function you can define and then call as required:

/**
 * Function that takes the original name of a taxonomy and returns its translation
 * 
 * @param $original             string : the default language taxonomy name to be translated
 * @param $singular_or_general  string : "singular" or "general" name being translated
 * @param $language             string : language code to translate to
 */
function wpmlsupp_translate_taxonomy_name( string $original, string $singular_or_general, string $language ) {

    return apply_filters( 'wpml_translate_single_string', $original, 'WordPress', "taxonomy $singular_or_general name: $original", $language );
}

So to translate "Product Materiaal" to German you would use:

$translated = wpmlsupp_translate_taxonomy_name( 'Product Materiaal', 'general', 'de' );

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

Last updated by Nigel 1 year, 7 months ago.

Assisted by: Nigel.

Author Posts
October 17, 2023 at 9:30 am #14592085

artjanv

We have translated product attributes/taxonomies and want to get the translated name with PHP! How can we archieve this? Terms are no problem and fixed but just the taxonomy name itself.

See our screenshot where we want to get the EN name "Material". The taxonomy is pa_materiaal

Scherm­afbeelding 2023-10-17 om 09.37.15.png
October 19, 2023 at 11:22 am #14614317

Nigel
WPML Supporter since 02/2016

Timezone: Europe/Madrid (GMT+02:00)

Sorry for the delayed response, we had a spike in support load.

Taxonomies such as in your example have a singular name ("Materiaal"), and a general name ("Product Materiaal").

You can get the translation of either the singular name or the general name, starting with the name in the default language, using the WPML API, specifically the wpml_translate_single_string hook: https://wpml.org/wpml-hook/wpml_translate_single_string/

That takes 4 arguments:

$original_value : in your example, "Materiaal"
$domain : for taxonomies, is "WordPress"
$name : for the singular name of Materiaal it would be "taxonomy singular name: Materiaal"; for the general name of Product Materiaal it would be "taxonomy general name: Product Materiaal"
$language_code : in your example it would be "en" or "de"

Here's a simple function you can define and then call as required:

/**
 * Function that takes the original name of a taxonomy and returns its translation
 * 
 * @param $original             string : the default language taxonomy name to be translated
 * @param $singular_or_general  string : "singular" or "general" name being translated
 * @param $language             string : language code to translate to
 */
function wpmlsupp_translate_taxonomy_name( string $original, string $singular_or_general, string $language ) {

    return apply_filters( 'wpml_translate_single_string', $original, 'WordPress', "taxonomy $singular_or_general name: $original", $language );
}

So to translate "Product Materiaal" to German you would use:

$translated = wpmlsupp_translate_taxonomy_name( 'Product Materiaal', 'general', 'de' );
October 24, 2023 at 6:37 am #14642493

artjanv

This works great. Thank you for you example code!