C Library Function - setlocale()
C Standard Library - <locale.h>
Description
The C library function char *setlocale(int category, const char *locale) sets or retrieves localization information.
Declaration
Here is the declaration for the setlocale() function.
char *setlocale(int category, const char *locale)
Parameters
category -- This is a named constant specifying the category of functions affected by the locale setting.
LC_ALL includes all of the below options.
LC_COLLATE for string comparison. See strcoll().
LC_CTYPE for character classification and conversion. For example, strtoupper().
LC_MONETARY for monetary formatting, used by localeconv().
LC_NUMERIC for the decimal separator, used by localeconv().
LC_TIME for date and time formatting, used by strftime().
LC_MESSAGES for system responses.
locale -- If locale is NULL or an empty string "", the locale name will be set according to the values of the environment variables, which have the same names as the above categories.
Return Value
On successful invocation of setlocale(), it returns an opaque string corresponding to the locale. If the request is invalid, the return value is NULL.
Example
The following example demonstrates the use of the setlocale() function.
#include <locale.h>
#include <stdio.h>
#include <time.h>
int main ()
{
time_t currtime;
struct tm *timer;
char buffer[80];
time( &currtime );
timer = localtime( &currtime );
printf("Locale is: %s\n", setlocale(LC_ALL, "en_GB.UTF-8"));
strftime(buffer,80,"%c", timer );
printf("Date is: %s\n", buffer);
printf("Locale is: %s\n", setlocale(LC_ALL, "de_DE.UTF-8"));
strftime(buffer,80,"%c", timer );
printf("Date is: %s\n", buffer);
return(0);
}
Let's compile and run the above program, which will produce the following result:
Locale is: en_GB
Date is: Thu 23 Aug 2012 06:39:32 MST
Locale is: de_DE
Date is: Do 23 Aug 2012 06:39:32 MST