1

I want to store the locale name and code page appended to it in a buffer, what should be the ideal size of the buffer?.

From winnt.h

// Maximum Locale Name Length in Windows
// Locale names are preferred to the deprecated LCID/LANGID concepts.
//
// Locale names should follow the BCP47 recommendations and typically
// include language, script, regional variant, and perhaps additional specifiers.
// BCP47 allows some variation, eg: en-US is preferred to en-Latn-US.
#define LOCALE_NAME_MAX_LENGTH   85

As we can see LOCALE_NAME_MAX_LENGTH is for locale name without code page, eg: "en-US", But I want to store "en-US.1252", "English_United States.1252" etc..

What is the standard maximum length allowed for the code page?

Actually I want to do the following. I am using an array of 128 wide characters, I want to know what is the optimal size for the same?

    size_t      buffer_size;
    wchar_t     wc_locale_name[128];
    wchar_t     buffer[128];
    wchar_t     wc_full_locale_name[128];
    
    buffer_size = _countof(buffer);
    wmemset(wc_locale_name, 0, buffer_size);
    wmemset(buffer, 0, buffer_size);
    wmemset(strbuf, 0, buffer_size);
    
    MultiByteToWideChar(CP_ACP, 0, "en-US", len, wc_locale_name,
                    LOCALE_NAME_MAX_LENGTH);
    if (GetLocaleInfoEx(wc_locale_name, LOCALE_SENGLISHLANGUAGENAME, buffer,
        buffer_size))
    {
        len = swprintf(wc_full_locale_name, buffer_size, L"%ls", buffer);
        wmemset(buffer, 0, buffer_size);
        if (GetLocaleInfoEx(wc_locale_name, LOCALE_SENGLISHCOUNTRYNAME,
            buffer, buffer_size))
        {
            len += swprintf(wc_full_locale_name + len, buffer_size - len, L"_%ls",
                buffer);
            wmemset(buffer, 0, buffer_size);
            if (GetLocaleInfoEx(wc_locale_name, LOCALE_IDEFAULTANSICODEPAGE,
                buffer, buffer_size))
            {
                len = swprintf(wc_full_locale_name + len, buffer_size - len, L".%ls",
                    buffer);
            }
    }
}
confucius_007
  • 186
  • 1
  • 15
  • 1
    What's the rationale behind appending a code page to a locale name? – IInspectable Jul 02 '20 at 09:33
  • 1
    The Windows codepage is a `UINT` which is no greater than `UINT_MAX`. – dxiv Jul 03 '20 at 06:39
  • 1
    @IInspectable: In Linux the environment variable `LANG` takes the form `.`, for example mine is now `en_US.UTF-8`. I've seen some Linux apps ported to Windows that expect `LANG` constructed similarly but with Windows locale names: `English_United States.1252`. – rodrigo Jul 03 '20 at 06:41

0 Answers0