Problem in using std::locale on unix system.

C

cris

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.
 
O

Ondra Holub

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.
 
D

Darko Miletic

cris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top