Win32 API之Setlocale函数配置地域化信息函数

转载 2016年08月31日 16:53:19

setlocale:
函数原形为:char *setlocale( int category, const char *locale );
头文件:<locale.h>
所支持的操作系统为:ANSI, Win 95, Win NT
对于简体中文可以使用如下设置:setlocale( LC_ALL, "chs" );

为什么一定要调用setlocale呢?
因为在C/C++语言标准中定义了其运行时的字符集环境为"C",也就是ASCII字符集的一个子集,那么mbstowcs在工作时会将cstr中所包含的字符串看作是ASCII编码的字符,而不认为是一个包含有chs编码的字符串,所以他会将每一个中文拆成2个ASCII编码进行转换,这样得到的结果就是会形成4个wchar_t的字符组成的串,那么如何才能够让mbstowcs正常工作呢?在调用mbstowcs进行转换之间必须明确的告诉mbstowcs目前cstr串中包含的是chs编码的字符串,通过setlocale( LC_ALL, "chs" )函数调用来完成,需要注意的是这个函数会改变整个应用程序的字符集编码方式,必须要通过重新调用setlocale( LC_ALL, "C" )函数来还原,这样就可以保证mbstowcs在转换时将cstr中的串看作是中文串,并且转换成为2个wchar_t字符,而不是4个。 

 

本地化设置需要具备三个条件:
    a.语言代码(Language Code)
    b.国家代码(Country Code)
    c.编码(Encoding)
    本地名字可以用下面这些部分来构造:
    语言代码_国家代码.编码 比如(zh_CN.UTF-8, en_US等)

locale的别名表见/usr/lib/X11/locale/locale.alias(以Debian GNU/Linux为例)

setlocale语言字符串参考

Primary
language

Sublanguage

Language string
Chinese Chinese "chinese"
Chinese Chinese (simplified) "chinese-simplified" or "chs"
Chinese Chinese (traditional) "chinese-traditional" or "cht"
Czech Czech "csy" or "czech"
Danish Danish "dan" or "danish"
Dutch Dutch (default) "dutch" or "nld"
Dutch Dutch (Belgian) "belgian", "dutch-belgian", or "nlb"
English English (default) "english"
English English (Australian) "australian", "ena", or "english-aus"
English English (Canadian) "canadian", "enc", or "english-can"
English English (New Zealand) "english-nz" or "enz"
English English (United Kingdom) "eng", "english-uk", or "uk"
English English (United States) "american", "american english", "american-english", "english-american", "english-us", "english-usa", "enu", "us", or "usa"
Finnish Finnish "fin" or "finnish"
French French (default) "fra" or "french"
French French (Belgian) "frb" or "french-belgian"
French French (Canadian) "frc" or "french-canadian"
French French (Swiss) "french-swiss" or "frs"
German German (default) "deu" or "german"
German German (Austrian) "dea" or "german-austrian"
German German (Swiss) "des", "german-swiss", or "swiss"
Greek Greek "ell" or "greek"
Hungarian Hungarian "hun" or "hungarian"
Icelandic Icelandic "icelandic" or "isl"
Italian Italian (default) "ita" or "italian"
Italian Italian (Swiss) "italian-swiss" or "its"
Japanese Japanese "japanese" or "jpn"
Korean Korean "kor" or "korean"
Norwegian Norwegian (default) "norwegian"
Norwegian Norwegian (Bokmal) "nor" or "norwegian-bokmal"
Norwegian Norwegian (Nynorsk) "non" or "norwegian-nynorsk"
Polish Polish "plk" or "polish"
Portuguese Portuguese (default) "portuguese" or "ptg"
Portuguese Portuguese (Brazilian) "portuguese-brazil" or "ptb"
Russian Russian (default) "rus" or "russian"
Slovak Slovak "sky" or "slovak"
Spanish Spanish (default) "esp" or "spanish"
Spanish Spanish (Mexican) "esm" or "spanish-mexican"
Spanish Spanish (Modern) "esn" or "spanish-modern"
Swedish Swedish "sve" or "swedish"
Turkish Turkish "trk" or "turkish"

 

setlocale国家字符串参考

