Print all std::locale names (Windows)

2019-02-04 18:23发布

问题:

My program checks for uppercase letters in German language.

#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/locale.hpp>

using namespace std;

int main()
{
    locale::global(locale("Germany_german"));
    //locale::global(locale("de_DE.UTF-8")); //Also tried "de_DE.UTF-8", but does not work

    string str1 = "über";
    cout << boolalpha << any_of(str1.begin(), str1.end(), boost::algorithm::is_upper()) << endl;

    string str2 = "Ää";
    cout << boolalpha << any_of(str2.begin(), str2.end(), boost::algorithm::is_upper()) << endl;

    return 0;
}

program crashes with error on console

terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid

I don't know what that exact locale string is, "de_DE.UTF-8" doesn't work as well.

Is there any way I can get exact locale name strings for all locales supported by OS. May be there is a list somewhere in header files, but I don't see anything <locale> header.

回答1:

I wrote a program to print all supported locale names.

#include <Windows.h>

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ostream>
#include <iterator>

using namespace std;

vector<wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

int _tmain(int argc, _TCHAR* argv[])
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);

    for (vector<wstring>::const_iterator str = locals.begin(); str != locals.end(); ++str)
        wcout << *str << endl;

    wcout << "Total " << locals.size() << " locals found." << endl;

    return 0;
}

Works great.

...
de
de-AT
de-CH
de-DE
de-DE_phoneb
de-LI
de-LU
...    
Total 429 locals found.