Search code examples
c++cwindowswinapicrt

How to get the locale name for my thread?


Say, I can set the locale from my C program using _create_locale as such:

localeUS = _create_locale(LC_ALL, "English_United States.1252");

But what I need is the opposite, i.e. to retrieve the locale name (2nd argument of the function above) for the calling thread. Any idea how to do that?

PS. I'm aware that the modern Windows uses LCIDs. I need this locale name for compatibility with older code.


Solution

  • Hopefully you can use Standard C++.

    From std::locale::name:

    Example

    #include <locale>
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::locale loc(std::locale(), new std::ctype<char>);
        std::cout << "The default locale is " << std::locale().name() << '\n'
                  << "The user's locale is " << std::locale("").name() << '\n'
                  << "A nameless locale is " << loc.name() << '\n';
    }
    

    Output:

    The default locale is C
    The user's locale is en_US.UTF8
    A nameless locale is *