SetLocaleInfo() is horrid, don’t use it!


I just ran into a bug with SetLocaleInfo() use, and it pretty much reminded me that SetLocaleInfo() stinks.  Michael said it years ago and it’s still true.

The only thing it’s useful for is a “Regional and Language Options” type app, and there’s already one of those in the control panel 🙂   Some apps try to call SetLocaleInfo() to set some behavior, and then call the date/time APIs or whatever.  One app changed the currency symbol while it ran reports.  Problem is that SetLocaleInfo() changes the user preferences for every app in the user account.  Now the poor user’s going to be running some report or something and they’re records will be formatted wrong. 

Some apps try to “fix” the setting, and change it only momentarily, then change it back.  Now the other app will have a couple odd results in the middle of mostly-correct results, and it’ll be nearly impossible for them to figure out why it doesn’t work.  SetLocaleInfo() also makes things slow because apps have to reload their settings all the time.  The app that’s using SetLocaleInfo() needs to seriously wonder what happens if another app did the same thing and called SetLocaleInfo() right after you did.  Obviously that wouldn’t be what you’d expect, so please don’t do it to everyone else.

Anyway, don’t use SetLocaleInfo().  If there’s some limitation you’re trying to work around by calling SetLocaleInfo(), let me know.

-Shawn

Comments (2)

  1. Mihai says:

    "If there’s some limitation you’re trying to work around by calling SetLocaleInfo(), let me know."

    Date formatting using calendars other than the default one.

    GetDateFormat takes a DATE_USE_ALT_CALENDAR flag, but some Arabic locales have up to 8 calendars. Which one is "the alternate" one? How can one use the other 7?

    And if the default locale already uses "the alternate calendar" (say Japanese), the flag does nothing. "ALT" means "the calendar that is not Gregorian", not "the one that is not in use now". So there is no way to use the Gregorian calendar if the uses has as default another calendar.

    GetCalendarDateFormatEx is new in Vista, but already deprecated (if you believe the doc).

    MS Word uses SetLocaleInfo to get around that.

  2. Mihai says:

    “If there’s some limitation you’re trying to work around by calling SetLocaleInfo(), let me know.”

    The support for alternate calendars is spotty at best.

    GetDateFormat takes a DATE_USE_ALT_CALENDAR flag, but some Arabic locales might have up to 8 calendars, so “the alternate one” is not enough to specify what you want.

    Also, if you use LOCALE_USER_DEFAULT the function will use the calendar specified by the user in Control Panel. If he specified an alternate calendar, (let’s say the Japanese imperial calendar), then there is no way to format a date with the Gregorian calendar, because DATE_USE_ALT_CALENDAR act as “a calendar other than the Gregorian one”, no “a calendar other than the default one”.

    New API exists in Vista (GetCalendarDateFormatEx, ConvertSystemTimeToCalDateTime, etc.), but they are not in header files and are already declared deprecated on MSDN.

    It looks like even MS Word uses SetLocaleInfo to get around this limitation.