Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do you find valid locale names?

I am currently using MAC OS X.
But information about other platforms would also be useful.

#include <fstream>
#include <iostream>


int main(int argc,char* argv[])
{
    try
    {
        std::wifstream  data;
        data.imbue(std::locale("en_US.UTF-16"));
        data.open("Plop");
    }
    catch(std::exception const& e)
    {
        std::cout << "Exception: " << e.what() << "
";
        throw;
    }
}

% g++ main.cpp
% ./a.out
Exception: locale::facet::_S_create_c_locale name not valid
Abort
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

This page says:

The constructor call std::locale("") creates a locale object that represents the user's preferences. The standard doesn't say what this means, but on many systems the library substitutes whatever is found in an environment variable (often LANG or LC_ALL) in place of the empty string. A common name for the American locale, for example, is "en_US". (On POSIX systems you can type locale -a to list the names of supported locales.)

locale -a should work for you.

If you mean programatically from the C++ std libary I'm not sure.

This stack overflow question is probably also relevant, but he doesn't seem to have had much response.

Edit

To use UTF-16 you probably will need to use libiconv as mentioned in this question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...