Skip to content

Instantly share code, notes, and snippets.

@emilsundberg
Created June 2, 2014 19:55
Show Gist options
  • Save emilsundberg/536f960f1842f1e7a933 to your computer and use it in GitHub Desktop.
Save emilsundberg/536f960f1842f1e7a933 to your computer and use it in GitHub Desktop.
Chargify Translation JavaScript
$(document).ready(function(){
function get_value(value)
{
var v = new String(value.match(/[0-9.,]+/));
return Number(v.replace(/[^0-9\.]+/g,""));
}
function remove_numerics(value)
{
return value.replace(value.match(/[0-9.]+/), '');
}
function remove_unnecessary()
{
$('.section_one').hide();
$('#content h2:first').hide();
$('#summary h2').hide();
}
function translate_static_text()
{
$('#security span').html('Den här sidan är säker');
$('#hosted-payment-form h2:first').html("Kortuppgifter");
$('#hosted-payment-form h2:eq(1)').html("Dina uppgifter");
$('#hosted-payment-form h2:eq(2)').html("Kontaktuppgifter");
$('.section_three .hint').html('Använd inga mellanrum eller bindestreck tack');
$('label[for="subscription_payment_profile_attributes_full_number"]').html("* Kreditkortsnummer");
$('label[for="subscription_payment_profile_attributes_expiration_month"]').html("* Går ut");
$('label[for="subscription_payment_profile_attributes_first_name"]').html("* Förnamn");
$('label[for="subscription_payment_profile_attributes_last_name"]').html("* Efternamn");
$('label[for="subscription_payment_profile_attributes_billing_address"]').html("* Adress");
$('label[for="subscription_payment_profile_attributes_billing_address_2"]').parent().hide();
// Area and Country
$('label[for="subscription_payment_profile_attributes_billing_country"]').html("Land");
$('#subscription_payment_profile_attributes_billing_country option:first-child').html("Välj land");
$('#subscription_payment_profile_attributes_billing_country').val('SE').change();
$('#subscription_payment_profile_attributes_billing_country option[value="SE"]').html("Sverige");
$('label[for="subscription_payment_profile_attributes_billing_state"]').html("Län");
$('#subscription_payment_profile_attributes_billing_state option:first-child').html("Välj län");
$('label[for="subscription_payment_profile_attributes_billing_city"]').html("Postort");
$('label[for="subscription_payment_profile_attributes_billing_zip"]').html("Postnummer");
$('label[for="subscription_coupon_code"]').html("Rabattkod");
$('#coupon_button').html("Applicera kod");
$('label[for="subscription_customer_attributes_first_name"]').html("* Förnamn");
$('label[for="subscription_customer_attributes_last_name"]').html("* Efternamn");
$('label[for="subscription_customer_attributes_email"]').html("* Email");
$('label[for="subscription_customer_attributes_phone"]').html("Telefon");
$('label[for="subscription_customer_attributes_organization"]').html("Företag/organisation");
$('.section_five label').html('Jag godkänner <a target="blank" href="http://example.com">användarvillkoren</a>');
$('#subscription_submit').val('Fortsätt');
}
function update_values()
{
// Summary
var baseline_items_value = $('#summary-recurring-charges').html();
var baseline_items_content = $('#summary-recurring-charges').html();
baseline_items_value = get_value(baseline_items_value);
if (baseline_items_value > 0) {
if (new String(baseline_items_content).indexOf('every 1 month') > 0) {
// Monthly subscription
$('#summary-recurring-charges').html(baseline_items_value + ' kr/mån');
var pay_interval = 'monthly';
} else {
// Annual subscription
$('#summary-recurring-charges').html(baseline_items_value + ' kr/år');
var pay_interval = 'annually';
}
} else {
// Free signup
$('#content h1').html('Din beställning');
$('#total').html('Snowfire Free (gratis) <div id="todays-charge" style="display: none">0</div>');
$('#summary h2').hide();
$('#summary .section_one').hide();
}
// Todays charge
var todays_charges = $('#todays-charge').html();
todays_charges = get_value(todays_charges);
var todays_charges_no_tax = todays_charges * 0.8;
var tax = todays_charges - todays_charges_no_tax;
if (pay_interval == 'annually') {
$('#total h3').html('<span id="todays-charge" style="padding-bottom:4px;display: block;">' + round(todays_charges_no_tax) + ' kr/år</span>');
$('#total h3').append('<span style="display: block;padding-top:3px;font-size: 12px;font-weight: normal;padding-bottom:4px">Moms 25%: ' + round(tax) + ' kr</span>');
$('#total h3').append('<span style="display: block;padding-top:3px;font-size: 12px;font-weight: normal;">Totalt: ' + round(tax + todays_charges_no_tax) + ' kr</span>');
$('#total .tint').html('').hide();
} else {
$('#total h3').html('<span id="todays-charge" style="padding-bottom:4px;display: block;">' + round(todays_charges_no_tax) + ' kr/mån</span>');
$('#total h3').append('<span style="display: block;padding-top:3px;font-size: 12px;font-weight: normal;padding-bottom:4px">Moms 25%: ' + round(tax) + ' kr</span>');
$('#total h3').append('<span style="display: block;padding-top:3px;font-size: 12px;font-weight: normal;">Totalt: ' + round(tax + todays_charges_no_tax) + ' kr</span>');
$('#total .tint').html('').hide();
}
var coupon_savings = $('#coupon_savings').html();
if (coupon_savings != null) {
coupon_savings = get_value(coupon_savings);
$('#coupon_savings').html("Rabatt: " + round(coupon_savings) + " SEK/månad");
}
}
function round(value)
{
return Math.round(value);
}
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
// Events
$('body').ajaxComplete(function(e, xhr, settings) {
var url = remove_numerics(settings.url);
//console.log('ajax completed: ' + url);
// Coupon event
if (url == '/product_families//coupons/validate/?existing_subscription=false') {
update_values();
remove_unnecessary();
// translate text
$('#short_coupon_message .success_message').html('Giltig');
$('#long_coupon_message .success_message').remove();
}
// Country change event
if (url == '/subdivisionsjson?code=SE') {
$('#subscription_payment_profile_attributes_billing_state').val('AB').change();
$('label[for="subscription_payment_profile_attributes_billing_state"]').css('visibility', 'hidden');
$('#subscription_payment_profile_attributes_billing_state').css('visibility', 'hidden');
} else if (url.substring(0, 19) == '/subdivisions.json?') {
$('label[for="subscription_payment_profile_attributes_billing_state"]').css('visibility', 'visible');
$('#subscription_payment_profile_attributes_billing_state').css('visibility', 'visible');
}
// Preview event
if (url == '/h//subscriptions/new/preview') {
update_values();
remove_unnecessary();
}
});
// Init translation
remove_unnecessary();
translate_static_text();
update_values();
// Auto apply coupon via GET
var coupon_code = getParameterByName('coupon_code');
$('#subscription_coupon_code').val(coupon_code);
$('#coupon_button').click();
// Hide coupon if we are asked to do so
var disable_coupon_code = getParameterByName('disable_coupon_code');
if (disable_coupon_code == 1) {
$('#subscription_coupon_code').parent().parent().hide();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment