Skip Navigation

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 1 voice.

Last updated by Nikhil Vishwanath 11 hours, 26 minutes ago.

Author Posts
January 24, 2025 at 7:17 am #16630403

Nikhil Vishwanath

Background of the issue:
I am working on a site under development and trying to translate dynamic text content using WPML string translation. We have a text like this - "The next section "Impermanence" will become available starting February 24, 2025." In this text, the section title and date are dynamically fetched. We are using variables for it and want to translate or have the ability to move around these dynamic variables; as sometimes the date comes first in some languages. Example of what we want our text translation to look like: __('The next section '.$section_title. ' will become available starting' .$section_date, 'my-theme');

Symptoms:
Currently, we did not find any documentation for translating dynamic strings.

Questions:
How can we translate dynamic text content using WPML string translations?
Is there any documentation available for translating dynamic strings with WPML?

January 24, 2025 at 9:04 am #16630688

Dražen
Supporter

Languages: English (English )

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

Hello,

Thanks for contacting us.

You can wrap each text as a string and make it translatable via gettext and between them show a variable, or you can include a variable inside translatable text.

Let’s say you have a string like the following:

echo 'Welcome $name.'

In this case, $name is the variable and you shouldn’t include it in the translation. Instead, you should use a placeholder in place of the variable. Then, you can use the printf() or sprintf() function to output the translated string with the variable value inserted.

printf(
    /* translators: %s: Name of a person */
    __( 'Welcome %s.', 'your-text-domain' ),
    $name
);

Now, the translator can translate “Welcome %s” as an entire phrase. The PHP printf() function inserts the name of the person back into the string.

Regards,
Drazen

January 24, 2025 at 11:31 am #16631129

Nikhil Vishwanath

Hi,

Thanks for your response.

We are using the following to translate our text and return it as a response to our ajax call -

<php

$translation_array = array( 'apply_translation' => __( 'The next section '.$next_lesson_title .' will become available starting '.$lesson_access_date, 'astra-child' ) );
wp_localize_script( 'wdm-custom-theme-script', 'translate_msg', $translation_array );
//the translation array is returned as a response
?>

Can you please let me know how I can integrate or make changes to our code using your example?

Thanks,
Nikhil