Translate custom shortcut fields in my theme's function.php file

With the help of the community, I was able to create, save and print labels and their values ​​on the page of one product.

I can also translate input values ​​into different languages ​​using Polylang , but translating custom labels (Terms and brands) is extremely difficult.

Can anyone help me with these issues?

I tried using Polylang , Saywhat ... without success!

Here is the code:

// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    global $woocommerce, $post;

    echo '<div class="options_group">';
    woocommerce_wp_text_input( array( // Text Field type
        'id'          => '_conditions', 
        'label'       => __( 'Conditions', 'woocommerce' ), 
        'placeholder' => 'i.e: brand-new; refurbished; defected...',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the conditions of the products here.', 'woocommerce' ) 
    ) );
    woocommerce_wp_text_input( array( // Text Field type
        'id'          => '_brands',
        'label'       => __( 'Brands', 'woocommerce' ), 
        'placeholder' => 'i.e: Lacoste; Hugo Boss...etc',
        'desc_tip'    => 'true',
        'description' => __( 'Enter names of the Brands of the products if any.', 'woocommerce' ) 
    ));
    echo '</div>'; // Closing </div> tag HERE
}

// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){

    // Saving "Conditions" field key/value
    $conditions_field = $_POST['_conditions'];
    if( !empty( $conditions_field ) )
        update_post_meta( $post_id, '_conditions', esc_attr( $conditions_field ) );

    // Saving "Brands" field key/value
    $brands_field = $_POST['_brands'];
    if( !empty( $brands_field ) )
        update_post_meta( $post_id, '_brands', esc_attr( $brands_field ) );
}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 20);
function woo_display_custom_general_fields_values() {
    global $product;

    echo '<p class="custom-conditions">Conditions: ' . get_post_meta( $product->id, '_conditions', true ) . '</p>';
    echo '<p class="custom-brands">Brands: ' . get_post_meta( $product->id, '_brands', true ) . '</p>';
}

And here is a screenshot:

enter image description here

Thank.

+4
source share
1 answer

-, gettex 'woocommerce' ( - ), woocommerce, .

1) :

, , , , function.php ( ), Loco Translate, WordPress.

Loco Translate, , .

Loco Translate :

  • WordPress admin
  • MO Gettext .
  • PO, , .
  • PO
  • PO
  • WordPress

2) ( WooCommerce):

WPML , WooCommerce -. WooCommerce .

, -, .

+1

Source: https://habr.com/ru/post/1664844/


All Articles