Created
August 25, 2012 21:02
-
-
Save marcoscaceres/3470960 to your computer and use it in GitHub Desktop.
A bit of simple code to enable the full ES Internationalization API in Chrome 21+. Does not work in Chrome Canary.
This file contains 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
<h1>Examples</h1> | |
This file contains 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(window) { | |
//check for V8 implementation support in Chrome | |
if ('v8Intl' in window) { | |
//We may need to rewrite the standard functions to use the Intl formatters | |
var oldToLocaleDateString, | |
oldNumberToLocaleString, | |
oldToLocaleTimeString, | |
//we check if various API calls are supported using this date | |
date = new Date('1/1/2001'); | |
//property used in testing if 18n API is already implemented | |
props = { | |
weekday: 'long', | |
timeZoneName: 'long' | |
}; | |
//create a proxy; | |
window.Intl = v8Intl; | |
//Check if toLocaleDateString already supported | |
if (date.toLocaleDateString(['en'], props) !== 'Monday') { | |
oldToLocaleDateString = Date.prototype.toLocaleDateString; | |
Date.prototype.toLocaleDateString = function(locales, options) { | |
var formatter; | |
if (locales === undefined && options === undefined) { | |
return oldToLocaleDateString.call(this); | |
} | |
//clean up and normalize; | |
locales = String(locales).split(','); | |
options = Object(options); | |
//format and return result; | |
formatter = new window.Intl.DateTimeFormat(locales, options); | |
return formatter.format(this); | |
} | |
} | |
//check Time Formatting support | |
if(date.toLocaleTimeString(["en"], props) !== "12:00:00 AM GMT+00:00"){ | |
oldToLocaleTimeString = Date.prototype.toLocaleTimeString; | |
Date.prototype.toLocaleTimeString = function(locales, options) { | |
var formatter, defaults; | |
if (locales === undefined && options === undefined) { | |
return oldToLocaleTimeString.call(this); | |
} | |
//clean up and normalize | |
locales = String(locales).split(','); | |
options = Object(options); | |
//set defaults for output, as Chrome does not do this | |
defaults = ['hour', 'minute', 'second']; | |
for (var i = 0; i < defaults.length; i++) { | |
if (!(defaults[i] in options)) { | |
options[defaults[i]] = 'numeric'; | |
} | |
} | |
//format and return result; | |
formatter = new window.Intl.DateTimeFormat(locales, options); | |
return formatter.format(this); | |
} | |
} | |
//check number formatting support | |
if((123456).toLocaleString(["en"]) !== "123,456"){ | |
oldNumberToLocaleString = Number.prototype.toLocaleString; | |
Number.prototype.toLocaleString = function(locales, options) { | |
var formatter; | |
if (locales === undefined && options === undefined) { | |
return oldNumberToLocaleString.call(this); | |
} | |
//clean up and normalize; | |
locales = String(locales).split(','); | |
options = Object(options); | |
//format and return result; | |
formatter = new window.Intl.NumberFormat(locales, options); | |
return formatter.format(this); | |
} | |
} | |
} | |
})(window); | |
//Show examples | |
var date = new Date('1/1/2001'), | |
options = { | |
weekday: 'long' | |
}; | |
var elem = document.createElement("div"); | |
elem.innerHTML += "toLocaleDateString: " + date.toLocaleDateString() + "<br>"; | |
elem.innerHTML += "toLocaleTimeString: " + date.toLocaleTimeString() + "<br>"; | |
elem.innerHTML += "Weekday 'en': " + date.toLocaleDateString('en', options) + "<br>"; | |
elem.innerHTML += "Weekday and time 'pt': " + date.toLocaleTimeString('pt', options) + "<br>"; | |
elem.innerHTML += "Chinese Time: " + date.toLocaleTimeString('zh') + "<br>"; | |
var number = 123456789; | |
elem.innerHTML += "toLocaleString of number: " + number.toLocaleString() + "<br>"; | |
elem.innerHTML += "Number formatted for Portgal: " + number.toLocaleString('pt-PT') + "<br>"; | |
elem.innerHTML += "Number formatted for Brazil: " + number.toLocaleString('pt-BR') + "<br>"; | |
elem.innerHTML += "Number formatted for English: " + number.toLocaleString('en', {}) + "<br>"; | |
document.body.appendChild(elem); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment