9

I'm trying to learn different locales and handling unicode and I chose to make all my strings UTF-8 and I decided to run the following code but for some reason it throws an error and I cannot figure out why.. I tried every locale so far and none except "C" works.. I tried the list of locales found here: http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html

and their example:

#include <iostream>
#include <string>

int main()
{
    std::locale::global(std::locale("en_US.UTF-8"));

    return 0;
}

Results in:

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

What am I doing wrong?

I'm using Mingw-Builds gcc/g++ 4.8.1 with the latest codeblocks on Windows 8. Language is set to EN_US and sometimes I switch to EN_GB.

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • Do you have that local installed on your system? [Valid Locale Names](http://stackoverflow.com/q/1922713/14065) – Martin York Sep 08 '13 at 17:24
  • Do you have the same error with `std::locale::global(std::locale(""));` See [std::locale breakage on MacOS 10.6 with LANG=en_US.UTF-8](http://stackoverflow.com/questions/1745045/stdlocale-breakage-on-macos-10-6-with-lang-en-us-utf-8) – cpp Sep 08 '13 at 17:25
  • I'm not sure what you mean by "installed locale" but my system language is en_US and sometimes I switch it to en_GB. Using `std::locale("")` prints `C` and I don't have that error. – Brandon Sep 08 '13 at 17:25
  • Old compiler version. – SChepurin Sep 08 '13 at 17:29
  • Gcc/g++ 4.8.1 Window with the latest codeblocks. I'll add all this to the OP. – Brandon Sep 08 '13 at 17:30
  • Strange, because it runs online on 4.8.1 but fails on 4.3.2 -http://ideone.com/4bAjfv – SChepurin Sep 08 '13 at 17:46
  • `$ g++ --version g++.exe (rev0, Built by MinGW-builds project) 4.8.1 Copyright (C) 2013 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.` Still same error. I don't know why. I guess it just doesn't work. – Brandon Sep 08 '13 at 17:54
  • Isn't that just because of MinGW locale support being incomplete? – Oleg Pyzhcov Sep 08 '13 at 17:57
  • MingW on Windows? You may try Windows locale name, like ".936" (which is for Chinese). Always thought locale names also depend on the system, not on compiler/library only. Haven't tried myself. – fefe Oct 20 '13 at 13:12

1 Answers1

1

I ran into same problem last week, I wrote a program to print all supported locale names under Windows OS.

See my answer Print all std::locale names (Windows)

Locale that you are looking for is just "en-US" under Windows.

Community
  • 1
  • 1
user1
  • 4,031
  • 8
  • 37
  • 66