Skip Navigation

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

Problem:
Translate content/field of the Code Snippet plugin (PHP code).

Solution:
It's PHP code, so it's not possible to translate it. THe solution is to use the ICL_LANGUAGE_CODE constant to check the active language and use a different code for each one. E.g.

<?php
//Function for the EN language
function bbloomer_display_acf_field_under_images_en() {
      //EN function here
}
 
//Function for the FR language
function bbloomer_display_acf_field_under_images_fr() {
      //FR function here
}
 
// If French content
 if ( defined( 'ICL_LANGUAGE_CODE' ) && 'fr' == ICL_LANGUAGE_CODE ) {
    //French code here
    add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_display_acf_field_under_images_fr', 30 );
}
// If English content
else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'en' == ICL_LANGUAGE_CODE ) {
    //English code here
    add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_display_acf_field_under_images_en', 30 );
}
?>

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

Last updated by remiG-4 2 years, 3 months ago.

Assigned support staff: Diego Pereira.

Author Posts
July 12, 2018 at 11:58 am

Rafal

I try to translate content in Code Snippet plugin but no result.
I can't see this in String translations. Here is the code

add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_display_acf_field_under_images', 30 );

function bbloomer_display_acf_field_under_images() {
echo '<a href="#" class="mixmatch"<b>Mix products:</b></a></p> ' . get_field('eee');
// Note: 'trade' is the slug of the ACF
}

I try
- scan theme
- scan plugin Code Snippet
- Translate texts in admin screens »

Any idea whats next ?

regards
Rafael

July 12, 2018 at 7:37 pm #2490665

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello @rafal, welcome to the WPML support Forum!

The string is a PHP code, so it is not possible to translate it. But maybe you can adapt your code and use the ICL_LANGUAGE_CODE to check the active language and use a different code for each one.Here is a example with french and english languages:

<?php
//Function for the EN language
function bbloomer_display_acf_field_under_images_en() {
      echo '<a href="#" class="mixmatch"<b>[EN]Mix products:</b></a></p> ' . get_field('eee');
      // Note: 'trade' is the slug of the ACF
}

//Function for the FR language
function bbloomer_display_acf_field_under_images_fr() {
      echo '<a href="#" class="mixmatch"<b>[FR]Mix products:</b></a></p> ' . get_field('eee');
      // Note: 'trade' is the slug of the ACF
}

// If French content
 if ( defined( 'ICL_LANGUAGE_CODE' ) && 'fr' == ICL_LANGUAGE_CODE ) {
    //French code here
    add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_display_acf_field_under_images_fr', 30 );
}
// If English content
else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'en' == ICL_LANGUAGE_CODE ) {
    //English code here
    add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_display_acf_field_under_images_en', 30 );
}
?>

More infos here: https://wpml.org/documentation/support/wpml-coding-api/

IMPORTANT: Please backup your site first before making any change, this is important for security reasons.

If you need more help just let me know.

All the best,
Diego

July 16, 2018 at 8:06 am #2527641

Rafal

Hi @diego

Thank you very much.
Work like a charm!

Regards
Rafal

July 16, 2018 at 11:24 am #2527972

Diego Pereira
Supporter

Languages: English (English ) Spanish (Español ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi there, I'm glad to hear that the issue is solved ?

Please contact us if you need our help in the future with WPML. We will be more than happy to help.

Cheers,
Diego

December 6, 2019 at 3:25 pm #5084875

christopherD-5

Is there a way to achieve the same thing but with js code in the header? Thanks!

December 19, 2019 at 3:47 pm #5164653

remiG-4

Hi Diego.
I did this code for add a metacode field in the emails...
But the langage doesn't works because in this case it must tale in consideration the langage of the order.. not of the curent langage.
If I change the langage of my admin page and send the email that work.. but it's not the goal.
Any idea?
Thanks
Rémi

/**
* Add a custom field (in an order) to the emails https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order )
{
// langues ne fonctionnent pas car regarde que la langue de la page active en cours
if(ICL_LANGUAGE_CODE=='fr'){
echo "<h2 style=\"color: #0F55A5\">Votre numéro de suivi</h2> ".get_post_meta($order->get_id(), 'my_tracking_number', true)."<br><br>";
}
elseif(ICL_LANGUAGE_CODE=='it'){
echo "<h2 style=\"color: #0F55A5\">Il tuo numero di tracking</h2> ".get_post_meta($order->get_id(), 'my_tracking_number', true)."<br><br>";
}
elseif(ICL_LANGUAGE_CODE=='es'){
echo "<h2 style=\"color: #0F55A5\">Su número de seguimiento</h2> ".get_post_meta($order->get_id(), 'my_tracking_number', true)."<br><br>";
}
elseif(ICL_LANGUAGE_CODE=='de'){
echo "<h2 style=\"color: #0F55A5\">Ihre Trackingnummer</h2> ".get_post_meta($order->get_id(), 'my_tracking_number', true)."<br><br>";
}
else {
echo "<h2 style=\"color: #0F55A5\">Your tracking number</h2> ".get_post_meta($order->get_id(), 'my_tracking_number', true)."<br><br>";
}

}

December 20, 2019 at 9:18 am #5169501

remiG-4

I think I must check the value of "wpml_language" metadata.. But not sure how to test it with "if" function...
Thanks!