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