0

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 );
disinfor
  • 10,865
  • 2
  • 33
  • 44
  • Does this answer your question? [Polylang: How to translate custom strings?](https://stackoverflow.com/questions/46557981/polylang-how-to-translate-custom-strings) – disinfor May 16 '20 at 21:58

2 Answers2

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.

rank
  • 2,361
  • 2
  • 10
  • 20
  • Hi, rank. Thanks for your kind answer, the code is working. But unfortunately, the test position was mess up. I wanted to have the text 'Gift Receiver' below the product title. Could you guide me on how to style the text using CSS? https://i.stack.imgur.com/gT3ZQ.png –  May 17 '20 at 14:13
  • Please accept the answer, as it solves your problem and the code is working. - Your new question: If you want you start it in a new line, just add `
    ` html tag after your a tag, or before you get the polylang string. No css should be needed as you just want to have a linebreak.
    – rank May 17 '20 at 14:18
  • I tried to add like this
    , But is not working.
    –  May 17 '20 at 16:23
  • May you try adding it before the `
    ` ? If does not work, maybe try like `'
    '.'
    – rank May 17 '20 at 16:30
  • The string seems not to be placed in the – rank May 17 '20 at 16:53
  • Here is the new question link: https://stackoverflow.com/questions/61856779/br-link-break-code-not-working-in-function-php-polylang –  May 17 '20 at 18:34
1

This is the setup that I am doing for Polylang string translations, so I can still have standard __ and _e translation functions.

if ( !is_admin() ) {
    add_filter( 'gettext', 'my_translate_string', 10, 3 );
}
function my_translate_string( $translated_text, $text, $domain ) {
    if ( $domain <> 'mytheme' || !function_exists( 'pll__' ) ) {
        return $translated_text;
    }
    return pll__( $text );
}

add_action( 'after_setup_theme', 'my_register_polylang_strings' );
function my_register_polylang_strings() {

    if ( function_exists( 'pll_register_string' ) ) {

        // template-parts/misc/breadcrumbs.php
        pll_register_string( 'my-search-results', 'Search Results', 'mytheme', false );
        pll_register_string( 'my-home', 'Home', 'mytheme', false );

        // template-parts/loop/post.php
        pll_register_string( 'my-read-more', 'Read More', 'mytheme', false );

        // in your example would be
        pll_register_string( 'my-gift-receiver', 'Gift Receiver', 'mytheme', false );
        
    }

}

Then you can use in your theme files

_e( 'Gift Receiver', 'mytheme' );
__( 'Gift Receiver', 'mytheme' );

So in your case that would be:

function gift_receiver( $cart_item, $cart_item_key ) {
    $greceiver = isset( $cart_item['greceiver'] ) ? $cart_item['greceiver'] : '';
    printf(
    '<div><label>'.__('Gift Receiver','mytheme').':</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 );

'mytheme' is your theme domain.

gviger
  • 11
  • 3