Search code examples
cmacoscocoa

Get Windows Locale code from preferred language


How can I get the windows locale code from from the first preferredLanguages?

Currently, it return me 12 (in decimal) which doesn't correspond to any windows locale (see: https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a)

#include <CoreFoundation/CoreFoundation.h>

int main() {
    CFArrayRef preferredLanguages = CFLocaleCopyPreferredLanguages();

    if (CFArrayGetCount(preferredLanguages) > 0) {
        CFStringRef firstLanguage = CFArrayGetValueAtIndex(preferredLanguages, 0);
        CFLocaleIdentifier localeIdentifier = CFLocaleCreateCanonicalLanguageIdentifierFromString(NULL, firstLanguage);
        uint32_t windowsLocaleCode = CFLocaleGetWindowsLocaleCodeFromLocaleIdentifier(localeIdentifier);

        printf("Windows locale code %d.\n", windowsLocaleCode);
    } else {
        printf("No preferred languages found.\n");
    }

    CFRelease(preferredLanguages);
    return 0;
}

Solution

  • https://ss64.com/locale.html appears more complete, and lists 12 as fr (French). Based on your very French user name, that seems to be a a very sensible result. So your code appears to work.[1] You are simply looking at the incorrect documentation, or the documentation is incomplete.


    1. But it's not correct. %d is not the correct conversion specifier for an uint32_t. You can use

      #include <inttypes.h>
      
      printf( "%" PRIu32 ".\n", windowsLocaleCode );