Skip Navigation

[Résolu] How to translate "strings" in custom code?

This support ticket is created Il y a 3 années et 6 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: America/Jamaica (GMT-05:00)

Marqué : 

This topic contains 13 réponses, has 2 voix.

Last updated by fred-r.M Il y a 3 années et 6 mois.

Assisted by: Shane.

Auteur
Publications
#1784971

Tell us what you are trying to do?

How we can translate strings in custom code?

Is there any documentation that you are following?

https://toolset.com/documentation/user-guides/legacy-documentation/translating-layouts-and-views/wpml-string-shortcode/

Is there a similar example that we can see?

Our custom code is like this, but we are not sure what the best approach is, to translate it:

function display_tourdetails() {

global $post;
$str = '';

$tour_duration_category = get_post_meta($post->ID,'wpcf-tour-duration-category',true);
//$tour_duration = get_term_meta($post->ID,'wpcf-tour-duration',true);
$atour_duration = wp_get_post_terms( $post->ID, 'tour-duration', array( 'fields' => 'names' ) );
$tour_duration = $atour_duration[0];
//$tour_specialty = get_term_meta($post->ID,'wpcf-tour-specialty',true);
$atour_specialty = wp_get_post_terms( $post->ID, 'tour-specialty', array( 'fields' => 'names' ) );
$tour_specialty = $atour_specialty[0];
//$tour_difficulty = get_term_meta($post->ID,'wpcf-tour-difficulty',true);
$atour_difficulty = wp_get_post_terms( $post->ID, 'tour-difficulty', array( 'fields' => 'names' ) );
$tour_difficulty = $atour_difficulty[0];
$total_tour_distance_km = get_post_meta($post->ID,'wpcf-total-tour-distance-km',true);
$total_tour_distance_mi = get_post_meta($post->ID,'wpcf-total-tour-distance-mi',true);
$average_distance_per_day = get_post_meta($post->ID,'wpcf-average-distance-per-day',true);
$average_climbing_per_day = get_post_meta($post->ID,'wpcf-average-climbing-per-day',true);
$price = number_format(get_post_meta($post->ID,'wpcf-price',true),0);

$str .= '<div class="facts">';
if(!empty($tour_duration_category)){
if (ICL_LANGUAGE_CODE == 'en') {
$str .= '<div class="style touring">Tour Cat:<br /> '.$tour_duration_category.'</div>';
}
if (ICL_LANGUAGE_CODE == 'de') {
$str .= '<div class="style touring">Tour Kat:<br /> '.$tour_duration_category.'</div>';
}
}
if(!empty($tour_duration)){
$str .= '<div class="duration">[wpml-string context='Tour Details' name='duration']Duration:[/wpml-string]Duration:<br /> '.$tour_duration.'</div>';
}
if(!empty($tour_difficulty)){
$str .= '<div class="destination">Difficulty:<br /> '.$tour_difficulty.'</div>';
}
if(!empty($tour_specialty)){
$str .= '<div class="price">Specialty:<br /> '.$tour_specialty.'</div>';
}
if(!empty($total_tour_distance_km)){
$str .= '<div class="style touring">Total Riding Distance:<br /> '.$total_tour_distance_km.' km / '.$total_tour_distance_mi.' miles</div>';
}
if(!empty($average_distance_per_day)){
$str .= '<div class="duration">Ride Distance p/day:<br /> '.$average_distance_per_day.'</div>';
}
if(!empty($average_climbing_per_day)){
$str .= '<div class="destination">Total Ascent:<br /> '.$average_climbing_per_day.'</div>';
}
if(!empty($price)){
if (ICL_LANGUAGE_CODE == 'en') {
$str .= '<div class="price">Price:<br /> '.$price.'</div>';
}
if (ICL_LANGUAGE_CODE == 'de') {
$str .= '<div class="price">Preis:<br /> '.$price.'</div>';
}
}
$str .= '</div>';
return $str;
}
add_shortcode( 'show_tourfacts', 'display_tourdetails' );

Different approaches possible:

We think this would be a way to translate:

if(!empty($tour_duration_category)){
if (ICL_LANGUAGE_CODE == 'en') {
$str .= '<div class="style touring">Tour Cat:<br /> '.$tour_duration_category.'</div>';
}
if (ICL_LANGUAGE_CODE == 'de') {
$str .= '<div class="style touring">Tour Kat:<br /> '.$tour_duration_category.'</div>';
}
}

or by using

if(!empty($tour_duration)){
$str .= '<div class="duration">[wpml-string context='Tour Details' name='duration']Duration:[/wpml-string]Duration:<br /> '.$tour_duration.'</div>';
}

For the second version, as we understand, it should be showing then in wpml string translation under the domain Tour Details. But this isn't showing.

#1786267

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Fred,

Thank you for getting in touch.

Actually for strings inside code it would be best to register these strings with a text domain.

https://developer.wordpress.org/themes/functionality/internationalization/#add-text-domain-to-strings

For e.g

_e("Tour Kat:", "custom-code");

This string once you've re-scanned your theme/plugin with WPML you will now see Tour Kat: available for translation using our String Translation plugin from WPML.

Here is the documentation on our String Translations plugin.
https://wpml.org/documentation/getting-started-guide/string-translation/

Please let me know if this helps.
Thanks,
Shane

#1786925

Dear Shane

Thank You. Do You know where I find the different between _e and _n ? I assume one is for a number format _n and _e for a string.

I have done now the way like this:

f(!empty($tour_duration_category)){
if (ICL_LANGUAGE_CODE == 'en') {
$str .= '<div class="style touring">Tour Cat:<br /> '.$tour_duration_category.'</div>';
}
if (ICL_LANGUAGE_CODE == 'de') {
$str .= '<div class="style touring">Tour Kat:<br /> '.$tour_duration_category.'</div>';
}
}

As this was the fastest solution for now. And okay if having only 2 languages. But will change it to Your solution, what makes a better solution.

Regards,

#1786949

Dear Shane

I tried to do like this:

from

if(!empty($tour_duration_category)){
if (ICL_LANGUAGE_CODE == 'en') {
$str .= '<div class="style touring">Tour Cat:<br /> '.$tour_duration_category.'</div>';
}
if (ICL_LANGUAGE_CODE == 'de') {
$str .= '<div class="style touring">Tour Kat:<br /> '.$tour_duration_category.'</div>';
}
}

to

if(!empty($tour_duration_category)){
  $str .= '<div class="style touring">'._e("Tour Cat:", "custom-code").'<br /> '.$tour_duration_category.'</div>';
}
#1787775

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Fred,

Actually no _e and _n are very similar however the _n allows for plurals.

Given that you've added it like the follow:

if(!empty($tour_duration_category)){
  $str .= '<div class="style touring">'._e("Tour Cat:", "custom-code").'<br /> '.$tour_duration_category.'</div>';
}

You will need to perform a scan on your theme using the string translations plugin if the custom code was placed in the functions.php file.

The text should now appear under the custom-code text domain.

Thanks,
Shane

#1788237

Dear Shane

I tried this, but it not showing the default language string on the front end.

I expect to see Tour Cat:

Regards

#1789455

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Fred,

Would you mind allowing me to have admin access to the site so that I can check on this for you ?

Also let me know the page where the string is being displayed as well as where you placed the custom code.

Thanks,
Shane

#1793151

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2020-09-28 at 9.47.20 AM.png
Screenshot 2020-09-28 at 9.45.52 AM.png

Hi Fred,

The localization code that I gave actually works.

Remember you will still need to translate it after it has been added. When you add the gettext calls to the strings you will need to go to WPML -> Theme and Plugin localization and select your child theme and scan it for strings.

Once you have done this you will see you theme will have strings that need translating. In this case you can click on the yellow number and it will take you to the string translations page for you to translate this string.

See Screenshots.

Please re-add the gettext calls that i've outlined in the link below to prepare your strings for translation.
https://toolset.com/forums/topic/how-to-translate-strings-in-custom-code/#post-1786267

Thanks,
Shane

#1793573

Dear Shane

The translation part is working... What I am mention is, that it not showing in the Front End, either the english version nor the german version.

On this link: hidden link

I expect to see the String I used for the default language (in my case "Tour Category").

hidden link

Instead I get:

<div class="style touring">
<br> 
Multiple Days Tour
</div>
#1794737

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Fred,

On your homepage i'm actually not seeing where you are calling the shortcode itself.

If you're able to provide me with a screenshot where you've added the shortcode to the page itself, please let me know and I can start investigating.

Thanks,
Shane

#1795037

Dear Shane

I wrote this in the private message:

I am "calling" and using the short code under Impreza -> Grid Layouts -> Tour Slider, in the second custom html. Any other short code is working in there. Also the short code using my way is working there.

Only when I am having your version, it's not showing the default ("english") language.

Regards,

#1796091

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2020-09-30 at 2.54.34 PM.png
Screenshot 2020-09-30 at 2.55.04 PM.png
Screenshot 2020-09-30 at 2.55.12 PM.png

Hi Fred,

Not sure why you say its not working but I checked on the frontend and both the original English is displayed as well as the german when you're viewing the translation.

Take a look at the Tour Category in the screenshots.

From what I can see, the texts are being translated fine unless i'm missing something. Notice Tour Category is using the gettext() funciton.

Thanks,
Shane

#1796271

Dear Shane

The text is not on the right place. The text should be same all other text "inside" the div class="facts":

I have with this version:

"Tour Category:Duration:"
<div class="facts">
      <div class="style touring"><strong></strong><br> Multiple Days Tour</div>
      <div class="duration"><strong></strong><br> 11 Days / 10 Nights / 8 Stages</div>
      <div class="destination"><strong>Difficulty:</strong><br> Easy</div>
      <div class="price"><strong>Specialty:</strong><br> Coast to Coast</div>
      <div class="style touring"><strong>Total Riding Distance:</strong><br> 823 km / 511 miles</div>
      <div class="duration"><strong>Ride Distance p/day:</strong><br> 50 - 146 km |<br>31 - 90 miles</div>
      <div class="destination"><strong>Total Ascent:</strong><br> mostly flat</div>
      <div class="price"><strong>Price:</strong><br> 62,000</div>
</div>

This isn't correct. You might have an idea or a tip how I can do it?

Regards

#1796375

My whole is like this now:

function display_tourdetails() {
  
  global $post;
  $str = '';
   
  $tour_duration_category = get_post_meta($post->ID,'wpcf-tour-duration-category',true);
  $tour_duration_category_label = __("Tour Category:", "custom-code");
  $atour_duration = wp_get_post_terms( $post->ID, 'tour-duration', array( 'fields' => 'names' ) );
  $tour_duration = $atour_duration[0];
  $tour_duration_label = __("Duration:", "custom-code");
  $atour_specialty = wp_get_post_terms( $post->ID, 'tour-specialty', array( 'fields' => 'names' ) );
  $tour_specialty = $atour_specialty[0];
  $tour_specialty_label = __("Specialty:", "custom-code");
  $atour_difficulty = wp_get_post_terms( $post->ID, 'tour-difficulty', array( 'fields' => 'names' ) );
  $tour_difficulty = $atour_difficulty[0];
  $tour_difficulty_label = __("Difficulty:", "custom-code");
  $total_tour_distance_km = get_post_meta($post->ID,'wpcf-total-tour-distance-km',true);
  $total_tour_distance_mi = get_post_meta($post->ID,'wpcf-total-tour-distance-mi',true);
  $total_tour_distance_label = __("Total Riding Distance:", "custom-code");
  $average_distance_per_day = get_post_meta($post->ID,'wpcf-average-distance-per-day',true);
  $average_distance_per_day_label = __("Ride Distance p/day:", "custom-code");
  $average_climbing_per_day = get_post_meta($post->ID,'wpcf-average-climbing-per-day',true);
  $average_climbing_per_day_label = __("Total Ascent:", "custom-code");
  $tousandseparator = __(",", "custom-code");
  $price = number_format(get_post_meta($post->ID,'wpcf-price',true),0,".","$tousandseparator");	
  $price_label = __("Price:", "custom-code");
  
  $str .= '<div class="facts">';
  if(!empty($tour_duration_category)){
  		$str .= '<div class="style touring"><strong>'.$tour_duration_category_label.'</strong><br /> '.$tour_duration_category.'</div>';
	}
  if(!empty($tour_duration)){
	    $str .= '<div class="duration"><strong>'.$tour_duration_label.'</strong><br /> '.$tour_duration.'</div>';
   }
   if(!empty($tour_difficulty)){
	   $str .= '<div class="destination"><strong>'.$tour_difficulty_label.'</strong><br /> '.$tour_difficulty.'</div>';
   }
   if(!empty($tour_specialty)){
	   $str .= '<div class="price"><strong>'.$tour_specialty_label.'</strong><br /> '.$tour_specialty.'</div>';
   }
   if(!empty($total_tour_distance_km)){
	   $str .= '<div class="style touring"><strong>'.$total_tour_distance_label.'</strong><br /> '.$total_tour_distance_km.' km / '.$total_tour_distance_mi.' miles</div>';
   }
   if(!empty($average_distance_per_day)){
	   $str .= '<div class="duration"><strong>'.$average_distance_per_day_label.'</strong><br /> '.$average_distance_per_day.'</div>';
   }
   if(!empty($average_climbing_per_day)){
	   $str .= '<div class="destination"><strong>'.$average_climbing_per_day_label.'</strong><br /> '.$average_climbing_per_day.'</div>';
   }
   if(!empty($price)){
	   $str .= '<div class="price"><strong>'.$price_label.'</strong><br /> '.$price.'</div>';
   }
   $str .= '</div>';
  return $str;
}
add_shortcode( 'show_tourfacts', 'display_tourdetails' );

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.