Setting locale at runtime

Discussion in 'C Programming' started by Torsten Bronger, Apr 11, 2005.

  1. Hallöchen!

    I have to generate excerpts in different human languages in a
    program. In general, the language is not the locale's language, but
    set at runtime. Nevertheless, GNU's gettext should be used for the
    excerpts. This means that I have to wrap calls to gettext() in a
    function that sets the locale temporarily.

    The problem is how to do that. Let's assume I need to generate
    German snippets. Then I could say

    setlocale (LC_MESSAGES, "de");

    and

    setlocale (LC_MESSAGES, "");

    to reset it. However, this doesn't work on my computer. It wants
    to see "de_DE". This, however, doesn't work on another computer
    which insists on a plain "de". Even worse, I have a report of an
    American developer that on his computer none of these variants had
    any effect, although German messages were definitely installed.

    Is there a reliable way of setting the locale?

    Thank you!

    Tschö,
    Torsten.

    --
    Torsten Bronger, aquisgrana, europa vetus
     
    Torsten Bronger, Apr 11, 2005
    #1
    1. Advertising

  2. Torsten Bronger wrote:
    > Hallöchen!
    >
    > I have to generate excerpts in different human languages in a
    > program. In general, the language is not the locale's language, but
    > set at runtime. Nevertheless, GNU's gettext should be used for the
    > excerpts. This means that I have to wrap calls to gettext() in a
    > function that sets the locale temporarily.
    >
    > The problem is how to do that. Let's assume I need to generate
    > German snippets. Then I could say
    >
    > setlocale (LC_MESSAGES, "de");
    >
    > and
    >
    > setlocale (LC_MESSAGES, "");
    >
    > to reset it. However, this doesn't work on my computer. It wants
    > to see "de_DE". This, however, doesn't work on another computer
    > which insists on a plain "de". Even worse, I have a report of an
    > American developer that on his computer none of these variants had
    > any effect, although German messages were definitely installed.
    >
    > Is there a reliable way of setting the locale?
    >
    > Thank you!
    >
    > Tschö,
    > Torsten.
    >
    > --
    > Torsten Bronger, aquisgrana, europa vetus


    I don't know how gnu's gettext works, asking in a gnu newsgroup would
    probably get you help there if there are things you might do to avoid
    setlocale.

    As to your question of setting a locale, the answer is not really.
    Since the locale string's content is implementation dependent, you need
    to cater to all implementations you want to support. There are
    numerous ways you might do that. For example, you could have a
    configuration file that has the locale strings for that implementation
    for the languages you support, say one config file per implementation.
    For example, you might have in your file parameters of the form
    LOCALE_STRING_<ISO 2 letter country code><ISO 2 letter language code>

    For one implementation
    LOCALE_STRING_DEAT = de_AT

    For a different one
    LOCALE_STRING_DEAT = de_AT.iso885915@euro

    And for yet another (ugh)
    LOCALE_STRING_DEAT = german-austrian_aut.1252

    Or you could put this in your code. For example, something like this:
    typedef struct locale_info {
    const char* locale;
    const char* windows_locale;
    const char* unix_locale; /* split as needed if different supported
    unices differ */
    } locale_info;

    static locale_info info[] = {
    { "deat", "german-austrian_aut.1252", "de_AT" },
    { "dech", "german-swiss_che.1252", "de_CH" }
    };

    const char *get_locale(const char *locale)
    {
    unsigned i;
    for (i = 0; i < sizeof info/sizeof info[0]; i++) {
    if (strcmp(info.locale, locale) == 0) {
    #ifdef _WIN32
    return info.windows_locale;
    #else
    return info.unix_locale;
    #endif
    }
    }
    return NULL;
    }

    In this method as in the param method, you need some platform neutral
    way of specifying the language (e.g. ISO 2 letter language and country
    code). I think this isn't as nice as the config file approach. There
    are probably other ideas as well, but these might help get you
    started...

    -David
     
    David Resnick, Apr 11, 2005
    #2
    1. Advertising

  3. Torsten Bronger

    Ben Pfaff Guest

    Torsten Bronger <-aachen.de> writes:

    > Is there a reliable way of setting the locale?


    Unfortunately, no. Names of locales are system dependent from
    the viewpoint of standard C.
    --
    "The fact that there is a holy war doesn't mean that one of the sides
    doesn't suck - usually both do..."
    --Alexander Viro
     
    Ben Pfaff, Apr 11, 2005
    #3
  4. Torsten Bronger wrote:
    > ... Let's assume I need to generate German snippets. Then I could
    > say
    >
    > setlocale (LC_MESSAGES, "de");
    >
    > and
    >
    > setlocale (LC_MESSAGES, "");
    >
    > to reset it. However, this doesn't work on my computer. It wants
    > to see "de_DE". This, however, doesn't work on another computer
    > which insists on a plain "de". Even worse, I have a report of an
    > American developer that on his computer none of these variants
    > had any effect, although German messages were definitely
    > installed.
    >
    > Is there a reliable way of setting the locale?


    You could make your program accept a runtime preference, either
    via an environment variable, or via command line argument, or
    something else.

    #include <stdio.h>
    #include <locale.h>

    int main(int argc, char **argv)
    {
    char *x;

    if (argc == 2 && argv[1][0] != 0)
    {
    printf("setlocale(LC_ALL, \"%s\") : ", argv[1]);
    x = setlocale(LC_ALL, argv[1]);
    puts(x ? "ok" : "failed");
    }

    return 0;
    }

    --
    Peter
     
    Peter Nilsson, Apr 12, 2005
    #4
    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. Alex Polite
    Replies:
    1
    Views:
    1,439
    Alan Moore
    Jun 5, 2004
  2. Maurice Hulsman
    Replies:
    1
    Views:
    1,916
    Guus Bosman
    Jul 25, 2004
  3. napo
    Replies:
    13
    Views:
    7,726
    chaitanyat2001
    Sep 23, 2008
  4. Sibylle Koczian
    Replies:
    2
    Views:
    1,156
    Sibylle Koczian
    Nov 20, 2010
  5. Vlastimil Brom
    Replies:
    0
    Views:
    988
    Vlastimil Brom
    Dec 9, 2010
Loading...

Share This Page