17

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.

user1
  • 4,031
  • 8
  • 37
  • 66

2 Answers2

26

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.
user1
  • 4,031
  • 8
  • 37
  • 66
  • 2
    This looks like a pretty useful snippet of code. Thanks for sharing your answer – sehe Dec 23 '14 at 10:36
  • Interesting, for some reason my compiler cannot find `EnumSystemLocalesEx`. Altough I include `windows.h`. –  Apr 27 '18 at 09:50
  • 1
    @Julien Looks like there is some change in API. Try including Winnls.h – user1 Apr 28 '18 at 05:47
  • @user1 Nevermind; for whatever reasons, my local MinGW does not support 'EnumSystemLocalesEx', as it cannot be found anywhere in their development files. –  May 30 '18 at 05:18
  • Tried compiling with . Receive a C1189 error "#error: 'No Target Architecture'" on line 173 of . The code is interesting, wish I could use it, but I still need to understand and implement something that allows put_money to work. I did try to use locale("en-US" ) with no success. From cplusplus.com and other sites, I believe locale mylocale("") should default to the regional locale, in my case the US. But, it doesn't work. – lostbits May 12 '21 at 20:21
  • 2
    Hi @user1 for me the code ran when I made 2 updates, 1. #include , 2. Change second null, EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, 0, NULL); – Sumeet Singh May 07 '23 at 01:06
-1

@user1 The following might do the same as your elegant code. I can't test it because of the C1189 compiler error.

#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;
}
Chnossos
  • 9,971
  • 4
  • 28
  • 40
lostbits
  • 928
  • 1
  • 9
  • 23