How to translate unit of measurement (kg, m, cm) in WooCommerce?

Does not work convert measurements in WooCommerce. In .po file the translation is, but on the website on the product they are in English
92bf9d8387a84049a8e6129c95cddd02.png

It does not help even change the code of WooCommerce

class-wc-settings-products.php

array(
 'title' => __( 'Weight Unit', 'woocommerce' ),
 'desc' => __( 'This controls what unit you will define weights in.', 'woocommerce' ),
 'id' => 'woocommerce_weight_unit',
 'class' => 'wc-enhanced-select',
 'css' => 'min-width:300px;',
 'default' => 'kg',
 'type' => 'select',
 'options' => array(
 'kg' => __( 'kg', 'woocommerce' ),
 'g' => __( 'g', 'woocommerce' ),
 'lbs' => __( 'lbs', 'woocommerce' ),
 'oz' => __( 'oz', 'woocommerce' ),
),
 'desc_tip' => true,
 ),
July 2nd 19 at 13:58
3 answers
July 2nd 19 at 14:00
Rummaged in the code and realized that this is a bug Welcomers. The unit of measure that displays in the tab with the attributes of not turning in localization functions.

Localization of units of weight

The output is in the file
.../plugins/woocommerce/templates/single-product/product-attributes.php

This file you need to copy to your theme and place at:
.../themes/[MY-THEME]/woocommerce/single-product/product-attributes.php

Then make changes to it:

Line 39
<?wc_format_localized_decimal php echo( $product--->get_weight() ) . '' . esc_attr( get_option( 'woocommerce_weight_unit' ) ); ?>


You need to wrap esc_attr( get_option( 'woocommerce_weight_unit' ) ) in the function __()

<?wc_format_localized_decimal php echo( $product--->get_weight() ) . '' . __(esc_attr( get_option( 'woocommerce_weight_unit' ) ), 'woocommerce'); ?>


Localization of units of length

Unfortunately, the length value displayed by the function get_dimensions(), which is registered in the kernel and cannot be rewritten. The function already returns the full string with numbers and unit of measure. But before the withdrawal string is passed through the filter woocommerce_product_dimensions.

In the file functions.php you can replace the value of a unit of length from English into Russian.

localize_demensions_units function($dimensions) {
 return str_replace('m', 'm', $dimensions);
}
add_filter('woocommerce_product_dimensions', 'localize_demensions_units');


You need to substitute the right values.
Thanks for the solution! just recently also tried to find how to solve it - Nathaniel commented on July 2nd 19 at 14:03
Thank you! But I somehow localization of measurements of weight is not completely gone. prnt.sc/dodsh3 - Cathy_Grimes commented on July 2nd 19 at 14:06
Thanks for the solution! Small improvements on the fan:

__(esc_attr( get_option( 'woocommerce_weight_unit' ) ), 'woocommerce')

can be written as
esc_attr__( get_option( 'woocommerce_weight_unit' ), 'woocommerce')


This function itself will adjust to meters, centimeters and millimeters:
localize_demensions_units function( $dimensions ) {
 $unit = mb_substr( $dimensions, mb_strrpos( $dimensions, '' ) + 1 );
 return str_replace( $unit, __( $unit, 'woocommerce' ), $dimensions );
}
- tianna_Torphy89 commented on July 2nd 19 at 14:09
Thank you! It's all good! - Shawna.Ols commented on July 2nd 19 at 14:12
July 2nd 19 at 14:02
I have the filter woocommerce_product_dimensions - didn't work. Changed it to woocommerce_format_dimensions - and everything is OK.
July 2nd 19 at 14:04
To localize the units of weight

localize_weight_units function($weight) {
 return str_replace('kg', 'kg', $weight);
}
add_filter('woocommerce_format_weight', 'localize_weight_units');

Find more questions by tags WooCommerceWordPress