I can set a locale using the setlocale()
function. But is there any way I can get the locale and save it in a variable so that I can reset it later.
This is what I am trying to do:
str=getlocale(LC_CTYPE);
setlocale(LC_CTYPE,"en_US");
...
setlocale(LC_CTYPE,str);
How can I implement the getlocale()
function, or is there any alternative?
You can pass NULL
and use the return value. See setlocale
You can also use this function to find out the current locale by passing a null pointer as the locale argument. In this case, setlocale returns a string that is the name of the locale currently selected for category category.
Or in the C standard:
7.11.1.1 The setlocale function
....If a pointer to a string is given for
locale
and the selection can be honored, thesetlocale
function returns a pointer to the string associated with the specifiedcategory
for the new locale. If the selection cannot be honored, thesetlocale
function returns a null pointer and the program’s locale is not changed.A null pointer for
locale
causes thesetlocale
function to return a pointer to the string associated with thecategory
for the program’s current locale; the program’s locale is not changed.The pointer to string returned by the
setlocale
function is such that a subsequent call with that string value and its associated category will restore that part of the program’s locale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With