1

I have a text field in the backend. The text value inside it will be different depends on the products I choose. So, it's a kind of dynamic field.

enter image description here

How can I target that field to make it translatable? so each time I create an offer WPML can detect the string?

if ( $total_to_add - $total_added > 0 ) {
    if ( ! in_array( $rule['key'], $special_offers, true ) ) {
        $rule_text = isset( $rule['text_in_modal_special_offer'] ) ? $rule['text_in_modal_special_offer'] : __( 'Get a special discount if you add {{total_to_add}} product(s) to your order.', 'discountText' );
        $special_offer = array(
            'text'     => $rule_text,
            'items_in_cart' => $total_added,
            'discount' => array(
                'type' => $discount['discount_amount']['type'],
                'amount' => $discount['discount_amount']['amount'],
             ),
         );
         $special_offers[ $rule['key'] ] = $special_offer;
     }
}
Bassam Radi
  • 169
  • 9

1 Answers1

0

You need to use the "Auto-register strings for translation" feature of WPML (documentation):

WPML uses static code analysis to locate strings that require translation in the theme and plugins. In some cases, the static code-scan cannot reliably find all strings. This often happens when strings are generated dynamically using code.

Then you need to go to the front end of the site and make sure both variants of your IF statement are executed. Also, I would try to __() the dynamic value as well:

$rule_text = isset( $rule['text_in_modal_special_offer'] ) ? __($rule['text_in_modal_special_offer'], 'discountText') : __( 'Get a special discount if you add {{total_to_add}} product(s) to your order.', 'discountText' );

After that, go to WPML > String Translation, search for both strings, and translate them.

montrealist
  • 5,593
  • 12
  • 46
  • 68