By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,572 Members | 2,949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,572 IT Pros & Developers. It's quick & easy.

Problem in using std::locale on unix system.

P: n/a
Hi,

I got trouble when I try to change the locale used in a program foo.cc
// ----------------- begin
#include <iostream>
#include <locale>
#include <exception>
using namespace std;

int main(int argc, char** argv)
{
try{
locale l(argv[1]);
cout.imbue(l);
cout << "Set locale to " << l.name() << " works" << endl;
}
catch (std::runtime_error& e)
{
cout << argv[1] << " : " << e.what() << endl;
}
}

// ----------------- end
g++ -o foo foo.cc

I try to set each locale on the locale unix machine :

for loc in `locale -a`; do ./foo $loc; done

Then some locale works, some not :
Set locale to C works
common : locale::facet::_S_create_c_locale name not valid
en_US.UTF-8 : locale::facet::_S_create_c_locale name not valid
Set locale to C works
iso_8859_1 : locale::facet::_S_create_c_locale name not valid
iso_8859_15 : locale::facet::_S_create_c_locale name not valid
en_CA : locale::facet::_S_create_c_locale name not valid
en_CA.ISO8859-1 : locale::facet::_S_create_c_locale name not valid
en_US : locale::facet::_S_create_c_locale name not valid
en_US.ISO8859-1 : locale::facet::_S_create_c_locale name not valid
en_US.ISO8859-15 : locale::facet::_S_create_c_locale name not valid
en_US.ISO8859-15@euro : locale::facet::_S_create_c_locale name not
valid
fr_CA : locale::facet::_S_create_c_locale name not valid
fr_CA.ISO8859-1 : locale::facet::_S_create_c_locale name not valid

If I change compiler from g++ to CC, more locale are allowed.

Set locale to C works
common : System does not recognize this locale name
Set locale to en_US.UTF-8 works
Set locale to C works
iso_8859_1 : System does not recognize this locale name
iso_8859_15 : System does not recognize this locale name
Set locale to en_CA works
Set locale to en_CA.ISO8859-1 works
Set locale to en_US works
Set locale to en_US.ISO8859-1 works
Set locale to en_US.ISO8859-15 works
Set locale to en_US.ISO8859-15@euro works
Set locale to fr_CA works
Set locale to fr_CA.ISO8859-1 works

.... Ok for iso_8859_15, the library
/usr/lib/locale/iso_8859_1/iso_8859_15.so.2 does not exists,
but the lib /usr/lib/locale/iso_8859_1/iso_8859_1.so.2 exists.

Why is there differences between two compilers ?
Why a locale is not accepted, despite an existing library ?

thanks for your help.

Nov 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

cris napsal:
Hi,

I got trouble when I try to change the locale used in a program foo.cc
// ----------------- begin
#include <iostream>
#include <locale>
#include <exception>
using namespace std;

int main(int argc, char** argv)
{
try{
locale l(argv[1]);
cout.imbue(l);
cout << "Set locale to " << l.name() << " works" << endl;
}
catch (std::runtime_error& e)
{
cout << argv[1] << " : " << e.what() << endl;
}
}

// ----------------- end
g++ -o foo foo.cc

I try to set each locale on the locale unix machine :

for loc in `locale -a`; do ./foo $loc; done

Then some locale works, some not :
Set locale to C works
common : locale::facet::_S_create_c_locale name not valid
en_US.UTF-8 : locale::facet::_S_create_c_locale name not valid
Set locale to C works
iso_8859_1 : locale::facet::_S_create_c_locale name not valid
iso_8859_15 : locale::facet::_S_create_c_locale name not valid
en_CA : locale::facet::_S_create_c_locale name not valid
en_CA.ISO8859-1 : locale::facet::_S_create_c_locale name not valid
en_US : locale::facet::_S_create_c_locale name not valid
en_US.ISO8859-1 : locale::facet::_S_create_c_locale name not valid
en_US.ISO8859-15 : locale::facet::_S_create_c_locale name not valid
en_US.ISO8859-15@euro : locale::facet::_S_create_c_locale name not
valid
fr_CA : locale::facet::_S_create_c_locale name not valid
fr_CA.ISO8859-1 : locale::facet::_S_create_c_locale name not valid

If I change compiler from g++ to CC, more locale are allowed.

Set locale to C works
common : System does not recognize this locale name
Set locale to en_US.UTF-8 works
Set locale to C works
iso_8859_1 : System does not recognize this locale name
iso_8859_15 : System does not recognize this locale name
Set locale to en_CA works
Set locale to en_CA.ISO8859-1 works
Set locale to en_US works
Set locale to en_US.ISO8859-1 works
Set locale to en_US.ISO8859-15 works
Set locale to en_US.ISO8859-15@euro works
Set locale to fr_CA works
Set locale to fr_CA.ISO8859-1 works

... Ok for iso_8859_15, the library
/usr/lib/locale/iso_8859_1/iso_8859_15.so.2 does not exists,
but the lib /usr/lib/locale/iso_8859_1/iso_8859_1.so.2 exists.

Why is there differences between two compilers ?
Why a locale is not accepted, despite an existing library ?

thanks for your help.
Hi. I do not know, which compiler is under your CC. I have only one
idea. This compiler may have some own set of locales. Maybe is it
nonsense, I do not know.

I tried your code on SUSE Linux10.1/64-bit. (I have to add #include
<stdexcept>, but I do not think it has anything with your problem). I
got about 350 working locales and only one not working (some japanese -
I probably do not have installed all necessary stuff for it). I have no
other Unix here, sorry.

Nov 28 '06 #2

P: n/a
cris wrote:
Hi,

I got trouble when I try to change the locale used in a program foo.cc
You need to check documentation of your C library function setlocale.
std::locale will use the same names as setlocale.

On linux with glibc locale names are formed like this:

<language_short_name>_<country short name>[@charset]

en_US
en_GB
es_ES
es_AR

Nov 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.