Skip to content

Instantly share code, notes, and snippets.

@ray-peters
Last active February 17, 2016 21:31
Show Gist options
  • Save ray-peters/3618446832df71f66d7c to your computer and use it in GitHub Desktop.
Save ray-peters/3618446832df71f66d7c to your computer and use it in GitHub Desktop.
generic uber globalize
(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 $grid,
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() {
if ( ++updateAttempts > 250 ) {
return;
}
if ( ( updateAttempts % 25 ) === 0 ) {
delayInterval += 100;
}
$grid = $( ".ontraport_grid_offer" );
$grid.length === 0 ?
setTimeout( poller, delayInterval ) :
replaceCurrencySymbols( $grid );
}, delayInterval );
setTimeout( function setupXD(){
if ( window[ 'XD' ] === undefined ) {
setTimeout( setupXD, 50 );
return;
}
XD.receiveMessage( function( msg ) {
if ( msg.data.indexOf( '"result_code":1' ) !== -1 ) {
// Success
setTimeout( function couponPoller(){
var $couponDisplay = $grid.find( ".grid-summary-coupon-display" );
$couponDisplay.length === 0 ?
setTimeout( couponPoller, 50 ) :
replaceCurrencySymbols( $couponDisplay );
}, 50 );
}
}, "https://app.ontraport.com" );
}, 25 );
},
"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(){
var ret = $.Deferred();
if ( !window.Globalize ) {
$.getScript( "//cdnjs.cloudflare.com/ajax/libs/globalize/0.1.1/globalize.min.js" ).done( function(){
ret.resolve( _uberGlobalize );
} );
} else {
ret.resolve( _uberGlobalize );
}
window.uberGlobalize = ret;
} );
oldOnload && oldOnload();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment