Question: Translate custom labels fields in function.php file of my theme

Question

Translate custom labels fields in function.php file of my theme

Answers 1
Added at 2016-12-25 16:12
Tags
Question

With the help from the community, I have succeeded to create, save and print the labels and their values to the single product page.

I can also translate the input values into different languages using Polylang, but translating the custom labels (Conditions and Brands) is extremely hard.

Anyone out there can help me with these issues?

I tried to use Polylang, Saywhat...with no 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 a screenshot:

enter image description here

Thank you.

Answers to

Translate custom labels fields in function.php file of my theme

nr: #1 dodano: 2016-12-25 22:12

First, you should change the gettex domain name from 'woocommerce' to the domain name of your theme (or something more custom), as it doesn't make part of woocommerce code, and it's located in your active theme.

1) the Free alternative:

Then as it's not really content that you are trying to translate, but some peaces of code located in the function.php file of your active child theme (or active theme), you should use a specialized free plugin as Loco Translate that will provides in-browser editing of WordPress translation files.

Loco Translate, also provides localization tools for developers, such as extracting strings and generating templates.

Loco Translate features include:

  • Built-in translation editor within WordPress admin
  • Create and update language files directly in your theme or plugin
  • Extraction of translatable strings from your source code
  • Native MO file compilation without the need for Gettext on your system
  • Support for PO features including comments, references and plural forms
  • PO source view with clickable source code references
  • Protected language directory for saving custom translations
  • Configurable PO file backups
  • Built-in WordPress locale codes

2) The commercial complete way (completely compatible with WooCommerce):

The most complete commercial alternative is WPML plugin, that will also handle perfectly and more easily WooCommerce custom localisation and translations content for multilingual web sites. The other free optional plugins are incomplete for WooCommerce and much more difficult to get them work totally well.

So If you are planing to publish a multilingual website, think about it.

Source Show
◀ Wstecz