Country/Region Country/Region string
Australia "aus" or "australia"
Austria "aut" or "austria"
Belgium "bel" or "belgium"
Brazil "bra" or "brazil"
Canada "can" or "canada"
China "china", "chn", "pr china", or "pr-china"
Czech Republic "cze" or "czech"
Denmark "dnk" or "denmark"
Finland "fin" or "finland"
France "fra" or "france"
Germany "deu" or "germany"
Greece "grc" or "greece"
Hong Kong SAR "hkg", "hong kong", or "hong-kong"
Hungary "hun" or "hungary"
Iceland "iceland" or "isl"
Ireland "irl" or "ireland"
Italy "ita" or "italy"
Japan "jpn" or "japan"
Korea "kor" or "korea"
Mexico "mex" or "mexico"
The Netherlands "nld", "holland", or "netherlands"
New Zealand "nzl", "new zealand", "new-zealand", or "nz"
Norway "nor" or "norway"
Poland "pol" or "poland"
Portugal "prt" or "portugal"
Russia "rus" or "russia"
Singapore "sgp" or "singapore"
Slovakia "svk" or "slovak"
Spain "esp" or "spain"
Sweden "swe" or "sweden"
Switzerland "che" or "switzerland"
Taiwan "twn" or "taiwan"
Turkey "tur" or "turkey"
United Kingdom "gbr", "britain", "england", "great britain", "uk", "united kingdom", or "united-kingdom"
United States "usa", "america", "united states", "united-states", or "us"
    

相关文章推荐

Win32 API之Setlocale函数配置地域化信息函数

setlocale: 函数原形为:char *setlocale( int category, const char *locale ); 头文件: 所支持的操作系统为:ANSI, Win 95, W...
  • whatday
  • whatday
  • 2013年08月03日 19:54
  • 2296

strftime()函数根据区域设置格式化本地时间/日期 setlocale() 函数设置地区信息(地域信息)

我是小白一个,知识要慢慢积累, 今天用到这个函数,发现很多功能,记录一下,下面其实就是PHP手册上复制下来的。 setlocale() 函数设置地区信息(地域信息) strft...

Win32 API消息函数:SendMessage(发送消息)

Win32 API消息函数:SendMessage(发送消息) 函数功能: 该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序,直到窗口程 序处理完消息再返回。...

Win32 API讲座--窗口函数(转)

2007-12-04 09:52一、 关于窗口函数在上一堂课里,我们已经提出了"句柄"的概念,并为此进行了较深度的讨论。现在来想,我要补充的是,句柄并非是仅仅是窗口才有的。似乎所有的WINDOWS对象...

C#中对应win32 API原型函数的一些数据类型

BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System.Int16 COLORREF=System.UInt...

WIN32API函数CreateProcess创建一个新的进程和它的主线程,新进程运行指定的可执行文件

#include //CString的头文件 #include //屏蔽窗口 #pragma comment(linker,"/subsystem:windows /ENTRY:mainCRTStar...

在Win32工程下利用API函数使用DateTimePicker方法小结

DateTimePicker是一种能够直观显示短日期/时间的控件,我想在Win32工程下,只利用API函数来使用它,以下为自己的使用方法总结: 前提:已经利用可视化界面直接拖放一个DateTimeP...
  • sywcxx
  • sywcxx
  • 2014年05月31日 11:22
  • 929

C#调用win32API画图函数示例

SelectObject和DeleteObject 函数 Windows显示设备的属性,共有下面几种:位图、画刷、字体、画笔、区域。如果要设置它们到当前设备里,就需要使用SelectObject...

Detours简介 (拦截x86机器上的任意的win32 API函数)

Detours 当然是用detours,微软明显高腾讯一筹,同上,至今没失败过.写这种HOOK一定要再写个测试程序,不要直接HOOK你的目的程序,例如QQ,因为这样不方面更灵活的测试. 说明一下:...

MFC调用win32窗口显示调试信息,使用AllocConsole 函数

 AllocConsole Function 为主调进程分配一个新的控制台。    语法 C++ :    BOOL WINAPI AllocConsole(void);    参数:   无    ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Win32 API之Setlocale函数配置地域化信息函数
举报原因:
原因补充:

(最多只允许输入30个字)