Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getlocale() function in C

Tags:

c

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?

like image 369
Jahid Avatar asked Dec 03 '22 15:12

Jahid


1 Answers

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, the setlocale function returns a pointer to the string associated with the specified category for the new locale. If the selection cannot be honored, the setlocale function returns a null pointer and the program’s locale is not changed.

A null pointer for locale causes the setlocale function to return a pointer to the string associated with the category 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.

like image 129
AlexD Avatar answered Dec 17 '22 14:12

AlexD