Export (0) Print
Expand All
Expand Minimize
2 out of 8 rated this helpful - Rate this topic

setlocale, _wsetlocale

Define the locale.

char *setlocale(
   int category,
   const char *locale 
);
wchar_t *_wsetlocale(
   int category,
   const wchar_t *locale 
);

Parameters

category
Category affected by locale.
locale
Locale name.

Return Value

If a valid locale and category are given, returns a pointer to the string associated with the specified locale and category. If the locale or category is invalid, returns a null pointer and the current locale settings of the program are not changed.

For example, the call

setlocale( LC_ALL, "English" );

sets all categories, returning only the string English_USA.1252. If all categories are not explicitly set by a call to setlocale, the function returns a string indicating the current setting of each of the categories, separated by semicolons. If the locale argument is a null pointer, setlocale returns a pointer to the string associated with the category of the program's locale; the program's current locale setting is not changed.

The null pointer is a special directive that tells setlocale to query rather than set the international environment. For example, the sequence of calls

// Set all categories and return "English_USA.1252"
setlocale( LC_ALL, "English" );
// Set only the LC_MONETARY category and return "French_France.1252"
setlocale( LC_MONETARY, "French" );
setlocale( LC_ALL, NULL );

returns

LC_COLLATE=English_USA.1252;
LC_CTYPE=English_USA.1252;
LC_MONETARY=French_France.1252;
LC_NUMERIC=English_USA.1252;
LC_TIME=English_USA.1252

which is the string associated with the LC_ALL category.

You can use the string pointer returned by setlocale in subsequent calls to restore that part of the program's locale information, assuming that your program does not alter the pointer or the string. Later calls to setlocale overwrite the string; you can use _strdup to save a specific locale string.

Remarks

Use the setlocale function to set, change, or query some or all of the current program locale information specified by locale and category. locale refers to the locality (country/region and language) for which you can customize certain aspects of your program. Some locale-dependent categories include the formatting of dates and the display format for monetary values. If you set locale to the default string for a language with multiple forms supported on your computer, you should check the setlocale return code to see which language is in effect. For example, using "chinese" could result in a return value of chinese-simplified or chinese-traditional.

_wsetlocale is a wide-character version of setlocale; the locale argument and return value of _wsetlocale are wide-character strings. _wsetlocale and setlocale behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tsetlocale setlocale setlocale _wsetlocale

The category argument specifies the parts of a program's locale information that are affected. The macros used for category and the parts of the program they affect are as follows:

LC_ALL
All categories, as listed below.
LC_COLLATE
The strcoll, _stricoll, wcscoll, _wcsicoll, strxfrm, _strncoll, _strnicoll, _wcsncoll, _wcsnicoll, and wcsxfrm functions.
LC_CTYPE
The character-handling functions (except isdigit, isxdigit, mbstowcs, and mbtowc, which are unaffected).
LC_MONETARY
Monetary-formatting information returned by the localeconv function.
LC_NUMERIC
Decimal-point character for the formatted output routines (such as printf), for the data-conversion routines, and for the nonmonetary-formatting information returned by localeconv.    In addition to the decimal-point character, LC_NUMERIC also sets the thousands separator and the grouping control string returned by localeconv.
LC_TIME
The strftime and wcsftime functions.

The locale argument is a pointer to a string that specifies the name of the locale. If locale points to an empty string, the locale is the implementation-defined native environment. A value of C specifies the minimal ANSI conforming environment for C translation. The C locale assumes that all char data types are 1 byte and that their value is always less than 256. The C locale is the only locale supported in Microsoft Visual C++ version 1.0 and earlier versions of Microsoft C/C++. Microsoft Visual C++ supports all locales listed in Language and Country/Region Strings. At program startup, the equivalent of the following statement is executed:

setlocale( LC_ALL, "C" );

The locale argument takes the following form:

locale :: "lang[_country_region[.code_page]]" 
            | ".code_page"
            | ""
            | NULL

