打印所有std :: locale名称(Windows)

时间:2014-12-23 05:38:38

标签: c++ windows c++11 boost locale

我的程序用德语检查大写字母。

#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;
}

程序在控制台

上崩溃并出现错误
terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid

我不知道确切的语言环境字符串是什么,“de_DE.UTF-8”也不起作用。

有没有办法可以为OS支持的所有语言环境获取确切的语言环境名称字符串。可能是头文件中有一个列表,但我没有看到任何<locale>标题。

2 个答案:

答案 0 :(得分:19)

我编写了一个程序来打印所有支持的区域设置名称。

#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;
}

效果很好。

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

答案 1 :(得分:0)

@user1 以下内容可能与您的优雅代码相同。由于 C1189 编译器错误,我无法对其进行测试。

#include <Winnls.h>

#include <iostream>

#include <ostream>

using namespace std;

int size = 0;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam) {

size++;

wcout << *pStr << endl;

}

int _tmain(int argc, _TCHAR* argv[]) {

EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);

wcout << "Total " << size << " locales found." << endl;

return 0; }

相关问题