setlocale, _wsetlocale

设置或检索运行时区域设置。

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

参数

  • category
    区域设置的影响的类别。

  • locale
    区域设置说明符。

返回值

如果为有效的 locale 和 category,返回指向该字符串与指定的 locale 和 category。 如果 locale 或 category 无效,则返回 null 指针,并且不更改程序的当前区域设置。

例如,调用

setlocale( LC_ALL, "en-US" );

设置所有类别,仅返回该字符串

en-US

您可以使用 setlocale 返回的字符串指针的后续调用继续执行程序的区域设置信息将该部分,假定,您的程序不修改指针或该字符串。 稍后对 setlocale 复盖字符串,使之前返回的字符串指针不再之前调用有效。 可以使用 _strdup 保存给定区域设置字符串的副本。

备注

使用 setlocale 功能集,或者更改查询 locale 和 category指定的部分或全部当前程序区域设置信息。 locale 引用局部性 (国家/地区和语言) 可以自定义您的程序的某些方面。 一些与区域设置相关的类别包括日期的格式设置和货币值的显示格式。 如果设置 locale 为一种语言的默认值字符串与在您的计算机上支持多个窗体,则应检查 setlocale 返回代码发现哪种语言有效。 例如,使用“中文”会导致 chinese-simplified 或 chinese-traditional的返回值。

_wsetlocale 是 setlocale的宽字符版本;locale 参数和返回 _wsetlocale 的值是宽字符字符串。 _wsetlocale 和 setlocale 否则具有相同的行为。

一般文本例程映射

TCHAR.H 实例

未定义的_UNICODE & _MBCS

定义的_MBCS

定义的_UNICODE

_tsetlocale

setlocale

setlocale

_wsetlocale

category 参数指定受影响程序的区域设置信息的一部分。 用于 category 的宏及其影响是过程的部分如下:

  • LC_ALL
    所有类别,如下所示。

  • LC_COLLATE
    strcoll、_stricoll、wcscoll、_wcsicoll、strxfrm、_strncoll、_strnicoll、_wcsncoll、_wcsnicoll和 wcsxfrm 功能。

  • LC_CTYPE
    字符托管函数 (除外 isdigit、isxdigit、mbstowcs和 mbtowc,不受影响)。

  • LC_MONETARY
    localeconv 函数返回的货币格式设置信息。

  • LC_NUMERIC
    十进制点字符格式化输出实例 (例如 printf),数据转换实例和对于 localeconv返回的非货币的格式设置信息。除小数点点字符之外,还 LC_NUMERIC 设置 localeconv和分组的控件字符串返回的千位分隔符。

  • LC_TIME
    strftime 和 wcsftime 功能。

此功能验证类别参数。 如果类别参数不是在上表中提供的一个值,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,该功能集 errno 到 EINVAL 并返回 NULL。

locale 参数是指向指定区域设置的字符串。 有关 locale 参数的格式的信息,请参见 区域设置名称、语言和国家/地区字符串。 如果 locale 指向一个空字符串,区域设置为实现中定义的本机环境。 C 的值为 C 将指定最小的 ANSI 匹配的环境。 C 区域设置,假设所有 char 数据类型为 1 字节,并且其小于 256 的值始终为。

在程序启动时,下面的语句等效执行:

setlocale( LC_ALL, "C" );

locale 参数可以采用区域设置名称、语言字符串、语言字符串和国家/地区代码、代码页或语言字符串、国家/地区代码和代码页。 设置可用区域设置名称、语言、国家/地区代码和代码页包含除需要多每个字符两个字节,例如 UTF-7、UTF-8 的代码页的 NLS windows API 支持的一些。 如果您提供与 UTF-7 或 UTF-8 的代码页,setlocale 将失败,则返回 null。 设置 setlocale 支持的区域设置名称在 区域设置名称、语言和国家/地区字符串所述。 设置语言和国家/地区/ setlocale 支持的区域字符串在 语言字符串国家/地区字符串列表。

作为 locale 传递的 NULL 指针调用 setlocale 查询而不是设置国际环境。 如果 locale 参数为 null 指针,不更改程序的当前区域设置。 相反,setlocale 返回指向该字符串与线程的当前区域设置的 category。 如果 category 参数是 LC_ALL,该函数返回指示当前设置每个的字符串类,由分号分隔。 例如,调用顺序

// Set all categories and return "en-US"

setlocale(LC_ALL, "en-US");

// Set only the LC_MONETARY category and return "fr-FR"

setlocale(LC_MONETARY, "fr-FR");

printf("%s\n", setlocale(LC_ALL, NULL));

返回

LC_COLLATE=en-US;LC_CTYPE=en-US;LC_MONETARY=fr-FR;LC_NUMERIC=en-US;LC_TIME=en-US

哪些为字符串与 LC_ALL 类别。

