Partilhar via


_create_locale

Creates a locale object.

_locale_t _create_locale(
   int category,
   const char *locale 
);

Parameters

  • category
    Category.

  • locale
    Locale name.

Return Value

If a valid locale and category are given, returns the specified locale settings as a _locale_t object. The current locale settings of the program are not changed.

Remarks

The _create_locale function allows you to create an object that represents certain region-specific settings, for use in locale-specific versions of many CRT functions (functions with the _l suffix). The behavior is similar to setlocale, except that instead of applying the specified locale settings to the current environment, the settings are saved in a _locale_t structure that is returned. The _locale_t structure should be freed using _free_locale when it is no longer needed.

The category argument specifies the parts of the locale-specific behavior that are affected. The flags used for category and the parts of the program they affect are as shown in the following table.

  • 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 non-monetary formatting information returned by localeconv. In addition to the decimal-point character, LC_NUMERIC sets the thousands separator and the grouping control string returned by localeconv.

  • LC_TIME
    The strftime and wcsftime functions.

This function validates the category and locale parameters. If the category parameter is not one of the values given in the previous table or if locale is NULL, the function returns NULL.

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 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 _create_locale is listed in Language and Country/Region Strings. For more information about locale settings, see setlocale, _wsetlocale.

The previous name of this function, __create_locale (with two leading underscores), has been deprecated.

Requirements

Routine

Required header

_create_locale

<locale.h>

For additional compatibility information, see Compatibility in the Introduction.

Example

// crt_create_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];
       _locale_t locale;

       // Create a locale object representing the German locale
       locale = _create_locale(LC_ALL, "German");
       time (&ltime);
       _gmtime64_s(&thetime, &ltime);

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

       _free_locale(locale);

       // Create a locale object representing the default C locale
       locale = _create_locale(LC_ALL, "C");
       time (&ltime);
       _gmtime64_s(&thetime, &ltime);

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

       _free_locale(locale);
}

Sample Output

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

.NET Framework Equivalent

System::Globalization::CultureInfo Class

See Also

Reference

_free_locale

_configthreadlocale

setlocale

Locale

localeconv

_mbclen, mblen, _mblen_l

strlen, strlen_l, wcslen, wcslen_l, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l

mbstowcs, _mbstowcs_l

mbtowc, _mbtowc_l

_setmbcp

strcoll Functions

strftime, wcsftime, _strftime_l, _wcsftime_l

strxfrm, wcsxfrm, _strxfrm_l, _wcsxfrm_l

wcstombs, _wcstombs_l

wctomb, _wctomb_l