The set of available languages, country/region codes, and code pages includes all those supported by the Win32 NLS API. The set of language and country/region codes supported by setlocale is listed in Language and Country/Region Strings.

If locale is a null pointer, setlocale queries, rather than sets, the international environment, and returns a pointer to the string associated with the specified category. The program's current locale setting is not changed. For example,

setlocale( LC_ALL, NULL );

returns the string associated with category.

The following examples pertain to the LC_ALL category. Either of the strings ".OCP" and ".ACP" can be used in place of a code page number to specify use of the user-default OEM code page and user-default ANSI code page, respectively.

setlocale( LC_ALL, "" );
Sets the locale to the default, which is the user-default ANSI code page obtained from the operating system.
setlocale( LC_ALL, ".OCP" );
Explicitly sets the locale to the current OEM code page obtained from the operating system.
setlocale( LC_ALL, ".ACP" );
Sets the locale to the ANSI code page obtained from the operating system.
setlocale( LC_ALL, "[lang_ctry]" );
Sets the locale to the language and country/region indicated, using the default code page obtained from the host operating system.
setlocale( LC_ALL, "[lang_ctry.cp]" );
Sets the locale to the language, country/region, and code page indicated in the [lang_ctry.cp] string. You can use various combinations of language, country/region, and code page. For example:
setlocale( LC_ALL, "French_Canada.1252" );
// Set code page to French Canada ANSI default
setlocale( LC_ALL, "French_Canada.ACP" );
// Set code page to French Canada OEM default
setlocale( LC_ALL, "French_Canada.OCP" );
setlocale( LC_ALL, "[lang]" );
Sets the locale to the country/region indicated, using the default country/region for the language specified, and the user-default ANSI code page for that country/region as obtained from the host operating system. For example, the following two calls to setlocale are functionally equivalent:
setlocale( LC_ALL, "English" );
setlocale( LC_ALL, "English_United States.1252" );
setlocale( LC_ALL, "[.code_page]" );
Sets the code page to the value indicated, using the default country/region and language (as defined by the host operating system) for the specified code page.

The category must be either LC_ALL or LC_CTYPE to effect a change of code page. For example, if the default country/region and language of the host operating system are "United States" and "English," the following two calls to setlocale are functionally equivalent:

setlocale( LC_ALL, ".1252" );
setlocale( LC_ALL, "English_United States.1252");

For more information see the setlocale pragma in the Preprocessor Reference.

Requirements

Routine Required header Compatibility
setlocale <locale.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_wsetlocale <locale.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_locale.c
/* Sets the current locale to "Germany" using the
 * setlocale function and demonstrates its effect on the strftime
 * function.
 */

#include <stdio.h>
#include <locale.h>
#include <time.h>

int main(void)
{
       time_t ltime;
       struct tm *thetime;
       unsigned char str[100];

       setlocale(LC_ALL, "German");
       time (&ltime);
       thetime = gmtime(&ltime);

       /* %#x is the long date representation, appropriate to
        * the current locale
        */
       if (!strftime((char *)str, 100, "%#x", 
                     (const struct tm *)thetime))
               printf("strftime failed!\n");
       else
               printf("In German locale, strftime returns '%s'\n", 
                      str);

       /* Set the locale back to the default environment */
       setlocale(LC_ALL, "C");
       time (&ltime);
       thetime = gmtime(&ltime);

       if (!strftime((char *)str, 100, "%#x", 
                     (const struct tm *)thetime))
               printf("strftime failed!\n");
       else
               printf("In 'C' locale, strftime returns '%s'\n", 
                      str);
}

Sample Output

In German locale, strftime returns 'Samstag, 9. Februar 2002'
In 'C' locale, strftime returns 'Saturday, February 09, 2002'

See Also

Locale Routines | localeconv | mblen | _mbstrlen | mbstowcs | mbtowc | _setmbcp | strcoll Functions | strftime | strxfrm | wcstombs | wctomb | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.