473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting locale at runtime

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
Nov 14 '05 #1
3 2383
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[i].locale, locale) == 0) {
#ifdef _WIN32
return info[i].windows_locale;
#else
return info[i].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

Nov 14 '05 #2
Torsten Bronger <br*****@physik.rwth-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
Nov 14 '05 #3
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

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim Seymour | last post by:
I have this thing I wrote called "Simple Contact Form." It's just a straight-forward contact form. (Rather than repeat myself, check out http://jimsun.linxnet.com/SCForm.html if you need to know...
4
by: Grumfish | last post by:
Is there a way to tell Java what localization to use so applications and the compiler doesn't default to the system's setting? I'm using a computer with Windows 2000 with a regional setting of...
1
by: David Lee AU | last post by:
We have an ASP.NET page that displays a date without a format specifier (in order to determine to default locale setting) and the date is shown as mm/dd/yyyy. We previously were able to set the...
6
by: Troels Arvin | last post by:
Hello, In the init-script contained in the RPMs downloadable from the PostgreSQL site (I checked the one for Fedora), an explicit locale is set before running initdb. - And the explicit locale...
3
by: robert | last post by:
Why can the default locale not be set by its true name? but only by '' ? : PythonWin 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32. (None, None) Traceback (most recent call last): File...
4
by: Matt F | last post by:
I have an application that utilizes a dataset. I've found just today, that if the regional language settings are changed at the point of where I instantiate the DataSet an error is thrown of:...
1
by: Torsten Bronger | last post by:
Hallöchen! I start a python script with subprocess from another Python script with python = subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.PIPE) However, this child script...
0
by: Gabriel Genellina | last post by:
En Tue, 26 Aug 2008 07:52:21 -0300, Robert Rawlins <robert.rawlins@thinkbluemedia.co.ukescribi�: Probably you don't have support for 'de_DE' locale. Try using locale.setlocale(locale.LC_ALL,...
5
by: nsbecker | last post by:
Hi - I've got an interesting one here: I've got an mdb file that some users run in Access 2003 and some in 2007. I have a Localize function that is called on the open of each form to set the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.