_create_locale_wcreate_locale

创建区域设置对象。

语法

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

参数

category
类别。

locale
区域设置说明符。

返回值

如果给定了有效的 localecategory,则函数返回指定区域设置作为 _locale_t 对象。 不更改程序的当前区域设置。

注解

_create_locale 函数允许创建表示某个特定于区域的设置的对象,以供许多特定于区域设置版本的 CRT 函数(具有 _l 后缀的函数)使用。 行为类似于 setlocale但将设置保存在返回的 _locale_t 结构中,而不是向当前环境应用指定的区域设置。 不再需要 _locale_t 结构时,应使用 _free_locale 将其释放。

_wcreate_locale_create_locale的宽字符版本; locale_wcreate_locale 参数是宽字符字符串。 除此以外,_wcreate_locale_create_locale 的行为完全相同。

category 参数指定受影响的特定于区域设置的行为部分。 用于 category 的标志及其影响的程序部分如下表中所示:

category 标志 影响
LC_ALL 以下列出了所有类别。
LC_COLLATE strcoll_stricollwcscoll_wcsicollstrxfrm_strncoll_strnicoll_wcsncoll_wcsnicollwcsxfrm 函数。
LC_CTYPE 字符处理函数(不受影响的 isdigitisxdigitmbstowcsmbtowc 除外)。
LC_MONETARY localeconv 函数返回的货币格式信息。
LC_NUMERIC 格式化输出例程(例如 printf)、数据转换例程和 localeconv 所返回的非货币格式设置信息的十进制点字符。 除小数点字符之外,LC_NUMERIC 还设置 localeconv 返回的千位分隔符和分组控制字符串。
LC_TIME strftimewcsftime 函数。

此函数验证 categorylocale 参数。 如果类别参数不是上表中给定的值之一,或者如果 localeNULL,则该函数将返回 NULL

locale 参数是指向指定区域设置的字符串的指针。 有关 locale 参数的格式信息,请参阅区域设置名称、语言和国家/地区字符串

locale 参数可以采用多种类型的值:区域设置名称、语言字符串、语言字符串和国家/地区代码、代码页或语言字符串、国家/地区代码和代码页的组合。 可用的区域设置名称、语言、国家/地区代码和代码页集合包括 Windows NLS API 支持的所有内容。 区域设置名称、语言和国家/地区字符串中介绍了 _create_locale 支持的区域设置名称集合。 语言字符串国家/地区字符串中列出了 _create_locale 所支持的语言和国家/地区字符串集合。

有关区域设置的详细信息,请参阅 setlocale_wsetlocale

已弃用此函数之前的名称 __create_locale(带两个前导下划线)。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

例程 必需的标头
_create_locale <locale.h>
_wcreate_locale <locale.h> 或 <wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_create_locale.c
// Sets the current locale to "de-CH" 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 (Switzerland) locale
    locale = _create_locale(LC_ALL, "de-CH");
    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 de-CH 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);
}
In de-CH locale, _strftime_l returns 'Samstag, 9. Februar 2002'
In 'C' locale, _strftime_l returns 'Saturday, February 09, 2002'

另请参阅

区域设置名称、语言和国家/地区字符串
语言字符串
国家/地区字符串
_free_locale
_configthreadlocale
setlocale
区域设置
localeconv
_mbclenmblen_mblen_l
strlenwcslen_mbslen_mbslen_l_mbstrlen_mbstrlen_l
mbstowcs_mbstowcs_l
mbtowc_mbtowc_l
_setmbcp
setlocale_wsetlocale
strcoll 函数
strftimewcsftime_strftime_l_wcsftime_l
strxfrmwcsxfrm_strxfrm_l_wcsxfrm_l
wcstombs_wcstombs_l
wctomb_wctomb_l