Freigeben über


How do I get localization in JScript (JavaScript) in IE7?

I ran into an interesting problem with a Vista sidebar calendar gadget.  It seems that it is not exactly trivial to find out interesting NLS data like localized month names, first day of week, etc. from JScript.

Eventually I found a workaround for some of the data.  VBScript has MonthName, WeekDayName and WeekDay functions that can be used for some of the data I was looking for.  These can be wrapped in VBScript functions which JScript can then call from the same page.  I calculated FirstDayOfWeek information by using the WeekDay function for a known date.

These are the helpers that I ended up with:

 <script language="VBscript">
' Helper functions so we can do this in JScript as well
Function MyMonthName(month)
   MyMonthName = MonthName(month)
End Function
Function MyAbbrevMonthName(month)
   MyAbbrevMonthName = MonthName(month, True)
End Function
Function MyDayName(day)
   MyDayName = WeekDayName(day)
End Function
Function MyAbbrevDayName(day)
   MyAbbrevDayName = WeekDayName(day, True)
End Function
' Get First Day of Week where 1 is Sunday, 2 is Monday, etc.
Function FirstDayOfWeek()
   Dim MyDate
   MyDate = DateSerial(2002, 9, 1) ' A Sunday
   FirstDayOfWeek = (9 - WeekDay(MyDate, 0))
   if FirstDayOfWeek = 8 then FirstDayOfWeek = 1
End Function</script>

Unfortunately some items like Shortest Day Names and Month/Year patterns can't be retrieved this way, but I was able to solve most of my problem.  The attached CalendarData.html uses these helper functions to dump the values I was interested in using both VBScript and JScript.

I also embedded the HTML here, so here's the output, hope this is interesting:

CalendarData.html

Comments