Getting all of the alternate sorts for a locale

by Michael S. Kaplan, published on 2005/11/25 04:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2005/11/25/494976.aspx


Recently, Eman asked in the newsgroups:

Please help me clear up general LCID logic.

As far as i understand the SORT_xx values make sense in a pair with PrimaryLanguage only. In other words, a Sort ID has the same meaning for a set of LCIDs where PrimaryLanguage is the same but SubLanguages differ. Is this correct?

I had to be the bearer of bad news, unfortunately:

No, that is not the case.

Sort ID's mske sense with specific LANGID values for which there are supported sorts. It has nothing to do with primary language ID alone.

And this is an important distinction -- for example, 0x0407 is German - Germany, and 0x0807 is German - Switzerland. You can get them by using:

MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN)

MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN_SWISS)

but if you wanted to use German phone book alternate sort, then only the following will work:

MAKELCID(MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN), SORT_GERMAN_PHONE_BOOK)

because it is not defined for any of the other German language locales.

Eman then asked a follow-up question:

Thank you for answer.

So, to let the user choose a sort within specific LangID i can EnumSystemLocales with LCID_INSTALLED | LCID_ALTERNATE_SORTS, filtering out items with the LangID.

Please correct if i am wrong again.

Now this answer is not wrong, but it is a lot more work than is needed. To get the list of alternate sorts available within a specific LANGID there are two methods:

  1. Call EnumSystemLocales with the LCID_ALTERNATE_SORTS flag, and then filter for all of the ones where LANGIDFROMLCID returns the LANGID you are looking for.
  2. Call IsValidLocale for every LCID value of MAKLCID(langid, n) where n is from 0x1 to 0xf, and wny time it is valid then you know you have found an alternate sort for the langid.

Which method to choose? Well at the moment method #1 may perhaps seem faster since there are way fewer than 15 alternate sorts across all locales, but method #1 returns strings and converting all of those strings to numbers so you can compare them in the locale macros may be more expensive than comparing 15 numbers. Both operations are fast enough that it is one of those silly interview questions to make a candidate answer as to which method is better.

Personally, I would choose method #2 since the future could have more alternate sorts in it (one never knows!) and I like to avoid dealing with strings when I can. But if that were an interview question I were given, I would be sure not to take that particular job.... :-)

 

This post brought to you by "Ɣ" (U+0194, a.k.a. LATIN CAPITAL LETTER GAMMA)


# Richard on 30 Nov 2005 7:23 AM:

> This post brought to you by

I don't have (not being from Microsoft) the Segoe UI typeface, therefore the Gamma does not show up (in IE, does in FF, so looks like FF is doing some additional defaulting here if none of the fonts specified are available).

# Tatiana Racheva on 24 Apr 2008 12:41 PM:

So for Vista & later, sounds like one should use EnumSystemLocalesEx, because it will also include Traditional Spanish sort when you use LOCALE_ALTERNATE_SORTS. It seems like you still have to make a special case for it, because you can't derive the same language ID from the two LCIDs...


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2005/11/26 Technically it *is* a hungarian sort

go to newer or older post, or back to index or month or day