Skip Navigation

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

Problem:
The client is working on a site under development and has successfully registered strings using the

wpml_register_string

hook. They need to insert translations for these strings programmatically as part of a migration involving hundreds of posts and multiple translations. However, the API documentation lacks information on how to do this.
Solution:
We recommend translating the xliff file that WPML uses for segmenting values and then re-inserting it. This approach is robust for custom coding scenarios. Alternatively, you can use the native editor to insert translated content and link it to the original content using our hooks. The wpml_set_element_language_details hook might be particularly useful in this case.
For more detailed information, please refer to our extensive developer documentation:

If this solution does not apply to your case, or if it seems outdated, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. Should you need further assistance, please open a new support ticket at WPML Support Forum.

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

Last updated by Itamar 10 months, 4 weeks ago.

Assisted by: Itamar.

Author Posts
July 8, 2024 at 12:47 pm #15913255

Ionut

Background of the issue:
I am working on a site under development and have managed to register strings with the `wpml_register_string` hook. I need to insert translations for these strings as part of a migration involving hundreds of posts and multiple translations.

Symptoms:
The API documentation is missing information on how to programmatically insert translations for registered strings.

Questions:
Is there any way of programmatically inserting a translation for a given string other than reversing the AJAX call on `WPML_TM_Editor_Job_Save`?

July 8, 2024 at 2:11 pm #15913840

Itamar
WPML Supporter since 02/2016

Languages: English (English )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

I'm waiting for a reply from our second-tier supporters. I'll update you here once I have it.

I appreciate your patience.
Itamar.

July 8, 2024 at 4:07 pm #15914521

Ionut

Cool, I've also opened a SO thread, hopefully i've explained the issue better there

https://stackoverflow.com/questions/78721456/how-to-programatically-insert-wpml-translation-for-a-custom-string

July 9, 2024 at 9:55 am #15919628

Ionut

I've managed to dig into the code and found this:

You register the string with the following code:

$package = [
'kind' => "The Translation Package ({$postID})",
'kind_slug' => "the-translation-package-{$postID}",
'post_id' => $postID,
'title' => 'My Package Title',
'name' => 'my-package-name'
];

$stringValue = 'original string; can be anything here';

do_action(
'wpml_register_string',
$stringValue,
'my-string-name',
$package,
'My String Title',
'LINE' // or AREA if it's a multi-line
);

Then you can add the translation as follows:

$string_id = icl_get_string_id($stringValue, "{$package['kind_slug']}-{$package['name']}", 'my-string-name');
icl_add_string_translation($string_id, 'es', 'translation value', ICL_TM_COMPLETE);

This do work, so yay.

However, there are couple of more issues:

1. Although the string translation IS inserted, it doesn't appear to mark the post as translated
2. Although the translation is marked as `ICL_TM_COMPLETE` the UI doesn't seem to reflect this state.

July 9, 2024 at 11:16 am #15920235

Itamar
WPML Supporter since 02/2016

Languages: English (English )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

This was the reply from our second-tier supporter before your last reply.

This is a bit hard question here.

As you know, WPML segments the values in a xliff file used for translation. The best way would be to translate that xliff file and re-insert it. That is a rather robust solution for custom coding.

Apart from that, it is possible to use only the native editor, insert the translated content, and link it to the original via our hooks. Probably https://wpml.org/wpml-hook/wpml_set_element_language_details/ would help.

We have extensive documentation for developers. Please check the main page here.

https://wpml.org/documentation/support/

This is the link to WPML's hooks list.

https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Regards,
Itamar.