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.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 - -
- - - - - - -

Supporter timezone: Africa/Cairo (GMT+02:00)

Tagged: 

This topic contains 3 replies, has 1 voice.

Last updated by Osama Mersal 2 days, 17 hours ago.

Assisted by: Osama Mersal.

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

January 27, 2025 at 9:50 am #16636747

Osama Mersal
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+02:00)

Hi Nikhil,

I'm Osama from WPML forums support. I'll be glad to help you today.

First of all, sorry for the late reply; it was due to a higher workload.

Kindly note that WPML has a limitation in translating JS. Please check this page.

Please make sure you are following our guides and WordPress's instructions on how to localize scripts:

https://developer.wordpress.org/reference/functions/wp_localize_script/

https://wpml.org/documentation/support/enabling-text-translation-for-themes-not-compatible-with-wpml/

Also, please note that we are currently not able to provide support for custom work within this forum, which will require translating something from inside Javascript.

If you need further assistance, I suggest contacting one of our third-party contractors, who specialize in custom work with WPML (https://wpml.org/contractors/), or a developer of your choice.

I hope that you will find this answer insightful.

Best regards,
Osama