Setlocale

来源:互联网 发布:淘宝双十一 实时 编辑:程序博客网 时间:2024/05/22 18:22
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"






Setlocale
目录
梗概:描述:返回值
编辑本段梗概:
配置地域化信息函数
#include <locale.h>
char *setlocale (int category, const char * locale);[1]
编辑本段描述:
函数种类: 操作系统与环境
内容说明:
本函数用来配置地域的信息,设置当前程序使用的本地化信息。参数 category 有下列的选择:
LC_ALL 包括下面的全部选项都要。 LC_COLLATE 配置字符串比较,PHP 目前尚未实作出来本项。 LC_CTYPE 配置字符类别及转换。例如全变大写?strtoupper()。 LC_MONETARY 配置金融货币,PHP 目前尚未实作。 LC_NUMERIC 配置小数点后的位数。 LC_TIME 配置时间日期格式,与 strftime() 合用。 而参数 locale 若是空字符串 "",则会使用系统环境变量的 locale 。若 locale 为零(NULL),则不会改变地域化配置,返回当前的地域值,若系统尚未实作则返回 false。
Locales contain information on how to interpret and perform certain input/output and transformation operations taking into consideration location and language specific settings.
Most running environments have certain locale information set according to the user preferences or localization. But, independently of this system locale, on start, all C programs have the "C" locale set, which is a rather neutral locale with minimal locale information that allows the result of programs to be predictable. In order to use the default locale set in the environment, this function can be called with "" as the?localeparameter.
The locale set on start is the same as setlocale(LC_ALL,"C") would set.
The entire default locale can be set by calling setlocale(LC_ALL,"");
C程序开始的时候的设置和 setlocale(LC_ALL,"C")相同
使用系统默认的设置调用setlocale(LC_ALL,"");
The parts of the current locale affected by a call to this function are specified by parameter?category.
若想显示中文"我爱你",则要包含#include &lt;locale.h&gt; 加一下代码:
wchar_t s[]={ 25105, 29233 ,20320 ,0};
setlocale( LC_ALL, "" );
printf("%ls",s);//注意是ls
编辑本段返回值
成功:返回相应设置的地域化信息。字符串可能分配在静态存储区。
失败:返回NULL
原创粉丝点击