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 17 replies, has 2 voices.

Last updated by Bruno Kos 2 months, 3 weeks ago.

Assigned support staff: Bruno Kos.

Author Posts
July 19, 2019 at 7:26 am #4244551

christianS-25

How can we translate taxonomy labels programmatically (not the terms but the group labels)?

We are able to translate terms within a taxonomy programmatically, but we also need to translate the taxonomy label itself with the same code.

We found a function (icl_get_string_id), but we can't seem the find the relation between taxonomy labels and translation.

We're trying the following code, but it's returning int(0):
$taxonomy = 'brand';
$taxonomy = get_taxonomy( 'pa_' . $taxonomy );
icl_get_string_id( 'taxonomy singular name: ' . $taxonomy->labels->singular_name, 'WordPress' );

But the taxonomy singular name can be found in our database using the following query:
SELECT * FROM `wp_icl_strings` WHERE `name` LIKE '%Brand'

Any idea?

July 19, 2019 at 8:36 am #4244933

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

Thank you for contacting WPML support!

I wasn't able to find a similar example for this, so I have asked our 2nd tier about this, whether it is possible and if yes, how.

I will keep you posted!

Regards,
Bruno Kos

July 19, 2019 at 8:43 am #4244979

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

While I am waiting for an answer, can you tell me if you found this thread already?
https://wpml.org/forums/topic/re-askhow-to-get-custom-taxonomy-translation-programmatically/#post-282194

It is quite old though, so I'm not sure if it is still valid, because WPML code base has changed since.

Regards,
Bruno Kos

July 22, 2019 at 7:26 am #4254277

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

Can you also try this filter?
https://wpml.org/wpml-hook/wpml_translate_single_string/

Regards,
Bruno Kos

July 22, 2019 at 3:00 pm #4259053

christianS-25

Hi Bruno,

Thank you!

We haven't tried this one yet. Can you please tell me which $original_value and $name we should use? Is it "taxonomy singular name: [name]" or something else?

July 23, 2019 at 5:51 am #4262735

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

Does it help if I provided this example of the category taxonomy label? This is how it is rendered in the string translation table. Perhaps you could have a look at database to see how it is stored there as well.

Let me know what have you tried and if it works or not.

Regards,
Bruno Kos

July 23, 2019 at 9:14 am #4264455

christianS-25

Alright, so we should query based on 'Value' with the context 'taxonomy singular name'. We'll test this in our site and reply!

July 23, 2019 at 10:01 am #4264929

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

Ok, let me know how it goes and if nothing works, please provide examples of the code you tried so that I could paste this information to our 2nd tier support.

Regards,
Bruno Kos

July 24, 2019 at 8:18 am #4271439

christianS-25

First thing we noticed was the "wpml_translate_single_string" filter not returning an object but a string. Furthermore, when adding a language code as a parameter it still returns the default (English) translation, while the language code and translation both exist.

We have attached a screenshot with added colors, it should visualize what we've tried. If not, let us know!

If the image quality is bad, open the image in a new tab and zoom in.

July 24, 2019 at 8:33 am #4271557

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

Checking these results with our 2nd tier!

Regards,
Bruno Kos

July 25, 2019 at 8:10 am #4280603

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

Here is a reply form our 2nd tier:

""""""""""""
The name is not correct that they have passed in the filter. You can see in the screenshot they have missed the prefix “taxonomy_singular_name:”
"""""""""""

Regards,
Bruno Kos

July 29, 2019 at 7:45 am #4297423

christianS-25

Hi Bruno,

Thanks for your reply, appreciated!

Adding

"taxonomy singular name: "

in front of the name variable within the filter works, I now get the proper translation. However, it solely returns a string instead of an object. Also, when I use a non-existent translation on purpose, it doesn't return null/false but returns

"taxonomy singular name: [input]"

instead. Here's two examples.

1) Existing Dutch (nl) translation:
// Filter

apply_filters( 'wpml_translate_single_string', 'taxonomy singular name: Electric Connection Load (kW)', 'WordPress', 'taxonomy singular name: Electric Connection Load (kW)', 'nl' );

// Result (var_dump)

string(37) "Elektrische verbindingsbelasting (kW)"

2) Non-existent Dutch (nl) translation:
// Filter

apply_filters( 'wpml_translate_single_string', 'taxonomy singular name: ABC1234', 'WordPress', 'taxonomy singular name: ABC1234', 'nl' );

// Result (var_dump)

string(31) "taxonomy singular name: ABC1234"

I could check if the result has "taxonomy singular name: " in its string to know whether a translation exists or not. But is this really the only and proper way to go forward?

July 29, 2019 at 9:49 am #4298447

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

This behavior is expected and this is how it should work, according to this documentation:
https://wpml.org/wpml-hook/wpml_translate_single_string/

"Retrieves an individual (as opposed to a string that is part of a package**) text string translation. The filter looks for a string with matching $domain and $name. If it finds it, it looks for a translation in the current language or the language you specify. If a translation exists, it will return it. Otherwise, it will return the original string."

Regards,
Bruno Kos

July 30, 2019 at 3:09 pm #4308939

christianS-25

Thank Bruno!

We are able to retrieve the translations. However, how do we programmatically add a new translation to a registered string translation. We found this action: wpml_register_single_string, but we believe it's used to register a new string instead of a string translation (with a language parameter).

Any idea?

July 31, 2019 at 5:37 am #4311567

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

However, how do we programmatically add a new translation to a registered string translation.

I asked this also, I will keep you posted! I wasn't able to find a filter for this, though.

Regards,
Bruno Kos