下面的示例与 LC_ALL 类别。 字符串“.OCP”和“.ACP”之一可以在代码分页位置以指定给用户的默认值 OEM 代码页和用户默认值 ANSI 代码页的使用,分别。

  • setlocale( LC_ALL, "" );
    将区域设置设置为默认值,该值是从操作系统获得的用户默认值 ANSI 代码页。

  • setlocale( LC_ALL, ".OCP" );
    显式设置区域设置为从操作系统获得的当前 OEM 代码页。

  • setlocale( LC_ALL, ".ACP" );
    将区域设置设置为从操作系统获得的 ANSI 代码页。

  • setlocale( LC_ALL, "<localename>" );
    将区域设置设置为 <localename>表示的区域设置名称。

  • setlocale( LC_ALL, "<language>_<country>" );
    将区域设置设置为 <language> 和 <country>表示该语言和国家/地区,使用从主机操作系统获得的默认值代码页。

  • setlocale( LC_ALL, "<language>_<country>.<code_page>" );
    将区域设置设置为 <language>、<country>和 <code_page> 字符串表示的语言、国家/地区和代码页。 可以使用语言、国家/地区和代码页的各种组合。 例如:

    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, "<language>" );
    将区域设置设置为 <language>表示的语言,则使用默认值国家/地区为该国家/地区指定的默认值和用户 ANSI 代码页该语言为从主机操作系统获取。 例如,以下对 setlocale 在功能上是等效的:

    setlocale( LC_ALL, "en-US" );
    setlocale( LC_ALL, "English" );
    setlocale( LC_ALL, "English_United States.1252" );
    
  • setlocale( LC_ALL, ".<code_page>" );
    设置代码页中 <code_page>表示的值,则使用默认值国家/地区和语言 (所定义的主机操作系统) 的指定代码页。

类必须是影响代码页的更改的 LC_ALL 或 LC_CTYPE。 例如,因此,如果主机操作系统的默认值国家/地区和语言为“US”和“英语,”以下两个调用 setlocale 在功能上是等效的:

setlocale( LC_ALL, ".1252" );

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

有关更多信息中看到在 预处理程序的setlocale 说明 引用。

函数 _configthreadlocale 用于控制 setlocale 是否影响所有线程区域设置在程序或调用线程的仅区域设置。

要求

实例

必需的标头

setlocale

<locale.h>

_wsetlocale

<locale.h> 或 <wchar.h>

有关其他的兼容性信息,请参见中介绍的 兼容性

示例

// crt_setlocale.cpp
// 
// This program demonstrates the use of setlocale when
// using two independent threads.
//

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

#define BUFF_SIZE 100

// Retrieve the date and time in the current
// locale's format.
int get_time(unsigned char* str)
{
    __time64_t ltime;
    struct tm  thetime;

    // Retieve the time
    _time64(&ltime);
    _gmtime64_s(&thetime, &ltime);

    // Format the current time structure into a string
    // using %#x is the long date representation,
    // appropriate to the current locale
    if (!strftime((char *)str, BUFF_SIZE, "%#x", 
                  (const struct tm *)&thetime))
    {
        printf("strftime failed!\n");
        return -1;
    }
    return 0;
}

// This thread sets its locale to German
// and prints the time.
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
    unsigned char str[BUFF_SIZE];

    // Set the thread local
    printf("The thread locale is now set to %s.\n",
           setlocale(LC_ALL, "de-DE"));

    // Retrieve the time string from the helper function
    if (get_time(str) == 0)
    {
        printf("The time in de-DE locale is: '%s'\n", str);
    }

    _endthreadex( 0 );
    return 0;
} 

// The main thread spawns a second thread (above) and then
// sets the locale to English and prints the time.
int main()
{ 
    HANDLE          hThread;
    unsigned        threadID;
    unsigned char   str[BUFF_SIZE];

    // Configure per-thread locale to cause all subsequently created 
    // threads to have their own locale.
    _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);

    // Retrieve the time string from the helper function
    printf("The thread locale is now set to %s.\n",
           setlocale(LC_ALL, "en-US"));

    // Create the second thread.
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc,
                                      NULL, 0, &threadID );

    if (get_time(str) == 0)
    {
        // Retrieve the time string from the helper function
        printf("The time in en-US locale is: '%s'\n\n", str);
    }

    // Wait for the created thread to finish.
    WaitForSingleObject( hThread, INFINITE );

    // Destroy the thread object.
    CloseHandle( hThread );
}
         

.NET Framework 等效项

System::Globalization::CultureInfo 选件类

请参见

参考

区域设置名称、语言和国家/地区字符串

_configthreadlocale

_create_locale、_wcreate_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功能

strftime, wcsftime, _strftime_l, _wcsftime_l

strxfrm, wcsxfrm, _strxfrm_l, _wcsxfrm_l

wcstombs, _wcstombs_l

wctomb, _wctomb_l