468,234 Members | 1,699 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,234 developers. It's quick & easy.

setlocale() always returns "C"

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)
{
unsigned char* loc = NULL;
loc = setlocale(LC_MESSAGES, NULL);

puts (loc);
return 0;
}

Depending upon what string 'loc' contains, I need to write "hello
world" (or any other message) in the corrosponding language (there are
separate header files with texts in different languages coded in a
struct.

I've ran this piece of code on RedHat linux (en_US.UTF-8), Windows XP
(??) and Ubuntu linux (on which the `locale` *command* returns
"en_IN.UTF-8" which is correct). However, I observe that the function
always returns "C" as the locale.

Q1. What explains this behaviour of 'setlocale()'
Q2. Any other way to find out the currently set language on the
computer?

cheers!
--
~ yogesh kulkarni.

Aug 10 '08 #1
5 5250
yogeshmk wrote:
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)
{
unsigned char* loc = NULL;
loc = setlocale(LC_MESSAGES, NULL);

puts (loc);
return 0;
}
I've ran this piece of code on RedHat linux (en_US.UTF-8), Windows XP
(??) and Ubuntu linux (on which the `locale` *command* returns
"en_IN.UTF-8" which is correct). However, I observe that the function
always returns "C" as the locale.

Q1. What explains this behaviour of 'setlocale()'
Q2. Any other way to find out the currently set language on the
computer?
A program's locale at startup will always be "C", so that's all you're going
to get without explicitly setting it to something else.

To change the current locale you want to use something like:

setlocale(LC_MESSAGES, "");

- Huibert.

--
Okay... really not something I needed to see. --Raven
Aug 10 '08 #2
On Aug 10, 5:12*pm, Huibert Bol <huibert....@quicknet.nlwrote:
yogeshmk wrote:
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)
{
* * unsigned char* loc = NULL;
* * loc = setlocale(LC_MESSAGES, NULL);
* * puts (loc);
* * return 0;
}
I've ran this piece of code on RedHat linux (en_US.UTF-8), Windows XP
(??) and Ubuntu linux (on which the `locale` *command* returns
"en_IN.UTF-8" which is correct). However, I observe that the function
always returns "C" as the locale.
Q1. What explains this behaviour of 'setlocale()'
Q2. Any other way to find out the currently set language on the
computer?

A program's locale at startup will always be "C", so that's all you're going
to get without explicitly setting it to something else.

To change the current locale you want to use something like:

* setlocale(LC_MESSAGES, "");

*- Huibert.

--
Okay... really not something I needed to see. *--Raven
Ummm....I was under the impression that 'setlocale()' with second
param NULL will query and return the current locale of the OS.

If what you say is right, then I have to set the locale myself, but
the question is which? I don't know whether my program is running on a
computer with spanish locale settings so I need to print "Hola Terra!"
instead of "Hello World!"?

--
~yogesh kulkarni.
Aug 10 '08 #3
yogeshmk wrote:
On Aug 10, 5:12*pm, Huibert Bol <huibert....@quicknet.nlwrote:
>To change the current locale you want to use something like:

setlocale(LC_MESSAGES, "");
If what you say is right, then I have to set the locale myself, but
the question is which? I don't know whether my program is running on a
computer with spanish locale settings so I need to print "Hola Terra!"
instead of "Hello World!"?
Using an empty string (like in the example above), will set the locale
"correctly". Under POSIX systems, for example, it will use the LC_*
environment variables.

--
Okay... really not something I needed to see. --Raven
Aug 10 '08 #4
On Aug 10, 5:51*pm, yogeshmk <yogesh.m.kulka...@gmail.comwrote:
On Aug 10, 5:12*pm, Huibert Bol <huibert....@quicknet.nlwrote:
yogeshmk wrote:
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)
{
* * unsigned char* loc = NULL;
* * loc = setlocale(LC_MESSAGES, NULL);
* * puts (loc);
* * return 0;
}
I've ran this piece of code on RedHat linux (en_US.UTF-8), Windows XP
(??) and Ubuntu linux (on which the `locale` *command* returns
"en_IN.UTF-8" which is correct). However, I observe that the function
always returns "C" as the locale.
Q1. What explains this behaviour of 'setlocale()'
Q2. Any other way to find out the currently set language on the
computer?
A program's locale at startup will always be "C", so that's all you're going
to get without explicitly setting it to something else.
To change the current locale you want to use something like:
* setlocale(LC_MESSAGES, "");
*- Huibert.
--
Okay... really not something I needed to see. *--Raven

Ummm....I was under the impression that 'setlocale()' with second
param NULL will query and return the current locale of the OS.

If what you say is right, then I have to set the locale myself, but
the question is which? I don't know whether my program is running on a
computer with spanish locale settings so I need to print "Hola Terra!"
instead of "Hello World!"?

--
~yogesh kulkarni.
I anyway tried the setlocale() call again.

loc = setlocale(LC_ALL /* any LC_* catagories are ok here */, "" /
*instead of NULL */);

returns me the current locale set on my system.
Thanks for the reply.

cheers!
--
~ yogesh kulkarni.
Aug 10 '08 #5
yogeshmk wrote:
On Aug 10, 5:12*pm, Huibert Bol <huibert....@quicknet.nlwrote:
>yogeshmk wrote:
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)
{
unsigned char* loc = NULL;
loc = setlocale(LC_MESSAGES, NULL);
puts (loc);
return 0;
}
I've ran this piece of code on RedHat linux (en_US.UTF-8), Windows
XP (??) and Ubuntu linux (on which the `locale` *command* returns
"en_IN.UTF-8" which is correct). However, I observe that the
function always returns "C" as the locale.
Q1. What explains this behaviour of 'setlocale()'
Q2. Any other way to find out the currently set language on the
computer?

A program's locale at startup will always be "C", so that's all
you're going to get without explicitly setting it to something else.

To change the current locale you want to use something like:

setlocale(LC_MESSAGES, "");

- Huibert.

--
Okay... really not something I needed to see. *--Raven

Ummm....I was under the impression that 'setlocale()' with second
param NULL will query and return the current locale of the OS.

If what you say is right, then I have to set the locale myself, but
the question is which? I don't know whether my program is running on a
computer with spanish locale settings so I need to print "Hola Terra!"
instead of "Hello World!"?
You can usually get the locale being used from environment variables,
for example the LANG variable under Unix system. For more details ask
in a group targetting your system like comp.unix.programmer or
comp.os.ms-windows.programmer.win32, since C leaves it entirely upto
the implementation to assign the semantics for locales other than "C".
In a production program you'll probably want to use a tried and tested
internationalisation package rather than reinvent on your own.

Aug 10 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by build | last post: by
5 posts views Thread by Tim | last post: by
1 post views Thread by Tim | last post: by
5 posts views Thread by Dan C Douglas | last post: by
9 posts views Thread by Klaus Johannes Rusch | last post: by
3 posts views Thread by robert.szczepanski | last post: by
2 posts views Thread by =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.