473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can not `setlocale(3)' more than once in Linux

Hi,

I am in Linux writing a program using setlocale(3). But I found, only
the first invocation of setlocale(3) can be success, any subsequent
calling of this function will fail $B!J(B return NULL ) and the locale is
not changed. Is there any tip here?

Thanks.
#define _(String) gettext (String)

static void init_lc( const char* lc )
{
setlocale( LC_ALL, "" );
bindtextdomain( "hello", "/usr/local/share/locale" );
textdomain( "hello" );
}

int main( void )
{
init_lc( "" );

printf( _("Current locale is %s\n"), setlocale( LC_ALL, NULL ) );
printf( _("press 'e' to select English, 'c' to select Simplified
Chinese: ") );

int c;
while ( true ) {
c = getch();
if ( c == 'e' )
setlocale( "en_US" );
else if ( c == 'c' )
setlocale( "zh_CN" );
printf( _("press 'e' to select English, 'c' to select
Simplified Chinese: ") );
printf( _("Hello, world!\n") );
}

return 0;
}
Jun 27 '08 #1
7 2622
Steven Woody <na********@gma il.comwrites:
I am in Linux writing a program using setlocale(3). But I found, only
the first invocation of setlocale(3) can be success, any subsequent
calling of this function will fail ( return NULL ) and the locale is
not changed. Is there any tip here?
All I can say is that is not the case on my system. I can call
setlocale as often as I like. The program you posted should not have
compiled and certainly would not have been able to set the locale
since all but on of the calls was badly formed so it is not clear to me
how you even know there is a problem.

Although the function is standard, the standard says very little about
what locale strings mean, so you will need to post in, say,
comp.unix.progr ammer to get more help. Before you do, prepare a small
example that illustrates the problem you are having. The solution may
be simple.

--
Ben.
Jun 27 '08 #2
On 25 Apr 2008 at 7:37, Steven Woody wrote:
I am in Linux writing a program using setlocale(3). But I found, only
the first invocation of setlocale(3) can be success, any subsequent
calling of this function will fail ( return NULL ) and the locale is
not changed. Is there any tip here?
[snip]
if ( c == 'e' )
setlocale( "en_US" );
else if ( c == 'c' )
setlocale( "zh_CN" );
The prototype for setlocale is
char *setlocale(int category, const char *locale);

You're missing an argument.

Jun 27 '08 #3
On Apr 25, 11:55 pm, Antoninus Twink <nos...@nospam. invalidwrote:
On 25 Apr 2008 at 7:37, Steven Woody wrote:
I am in Linux writing a program usingsetlocale( 3). But I found, only
the first invocation ofsetlocale(3) can be success, any subsequent
calling of this function will fail ( return NULL ) and the locale is
not changed. Is there any tip here?
[snip]
if ( c == 'e' )
setlocale( "en_US" );
else if ( c == 'c' )
setlocale( "zh_CN" );

The prototype forsetlocaleis
char *setlocale(int category, const char *locale);

You're missing an argument.
Sorry, when I modified the original code to make it simple to show
you, I made some errors. But the original code is right and I did
supplied correct arguments. Well, below is a correct version of the
code and I made it more simple to illustrate the problem:

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

int main( void )
{
printf( "Current locale is %s\n", setlocale( LC_ALL, NULL ) );

bindtextdomain( "hellonls", "/usr/local/share/locale" );
textdomain( "hellonls" );

const char* msg = gettext( "Hello, world!" );

if ( ! setlocale( LC_ALL, "en_US" ) )
fprintf( stderr, "setlocale( en_US ) error\n" );
printf( "%s\n", msg );

if ( ! setlocale( LC_ALL, "zh_CN" ) )
fprintf( stderr, "setlocale( zh_CN ) error\n" );
printf( "%s\n", msg );

if ( ! setlocale( LC_ALL, "" ) )
fprintf( stderr, "setlocale( \"\" ) error\n" );
printf( "%s\n", msg );

return 0;
}

I found only the 3rd invocation of setlocale( ... ) can be success,
that is, I can only set locale to the system default. The calling
lines with "en_US" and "zh_CN" failed and returned NULL. So if my
system has LC_ALL=en_US, the code will print three same lines of
"Hello, world!" in english on the stdout, and If I set the LC_ALL to
zh_CN before run the code, I will got three same lines of "Hello,
world!" in Chinese.

Could you please try the code and tell me what's wrong here? You may
need to change "zh_CN" to a suitable locale to you. Thanks.

-
woody
Jun 27 '08 #4
On 27 Apr 2008 at 15:42, Steven Woody wrote:
I found only the 3rd invocation of setlocale( ... ) can be success,
that is, I can only set locale to the system default. The calling
lines with "en_US" and "zh_CN" failed and returned NULL.
The first thing you could try is is perror(NULL) when setlocale() fails,
and see if there's any useful error message.

Then, try
strace ./yourprog 2>&1 | grep locale
and see if that provides any information.

Jun 27 '08 #5
Steven Woody wrote, On 27/04/08 16:42:
On Apr 25, 11:55 pm, Antoninus Twink <nos...@nospam. invalidwrote:
>On 25 Apr 2008 at 7:37, Steven Woody wrote:
<snip>
>You're missing an argument.

Sorry, when I modified the original code to make it simple to show
you, I made some errors. But the original code is right and I did
supplied correct arguments.
This, of course, is why you should always copy and paste actual tested code.
Well, below is a correct version of the
code and I made it more simple to illustrate the problem:

#include <libintl.h>
The above header is not defined by the C standard, so it would have been
better if you provided an example which did not use it.
#include <locale.h>
#include <stdio.h>

int main( void )
{
printf( "Current locale is %s\n", setlocale( LC_ALL, NULL ) );

bindtextdomain( "hellonls", "/usr/local/share/locale" );
textdomain( "hellonls" );

const char* msg = gettext( "Hello, world!" );
The above three functions are not part of the C standard. It is possible
they are doing something to affect the results, so try again without
using them. If they are the problem then I suggest asking in a Linux
group or if that is your platform or possibly comp.unix.progr ammer.
if ( ! setlocale( LC_ALL, "en_US" ) )
fprintf( stderr, "setlocale( en_US ) error\n" );
I don't believe that errno is required to be set by setlocale, but it
might be worth replacing the above (and the other error printouts) with
calls to perror for testing just in case your implementation is helpful.
I.e.
perror( "setlocale( en_US ) error\n" );
printf( "%s\n", msg );

if ( ! setlocale( LC_ALL, "zh_CN" ) )
fprintf( stderr, "setlocale( zh_CN ) error\n" );
printf( "%s\n", msg );

if ( ! setlocale( LC_ALL, "" ) )
fprintf( stderr, "setlocale( \"\" ) error\n" );
printf( "%s\n", msg );

return 0;
}

I found only the 3rd invocation of setlocale( ... ) can be success,
that is, I can only set locale to the system default. The calling
lines with "en_US" and "zh_CN" failed and returned NULL. So if my
system has LC_ALL=en_US, the code will print three same lines of
"Hello, world!" in english on the stdout, and If I set the LC_ALL to
zh_CN before run the code, I will got three same lines of "Hello,
world!" in Chinese.

Could you please try the code and tell me what's wrong here? You may
need to change "zh_CN" to a suitable locale to you. Thanks.
Having removed the non-standard code and changed to supported locales it
works, so I suspect either one of the non-standard function calls is
breaking setlocale or you are selecting locales not supported on your
system. If you ask on a group dedicated to your implementation they may
be able to tell you how to check what locales are supported (you could
try the command "locale -a") and as I say if removing the non-standard
functions helps a group dedicated to your implementation may be able to
tell you why.
--
Flash Gordon
Jun 27 '08 #6
Steven Woody <na********@gma il.comwrites:
Sorry, when I modified the original code to make it simple to show
you, I made some errors. But the original code is right and I did
supplied correct arguments. Well, below is a correct version of the
code and I made it more simple to illustrate the problem:
<snip code>

The problem is not (now) the call to setlocale but the way your system
interprets the string it is passed. If you are using a POSIX system
(most Unix-like OSes) then post in comp.unix.progr ammer and you will
get access to lots of experts.

Here, the topic is standard C, and your problem is no longer with that
standard function but with what your system does with the string. The
only help you are likely to get here is from Antoninus Twink who is
single-handedly attempting to broaden the topicality here to include
other languages as well as Linux locale commands. Over in c.u.p you
will find lot of people who can help.

--
Ben.
Jun 27 '08 #7
On Apr 28, 2:08 am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Steven Woody <narkewo...@gma il.comwrites:
Sorry, when I modified the original code to make it simple to show
you, I made some errors. But the original code is right and I did
supplied correct arguments. Well, below is a correct version of the
code and I made it more simple to illustrate the problem:

<snip code>

The problem is not (now) the call to setlocale but the way your system
interprets the string it is passed. If you are using a POSIX system
(most Unix-like OSes) then post in comp.unix.progr ammer and you will
get access to lots of experts.

Here, the topic is standard C, and your problem is no longer with that
standard function but with what your system does with the string. The
only help you are likely to get here is from Antoninus Twink who is
single-handedly attempting to broaden the topicality here to include
other languages as well as Linux locale commands. Over in c.u.p you
will find lot of people who can help.

--
Ben.
Yes, I think I am in wrong group. I will go to comp.unix.progr ammer.
Thanks.
Jun 27 '08 #8

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

Similar topics

0
1816
by: Dariusz | last post by:
I am writing a guestbook script using MySQL, the part of the script below is a loop which executes to read from the database all the input data to print it out to screen. 1) In the $gbDate line, the $T argument displays as $T instead of the actual time in Windows. Is this a fault in Windows, or have I done something wrong? Webpage will eventually be in Unix server online. 2)
3
3820
by: transam | last post by:
Hi, I use Mandrake linux 10 with German setup, Hungarian Keyboard. My Python is python 2.3.3. The following program fails: --------------------------------------------------------- import locale loc = locale.setlocale(locale.LC_ALL) # get current locale locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name
3
3243
by: Ksenia Marasanova | last post by:
Hi, I have some problems with locale module. On my workstation, changing locale doesn't have effect: Python 2.3 (#1, Sep 13 2003, 00:49:11) on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import locale >>> locale.setlocale(locale.LC_ALL, 'nl_NL')
3
2784
by: J Trauntvein | last post by:
I was working with a co-worker the other day to work through the process of formatting numeric values by imbueing C++ iostreams with locales. His program's initialisation code had a call to setlocale("",LC_ALL) which I believe sets up locale information for the C run time library but is not supposed to effect the C++ run time. What we found, however, was that, with this function call in place, the C++ iostreams were formatting floating...
3
7098
by: Schraalhans Keukenmeester | last post by:
Hi, I run Apache 1.3.33 and PHP 5.0.4 on my Suse9.2 Linux box. If I try any of the following: setlocale (LC_TIME,'nl'); setlocale (LC_TIME,'nl_NL'); //example php.net site setlocale (LC_TIME,'NL'); setlocale (LC_TIME,'Dutch'); setlocale (LC_TIME,'nl-NL');
0
1379
by: Paul Lautman | last post by:
Last night I had a web site. This morning it was returning nothing. I traced it to a call to setlocale(). It appears that something has happened that means that any call to setlocale completely kills the site, including dumping anything that is in the output buffer. Take a look at http://www.osg-uk.com/st.php and http://www.osg-uk.com/sts.php the contents of which are shown below. Anyone got any ideas? st.php:
0
1504
by: jim | last post by:
Hello, I've got a problem with the setlocale function. This is my code (test.php) : ------------------ echo setlocale(LC_ALL, 'fr'); echo "<br />" ; echo bindtextdomain('fr-FR', './lang') ; echo "<br />" ;
0
1098
by: Michael Goerz | last post by:
Hi, From http://www.pyzine.com/Issue008/Section_Articles/article_Encodings.html#guessing-the-encoding: Why should the call locale.setlocale(locale.LC_ALL, '') only be made once? What exactly is the "sensitivity of the underlying C locale module"?
5
5712
by: yogeshmk | last post by:
I'm writing an application which is required to function with many languages and also compile on Linux & windows. Here's how I find the locale .. # include <stdio.h> # include <locale.h> int main(void) {
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.