Move localisation to address-i18n.js for account page. Closes #4446

This commit is contained in:
Mike Jolley 2014-01-08 14:38:17 +00:00
parent 51bd102fb8
commit 7ddd4d012a
6 changed files with 105 additions and 95 deletions

View File

@ -0,0 +1,94 @@
jQuery(document).ready(function($) {
var locale_json = wc_address_i18n_params.locale.replace(/"/g, '"');
var locale = $.parseJSON( locale_json );
var required = ' <abbr class="required" title="' + wc_address_i18n_params.i18n_required_text + '">*</abbr>';
$('body')
// Handle locale
.bind('country_to_state_changing', function( event, country, wrapper ){
var thisform = wrapper;
if ( typeof locale[country] != 'undefined' ) {
var thislocale = locale[country];
} else {
var thislocale = locale['default'];
}
// Handle locale fields
var locale_fields = $.parseJSON( wc_address_i18n_params.locale_fields );
$.each( locale_fields, function( key, value ) {
var field = thisform.find( value );
if ( thislocale[key] ) {
if ( thislocale[key]['label'] ) {
field.find('label').html( thislocale[key]['label'] );
}
if ( thislocale[key]['placeholder'] ) {
field.find('input').attr( 'placeholder', thislocale[key]['placeholder'] );
}
field.find('label abbr').remove();
if ( typeof thislocale[key]['required'] == 'undefined' && locale['default'][key]['required'] == true ) {
field.find('label').append( required );
} else if ( thislocale[key]['required'] == true ) {
field.find('label').append( required );
}
if ( key !== 'state' ) {
if ( thislocale[key]['hidden'] == true ) {
field.hide().find('input').val('');
} else {
field.show();
}
}
} else if ( locale['default'][key] ) {
if ( locale['default'][key]['required'] == true ) {
if (field.find('label abbr').size()==0) field.find('label').append( required );
}
if ( key !== 'state' ) {
if ( typeof locale['default'][key]['hidden'] == 'undefined' || locale['default'][key]['hidden'] == false ) {
field.show();
} else if ( locale['default'][key]['hidden'] == true ) {
field.hide().find('input').val('');
}
}
}
});
var $postcodefield = thisform.find('#billing_postcode_field, #shipping_postcode_field');
var $cityfield = thisform.find('#billing_city_field, #shipping_city_field');
var $statefield = thisform.find('#billing_state_field, #shipping_state_field');
if ( ! $postcodefield.attr('data-o_class') ) {
$postcodefield.attr('data-o_class', $postcodefield.attr('class'));
$cityfield.attr('data-o_class', $cityfield.attr('class'));
$statefield.attr('data-o_class', $statefield.attr('class'));
}
// Re-order postcode/city
if ( thislocale['postcode_before_city'] ) {
$postcodefield.add( $cityfield ).add( $statefield ).removeClass('form-row-first form-row-last').addClass('form-row-wide');
$postcodefield.insertBefore( $cityfield );
} else {
// Default
$postcodefield.attr('class', $postcodefield.attr('data-o_class'));
$cityfield.attr('class', $cityfield.attr('data-o_class'));
$statefield.attr('class', $statefield.attr('data-o_class'));
$postcodefield.insertAfter( $statefield );
}
});
});

View File

@ -0,0 +1 @@
jQuery(document).ready(function(e){var t=wc_address_i18n_params.locale.replace(/&quot;/g,'"'),n=e.parseJSON(t),r=' <abbr class="required" title="'+wc_address_i18n_params.i18n_required_text+'">*</abbr>';e("body").bind("country_to_state_changing",function(t,i,s){var o=s;if(typeof n[i]!="undefined")var u=n[i];else var u=n["default"];var a=e.parseJSON(wc_address_i18n_params.locale_fields);e.each(a,function(e,t){var i=o.find(t);if(u[e]){u[e].label&&i.find("label").html(u[e].label);u[e].placeholder&&i.find("input").attr("placeholder",u[e].placeholder);i.find("label abbr").remove();typeof u[e]["required"]=="undefined"&&n["default"][e]["required"]==1?i.find("label").append(r):u[e]["required"]==1&&i.find("label").append(r);e!=="state"&&(u[e]["hidden"]==1?i.hide().find("input").val(""):i.show())}else if(n["default"][e]){n["default"][e]["required"]==1&&i.find("label abbr").size()==0&&i.find("label").append(r);e!=="state"&&(typeof n["default"][e]["hidden"]=="undefined"||n["default"][e]["hidden"]==0?i.show():n["default"][e]["hidden"]==1&&i.hide().find("input").val(""))}});var f=o.find("#billing_postcode_field, #shipping_postcode_field"),l=o.find("#billing_city_field, #shipping_city_field"),c=o.find("#billing_state_field, #shipping_state_field");if(!f.attr("data-o_class")){f.attr("data-o_class",f.attr("class"));l.attr("data-o_class",l.attr("class"));c.attr("data-o_class",c.attr("class"))}if(u.postcode_before_city){f.add(l).add(c).removeClass("form-row-first form-row-last").addClass("form-row-wide");f.insertBefore(l)}else{f.attr("class",f.attr("data-o_class"));l.attr("class",l.attr("data-o_class"));c.attr("class",c.attr("data-o_class"));f.insertAfter(c)}})});

View File

@ -351,98 +351,8 @@ jQuery(document).ready(function($) {
return false;
});
/* Localisation */
var locale_json = wc_checkout_params.locale.replace(/&quot;/g, '"');
var locale = $.parseJSON( locale_json );
var required = ' <abbr class="required" title="' + wc_checkout_params.i18n_required_text + '">*</abbr>';
$('body')
// Handle locale
.bind('country_to_state_changing', function( event, country, wrapper ){
var thisform = wrapper;
if ( typeof locale[country] != 'undefined' ) {
var thislocale = locale[country];
} else {
var thislocale = locale['default'];
}
// Handle locale fields
var locale_fields = $.parseJSON( wc_checkout_params.locale_fields );
$.each( locale_fields, function( key, value ) {
var field = thisform.find( value );
if ( thislocale[key] ) {
if ( thislocale[key]['label'] ) {
field.find('label').html( thislocale[key]['label'] );
}
if ( thislocale[key]['placeholder'] ) {
field.find('input').attr( 'placeholder', thislocale[key]['placeholder'] );
}
field.find('label abbr').remove();
if ( typeof thislocale[key]['required'] == 'undefined' && locale['default'][key]['required'] == true ) {
field.find('label').append( required );
} else if ( thislocale[key]['required'] == true ) {
field.find('label').append( required );
}
if ( key !== 'state' ) {
if ( thislocale[key]['hidden'] == true ) {
field.hide().find('input').val('');
} else {
field.show();
}
}
} else if ( locale['default'][key] ) {
if ( locale['default'][key]['required'] == true ) {
if (field.find('label abbr').size()==0) field.find('label').append( required );
}
if ( key !== 'state' ) {
if ( typeof locale['default'][key]['hidden'] == 'undefined' || locale['default'][key]['hidden'] == false ) {
field.show();
} else if ( locale['default'][key]['hidden'] == true ) {
field.hide().find('input').val('');
}
}
}
});
var $postcodefield = thisform.find('#billing_postcode_field, #shipping_postcode_field');
var $cityfield = thisform.find('#billing_city_field, #shipping_city_field');
var $statefield = thisform.find('#billing_state_field, #shipping_state_field');
if ( ! $postcodefield.attr('data-o_class') ) {
$postcodefield.attr('data-o_class', $postcodefield.attr('class'));
$cityfield.attr('data-o_class', $cityfield.attr('class'));
$statefield.attr('data-o_class', $statefield.attr('class'));
}
// Re-order postcode/city
if ( thislocale['postcode_before_city'] ) {
$postcodefield.add( $cityfield ).add( $statefield ).removeClass('form-row-first form-row-last').addClass('form-row-wide');
$postcodefield.insertBefore( $cityfield );
} else {
// Default
$postcodefield.attr('class', $postcodefield.attr('data-o_class'));
$cityfield.attr('class', $cityfield.attr('data-o_class'));
$statefield.attr('class', $statefield.attr('data-o_class'));
$postcodefield.insertAfter( $statefield );
}
})
// Init trigger
.bind('init_checkout', function() {
$('#billing_country, #shipping_country, .country_to_state').change();

File diff suppressed because one or more lines are too long

View File

@ -64,6 +64,7 @@ class WC_Frontend_Scripts {
wp_register_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
wp_register_script( 'wc-single-product', $frontend_script_path . 'single-product' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
wp_register_script( 'wc-country-select', $frontend_script_path . 'country-select' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
wp_register_script( 'wc-address-i18n', $frontend_script_path . 'address-i18n' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
wp_register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.3.1', true );
// Queue frontend scripts conditionally
@ -80,7 +81,7 @@ class WC_Frontend_Scripts {
wp_enqueue_style( 'woocommerce_chosen_styles', $assets_path . 'css/chosen.css' );
}
wp_enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select' ), WC_VERSION, true );
wp_enqueue_script( 'wc-checkout', $frontend_script_path . 'checkout' . $suffix . '.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ), WC_VERSION, true );
}
if ( is_add_payment_method_page() )
@ -113,14 +114,17 @@ class WC_Frontend_Scripts {
wp_localize_script( 'wc-checkout', 'wc_checkout_params', apply_filters( 'wc_checkout_params', array(
'ajax_url' => WC()->ajax_url(),
'ajax_loader_url' => apply_filters( 'woocommerce_ajax_loader_url', $assets_path . 'images/ajax-loader@2x.gif' ),
'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ),
'update_order_review_nonce' => wp_create_nonce( "update-order-review" ),
'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ),
'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
'checkout_url' => add_query_arg( 'action', 'woocommerce_checkout', WC()->ajax_url() ),
'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0,
'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0
) ) );
wp_localize_script( 'wc-address-i18n', 'wc_address_i18n_params', apply_filters( 'wc_address_i18n_params', array(
'locale' => json_encode( WC()->countries->get_country_locale() ),
'locale_fields' => json_encode( WC()->countries->get_country_locale_field_selectors() )
'locale_fields' => json_encode( WC()->countries->get_country_locale_field_selectors() ),
'i18n_required_text' => esc_attr__( 'required', 'woocommerce' ),
) ) );
wp_localize_script( 'wc-cart', 'wc_cart_params', apply_filters( 'wc_cart_params', array(

View File

@ -161,6 +161,7 @@ class WC_Shortcode_My_Account {
// Enqueue scripts
wp_enqueue_script( 'wc-country-select' );
wp_enqueue_script( 'wc-address-i18n' );
// Prepare values
foreach ( $address as $key => $field ) {