I am trying to understand how the JavaScript running in the various browsers determines the locale that will be used by the JavaScript Date
object when using the method t开发者_JS百科oLocaleString()
.
I have changed the language and locale settings of my system through Windows control panel and I have also changed every browser's language settings to the same. The function still returned an English date.
Is there a registry setting on windows that you have to manually set? Did I miss some setting somewhere? Or do I have to get a Windows install disc for particular regions?
A great article from one of my favourite blogs on JScript's behavior:
I believe we are now in "always default to US-English" mode in IE. It's confusing because the script engines have the ability to change the locale used for the error messages independent of the locale used to format dates, numbers, etc.
and
Date.prototype.toLocaleDateString, toLocaleTimeString, and toLocaleString are complicated by some bizarre weirdnesses in the Win32 NLS API. To work around various problems, only dates between 1600 and 10000 AD are localized. Hebrew date formats for years after 2240 AD are also not supported. Once we jump through those hurdles, the Win32 APIs GetDateFormat and GetTimeFormat are used to format the strings. (I'm vaguely recalling that there was also a bug in there involving the Thai calendar but I don't remember the details.)
https://blogs.msdn.com/ericlippert/archive/2004/05/18/jscript-localization-and-those-wacky-newfoundlanders.aspx
And from the MDC (Mozilla Developer Center):
The toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000, toLocaleString returns a string that is not year-2000 compliant. toLocaleString behaves similarly to toString when converting a year that the operating system does not properly format.
It is from the timezone setting from the system clock, not locale.
[Edit] Now that I reread the question, you are talking about date formatting aren't you? (15 Feb vs. Feb 15) Here is a reference about the method for Firefox: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date/toLocaleString. It says that it uses the operating system's locale.
It's possible you might have to reboot the operating system for the locale changes to take effect.
While the browser implementation typically would depend on OS routines to do the formatting, I do not think there is anything in the specification to determine how the formatting occurs or what the output is, only that it is appropriate for the locale.
I think the question was about how the locale is determined. At least Firefox treats this as a fixed value depending on the installation. It cannot be changed through OS or even browser preferences (the language/regional preferences you see in Firefox relate to a different locale setting used only in HTTP transactions)
To actually control date formatting in arbitrary locales besides what your browser assumes to be the installed locale, you'd need a JS library like jsdate or dojo.date.
精彩评论