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

Last updated by wolfgangD-6 2 months, 3 weeks ago.

Assigned support staff: Noman.

Author Posts
July 25, 2019 at 12:27 pm #4283057

wolfgangD-6

I have a php snippet implemented that shows an information/text/message to the customers in some cases.

Now I need to translate this text/message. How can I make this text/message it appear in string translation?

That's the snippet, and the message is under "echo '<p class="checkout_notice"....."

--- BEGIN SNIPPET ------------------------------------------------

function add_checkout_notice() {
 
if ( ! is_ajax() ) {
echo '<p class="checkout_notice" style="color:green;float:left;margin-bottom:18px;">➽ Bitte beachte, dass in Deinem Land bei der Einfuhr noch Kosten durch lokale Steuern und/oder Zoll anfallen können, auf welche wir keinen Einfluss haben!</p>';
} 
 
}
add_action( 'woocommerce_review_order_before_cart_contents', 'add_checkout_notice', 10 ); 
add_action( 'woocommerce_review_order_before_cart_contents', 'show_checkout_notice', 12 );
  
function show_checkout_notice() {
  
global $woocommerce;
$msg_states = array( 'CH','LI' );
  
if( !in_array( WC()->customer->shipping_country, $msg_states ) ) {
echo '<style>.checkout_notice{display:block !important}</style>';
 
} else {
echo '<style>.checkout_notice{display:none !important}</style>';
}
}

--- END SNIPPET ------------------------------------------------

Thanks a lot for your hints, have a great day,

Wolfgang

July 25, 2019 at 1:57 pm #4283843

Noman
Supporter

Languages: English (English )

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

Hi,

Thank you for contacting WPML Support.

1. You need to wrap the string in the gettext functions __() with some text domain like 'themedomain’. You can see example code here::

echo '<p class="checkout_notice" style="color:green;float:left;margin-bottom:18px;">➽ '.__('Bitte beachte, dass in Deinem Land bei der Einfuhr noch Kosten durch lokale Steuern und/oder Zoll anfallen können, auf welche wir keinen Einfluss haben!', 'themedomian').'</p>';

2. Please scan the theme from WPML >> Theme and plugins localization section.
3. Then search strings from WPML >> String Translation and add it’s translation.

Here are docs for more details:
- https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
- https://wpml.org/documentation/support/enabling-text-translation-for-themes-not-compatible-with-wpml/

Please let me know if this resolves your issue or you need further assistance with this issue.

Thank you

July 25, 2019 at 2:48 pm #4284399

wolfgangD-6

Hi Noman

Thanks for your answer. I tried and assigned the text to the domain "lonesomedragon" like this (but I only changed this line in the snippet, nothing else, hope that was enough):

echo '<p class="checkout_notice" style="color:green;float:left;margin-bottom:18px;">➽ '.__('Bitte beachte, dass in Deinem Land bei der Einfuhr noch Kosten durch lokale Steuern und/oder Zoll anfallen können, auf welche wir keinen Einfluss haben!', 'lonesomedragon').'</p>';

But even after scanning the theme the string does not show off in string translation. Maybe it's because I enter the code via the plugin "Snippets"? So I also scanned this plugin with WPML, but still the string (and no such domain like "lonesomedragon") appears not in string translation.

Am I doing something wrong?

Thanks again for your help,

Wolfgang

July 25, 2019 at 4:51 pm #4285249

Noman
Supporter

Languages: English (English )

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

Hi,

As you are using Snippets plugin, could you please try this solution that described in this ticket (Summary at top):
https://wpml.org/forums/topic/i-cant-see-translation-for-code-snippet-plugin/#post-2490665

Thank you

July 25, 2019 at 5:14 pm #4285347

wolfgangD-6

Hi Noman

Thanks for your answer. But this would mean to have a separate function for each language, this seems not very elegant to me.

Isn't there a possibility to enhance the code with something like in order to make it appear in WPML? But I'm not a programmer, I have no clue what I'm talking about *g*:

if (function_exists(‘pll_register_string’))

Thanks a lot,

Wolfgang

July 25, 2019 at 9:29 pm #4286137

wolfgangD-6

P.S. the idea with the registering in my last mail was found here:

https://wordpress.org/support/topic/translate-string-in-functions-php/

Although the solution is for the "Polylang" plugin maybe it can be adapted to WPML as well?

Thanks again, have a nice day,

Wolfgang

July 28, 2019 at 10:12 am #4295389

Noman
Supporter

Languages: English (English )

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

Hi,

You don’t need to create separate functions, you only need to use if/else statements to display language specific content. To do it, you can use ‘ICL_LANGUAGE_CODE’ constant to get current language code and you can use it in your code in this way:

<?php if(ICL_LANGUAGE_CODE == 'de'): ?>
	German language text goes here	      
<?php else: ?>
	//English/other language text goes here	
	
<?php endif; ?>

Here is the doc:
https://wpml.org/documentation/support/wpml-coding-api/

Please let me know if this resolves your issue or you need further assitance with this issue.

Thank you

July 28, 2019 at 8:44 pm #4295947

wolfgangD-6

Dear Noman

Thanks for your answer. So there's no way to register the string?

Regarding your proposed solution with php, what if there are more than two languages? Since I'm no programmer I have no idea how the "IF's" can be nested 🙁

Furthermore the echo with the text is already in an "IF", I don't know if this impacts.

I know this is more than I can ask, but I reckon for you it's just a second; would you mind to extend the current code with the IF's for let's say three languages (e.g. DE, FR and "EN/other languages")?

The current code is this (the message will only be output if the country is <> 'CH' or 'LI'):

--- BEGIN SNIPPET ----------------------------------------------

<?php

function add_checkout_notice() {
 
if ( ! is_ajax() ) {
echo '<p class="checkout_notice" style="color:green;float:left;margin-bottom:18px;">➽ THIS IS THE MESSAGE THAT NEEDS TO BE IN DIFFERENT LANGUAGES</p>';
} 
 
}
add_action( 'woocommerce_review_order_before_cart_contents', 'add_checkout_notice', 10 ); 
add_action( 'woocommerce_review_order_before_cart_contents', 'show_checkout_notice', 12 );
  
function show_checkout_notice() {
  
global $woocommerce;
$msg_states = array( 'CH','LI' );
  
if( !in_array( WC()->customer->shipping_country, $msg_states ) ) {
echo '<style>.checkout_notice{display:block !important}</style>';
 
} else {
echo '<style>.checkout_notice{display:none !important}</style>';
}
}

--- END SNIPPET ------------------------------------------------

Thanks a lot for your help,

Wolfgang

July 29, 2019 at 8:09 am #4297635

Noman
Supporter

Languages: English (English )

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

Hi,

To take a closer look on this issue, please provide temporary access (WP-Admin and FTP Login info) to your site, so that I can look into your setup and debug the issue.

Your next answer will be private which means only you and I have access to it.

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please provide me backend/frontend page links where I can see this string.

Thank you

July 30, 2019 at 11:01 am #4307049

Noman
Supporter

Languages: English (English )

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

Thanks for login details. I have replaced below code:

if ( ! is_ajax() ) {
echo '<p class="checkout_notice" style="color:green;float:left;margin-bottom:18px;">➽ THIS IS THE MESSAGE THAT NEEDS TO BE IN DIFFERENT LANGUAGES</p>';
}

With:

if ( ! is_ajax() ) {
	
	$cus_checkout_notice = '';
	if(ICL_LANGUAGE_CODE == 'de') { // For German language
		$cus_checkout_notice = 'THIS IS THE MESSAGE THAT NEEDS TO BE IN DIFFERENT LANGUAGES - DE';
	}
	elseif(ICL_LANGUAGE_CODE == 'fr'){ // For French language
		$cus_checkout_notice = 'THIS IS THE MESSAGE THAT NEEDS TO BE IN DIFFERENT LANGUAGES - FR';
	}
	elseif(ICL_LANGUAGE_CODE == 'en'){ // For English language
		$cus_checkout_notice = 'THIS IS THE MESSAGE THAT NEEDS TO BE IN DIFFERENT LANGUAGES - EN';
	}
	else { // For other language
		$cus_checkout_notice = 'THIS IS THE MESSAGE THAT NEEDS TO BE IN DIFFERENT LANGUAGES';
	}
	
	echo '<p class="checkout_notice" style="color:green;float:left;margin-bottom:18px;">➽ '.$cus_checkout_notice.'</p>';
} 

And now its showing correct notice according to selected language as you can see in the attached screenshots.

Thank you

July 30, 2019 at 7:01 pm #4310245

wolfgangD-6

Dear Noman

Thanks for the IF-nesting. That's so fantastic, you're the best!!!

When ever you need a coupon for a discounted love swing just let me know.

Keep on this great work, have a nice day and thanks again,

Wolfgang

July 30, 2019 at 7:03 pm #4310249

wolfgangD-6

Noman, thanks again so much for this extraordinary effort and help - keep on this great work and take care!