Can anyone teach me how to translate strings from my custom code in function.php of child theme?
I want to have the strings 'Gift Receiver:' and 'Contact No:' to be translatable in Polylang 'Strings Translation' Tab.
Regards
function gift_receiver( $cart_item, $cart_item_key ) {
$greceiver = isset( $cart_item['greceiver'] ) ? $cart_item['greceiver'] : '';
printf(
'<div><label>Gift Receiver:</label> <textarea class="%s" id="rcart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$greceiver
);
}
add_action( 'woocommerce_after_cart_item_name', 'gift_receiver', 10, 2 );
function gift_sender( $cart_item, $cart_item_key ) {
$gsender = isset( $cart_item['gsender'] ) ? $cart_item['gsender'] : '';
printf(
'<div><label>Contact No:</label> <textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
'prefix-cart-notes',
$cart_item_key,
$cart_item_key,
$gsender
);
}
add_action( 'woocommerce_after_cart_item_name', 'gift_sender', 10, 2 );
You will need the text domain of your theme to make it work. Insert your text domain when I use "your_textdomain".
You need to put the strings in your plugin like this:
<?php echo pll_e('Your String'); ?>
So with your code something like:
... printf('<div><label>'.pll_e('Gift Receiver').':</label> <textarea ....
Then you can add this strings to the functions.php file in your theme folder:
/** TRANSLATIONS **/
add_action('init', function() {
pll_register_string('your_textdomain', 'Your String');
pll_register_string('your_textdomain', 'Gift Receiver');
});
After that, you are able to find this strings in the string translations of polylang plugin. Using the textdomain of your theme, you can find them easily using the select field of the string translations tab.