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.

This topic contains 2 replies, has 2 voices.

Last updated by Diego Pereira 1 year ago.

Assigned support staff: Diego Pereira.

Author Posts
September 27, 2018 at 3:18 pm #2771018

bryceD

I'm trying to programmatically add translations to categories when the 'create_category' action hook is triggered by WordPress. After a post is created by a user, a category is added for the language that post is written in via PHP. It would be nice if this action could also trigger translation of the category name, referencing a .json I created as a look up for the translations. Here is the code I am trying to implement in functions.php:

<?php

add_action('create_category', 'translate_category', 10, 2) {
function translate_category( $category_id, $tt_id )
if ( function_exists( 'icl_get_string_id' ) && function_exists( 'icl_add_string_translation' ) ) {

$category = get_term_by ('id', $category_id, 'category');

$name = $category -> name;
$slug = $category -> slug;

$string_id = icl_get_string_id( $name, 'category-domain');

// Status = 10 (translated)
// Would loop through this for each $language in $languages to add $translation
icl_add_string_translation( $string_id, $language, $translation, 10)
}
}

?>

If I knew the text domain to use in place of CATEGORY-DOMAIN, this would work. However, I don't know if categories have a text domain. If they do not, can you offer a work around? Thank you!

September 28, 2018 at 3:51 pm #2774363

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello @bryced, welcome to the WPML support Forum!

Here is our list of available filters and hooks:
- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

I am sending the code to my team to take a look. I'll send you feedback soon.

All the best,
Diego

October 1, 2018 at 12:04 pm #2778014

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi there,

I got the feedback from my team. Unfortunately the categories does not contain a string, so they also do not have a textdomain.

We found this code snippet with similar function. Please take a look, it may be useful: https://wpml.org/forums/topic/504-gateway-time-out-translating-category/#post-129302

As this is a customization job, unfortunately it is not part of our support policy (https://wpml.org/purchase/support-policy/). In this case, I recommend to take a look at our list of experienced contractors available here: https://wpml.org/contractors/

Here is the links of our coding API and hooks list:
- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
- https://wpml.org/documentation/support/wpml-coding-api/

Kind regards,
Diego