【问题标题】:Print all std::locale names (Windows)打印所有 std::locale 名称 (Windows)
【发布时间】:2015-02-21 06:13:24
【问题描述】:

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

#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”也不行。

有什么方法可以获取操作系统支持的所有语言环境的准确语言环境名称字符串。可能在头文件的某个地方有一个列表,但我没有看到任何 &lt;locale&gt; 头文件。

【问题讨论】:

  • 您可以在命令行上执行locale -a
  • "de-DE" 和 "German_Germany" 应该可以工作。至少,您应该能够从这些字符串构造locale
  • 关于枚举语言环境,见EnumSystemLocalesEx
  • @IgorTandetnik 我刚刚尝试了您的建议。我无法构建语言环境,编译器在 locale::global 行上抛出异常。

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


【解决方案1】:

我编写了一个程序来打印所有支持的语言环境名称。

#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.

【讨论】:

  • 这看起来像是一个非常有用的 sn-p 代码。感谢您分享您的答案
  • 有趣,由于某种原因我的编译器找不到EnumSystemLocalesEx。尽管我包括windows.h
  • @Julien 看起来 API 有一些变化。尝试包括 Winnls.h
  • @user1 没关系;无论出于何种原因,我的本地 MinGW 不支持“EnumSystemLocalesEx”,因为在他们的开发文件中的任何地方都找不到它。
  • 尝试使用 进行编译。在 的第 173 行收到 C1189 错误“#error: 'No Target Architecture'”。代码很有趣,希望我可以使用它,但我仍然需要理解和实现一些允许 put_money 工作的东西。我确实尝试使用 locale("en-US" ) 但没有成功。从 cplusplus.com 和其他网站,我相信 locale mylocale("") 应该默认为区域语言环境,在我的例子中是美国。但是,它不起作用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多