Skip Navigation

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

Problem:
The client wants to add a translation inside a variable

Solution:
- First wrap the string in the gettext function like this: https://wpml.org/forums/topic/adding-wpml-translation-inside-a-variable/#post-13172685
- After that, please scan the theme in which you update the code from WPML >> Theme and plugins localization page.
- Go to WPML >> String Translation page and search for that string and add the translation.

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/gettext/
https://wpml.org/documentation/support/translating-the-theme-you-created/

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

Last updated by Ferran 1 year, 10 months ago.

Assisted by: Noman.

Author Posts
March 4, 2023 at 9:14 am #13169193

Ferran

Tell us what you are trying to do?

just trying (if possible) to add a translation inside a variable like so:

if ($readingtime == 1) {
$timer = "<?php echo __(' minute reading', 'my-theme'); ?>";
} else {
$timer = "<?php echo __(' minutes reading', 'my-theme'); ?>";
}

is that doable?

March 5, 2023 at 9:46 am #13172685

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting WPML Support. You may store the translated string in a variable and then echo that variable. So, your code will look like this:

if ( $readingtime == 1 ) {
	$timer = __(' minute reading', 'my-theme');
} else {
	$timer = __(' minutes reading', 'my-theme');
}

And later you can echo it by using the below code:

echo $timer;

- After that, please scan the theme in which you update the code from WPML >> Theme and plugins localization page.

- Go to WPML >> String Translation page and search for that string and add the translation.

Here are docs for more details:
https://developer.wordpress.org/reference/hooks/gettext/
https://wpml.org/documentation/support/translating-the-theme-you-created/

Thank you