John Davidson

wordpress - Make changeable text field translate ready in php

0 comments
Message:


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.



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;
}
}


My Answer:
To make a changeable text field translatable in WordPress, you can use the `__()` function or `esc_html__()` function to wrap the text value inside the field. This will allow WPML (WordPress Multilingual Plugin) to detect the string and make it translatable.

For example, you can modify your code like this:

php
$text_value = 'Your dynamic text value here';
$translated_text = esc_html__( $text_value, 'your-text-domain' );


By using the `esc_html__()` function, you are marking the text value as translatable and providing a text domain for WPML to detect and translate it.

Make sure to replace `'your-text-domain'` with the actual text domain of your theme or plugin.

Now, each time you create an offer or update the text value in the field, WPML will be able to detect the string and provide translation options for it.

Rate this post

4 of 5 based on 8863 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us