Last active
August 29, 2015 14:07
-
-
Save ray-peters/79fb7f48615e0e7b4b5a to your computer and use it in GitHub Desktop.
Translates currency symbols for ONTRAPORT order forms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
// jQuery is required to run this snippet | |
// They are provided above from reliable CDNs if your page doesn't already have them added. | |
var uberGlobalize = (function(){ | |
var cloudFlareCDN = "//cdnjs.cloudflare.com/ajax/libs/globalize/0.1.1/cultures", | |
currentCulture = "", | |
previousCulture = "", | |
getCurrencySymbol = function( culture ) { | |
// Defaults to USD buck symbol | |
var culturedSymbol = "$", | |
tmp = culture.numberFormat | |
&& culture.numberFormat.currency | |
&& culture.numberFormat.currency.symbol; | |
tmp && ( culturedSymbol = tmp ); | |
return culturedSymbol; | |
}; | |
return function( cultureCode ) { | |
$.ajax( { | |
"url": cloudFlareCDN + "/globalize.culture." + cultureCode + ".min.js", | |
"dataType": "script", | |
"success": function( data ) { | |
// Cache the current culture code | |
currentCulture = cultureCode; | |
// Store previous culture | |
previousCulture = Globalize.culture(); | |
// Set the new culture | |
Globalize && Globalize.culture( currentCulture ); | |
// Cache some values | |
var updateAttempts = 0, | |
delayInterval = 100, | |
currencyRegex = /\$/g, | |
// Acquire new currency symbol | |
newCurrencySymbol = getCurrencySymbol( Globalize.culture() ), | |
replaceCurrencySymbols = function( $elem ) { | |
var nodes = [], | |
root = $elem.get( 0 ), | |
node = root.childNodes[0]; | |
// Collect all text nodes | |
while ( node != null ) { | |
if ( node.nodeType === 3 ) { | |
nodes.push( node ); | |
} | |
if ( node.hasChildNodes() ) { | |
node = node.firstChild; | |
} else { | |
while ( node != null && node.nextSibling == null && node != root ) { | |
node = node.parentNode; | |
} | |
node && ( node = node.nextSibling ); | |
} | |
} | |
// Iterate through all text nodes | |
for ( var i = 0, l = nodes.length; i < l; i++ ) { | |
// Replace any currency symbols | |
nodes[ i ].nodeValue = nodes[ i ].nodeValue.replace( currencyRegex, newCurrencySymbol ); | |
} | |
}; | |
setTimeout( function poller() { | |
var $grid; | |
if ( ++updateAttempts > 250 ) { | |
return; | |
} | |
if ( ( updateAttempts % 25 ) === 0 ) { | |
delayInterval += 100; | |
} | |
$grid = $( ".ontraport_grid_offer" ); | |
$grid.length === 0 ? | |
setTimeout( poller, delayInterval ) : | |
replaceCurrencySymbols( $grid ); | |
}, delayInterval ); | |
}, | |
"error": function( data ) { | |
console.warn( "Failed to set the culture... please verify that the cultureCode is available from http://cdnjs.com/libraries/globalize" ); | |
} | |
} ); | |
}; | |
})(); | |
// Load and set the culture | |
var oldOnload = window.onload; | |
window.onload = function(){ | |
jQuery && jQuery( document ).ready(function(){ | |
if ( !window.Globalize ) { | |
$.getScript( "//cdnjs.cloudflare.com/ajax/libs/globalize/0.1.1/globalize.min.js" ).done( function(){ | |
// A list of cultures available to this script/workaround is at: | |
// -> http://cdnjs.com/libraries/globalize | |
// A list of cultures to try and load is available on the left-most column of: | |
// -> http://download1.parallels.com/SiteBuilder/Windows/docs/3.2/en_US/sitebulder-3.2-win-sdk-localization-pack-creation-guide/30801.htm | |
// Common country codes: | |
// AUSTRALIA: en-AU | |
// GREAT BRITAIN: en-GB | |
uberGlobalize( "en-GB" ); | |
} ); | |
} | |
}); | |
oldOnload && oldOnload(); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment