Problem in using std::locale on unix system.

Discussion in 'C++' started by cris, Nov 28, 2006.

  1. cris

    cris Guest

    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.
    cris, Nov 28, 2006
    #1
    1. Advertising

  2. cris

    Ondra Holub Guest

    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.
    Ondra Holub, Nov 28, 2006
    #2
    1. Advertising

  3. 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
    Darko Miletic, Nov 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,817
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    4
    Views:
    980
  3. Gabriel Genellina
    Replies:
    0
    Views:
    679
    Gabriel Genellina
    Feb 18, 2009
  4. zade
    Replies:
    1
    Views:
    591
    James Kanze
    Mar 5, 2010
  5. Sibylle Koczian
    Replies:
    2
    Views:
    1,087
    Sibylle Koczian
    Nov 20, 2010
Loading...

Share This Page