Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in using std::locale on unix system.

Reply
Thread Tools

Problem in using std::locale on unix system.

 
 
cris
Guest
Posts: n/a
 
      11-28-2006
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.

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      11-28-2006

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.

 
Reply With Quote
 
 
 
 
Darko Miletic
Guest
Posts: n/a
 
      11-30-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Borland C++ Builder under Windows AND Unix? Ramon F Herrera C++ 4 02-05-2005 02:00 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
ASP.NET service providers using Samba+Unix Neil Zanella ASP .Net 3 01-06-2005 04:43 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM



Advertisments