2014-12-23 37 views
9

我的程序檢查德語中的大寫字母。打印所有std ::語言環境名稱(Windows)

#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」不正常工作。

有什麼辦法,我可以得到精確的區域名稱字符串由操作系統支持的所有語言環境。可能在頭文件中有一個列表,但我沒有看到任何東西<locale>頭。

+0

您可以在命令行上執行'locale -a'。 – 0x499602D2

+0

我在Windows上 – user1

+0

「de-DE」和「German_Germany」應該可以工作。至少,你應該能夠從這些字符串構造'locale'。 –

回答

16

我寫了一個程序打印所有支持的語言環境名稱。

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

這看起來像是一個非常有用的代碼片段。感謝您分享您的答案 – sehe

相關問題