好友
阅读权限 20
听众
最后登录 1970-1-1
本帖最后由 JerusalemSky 于 2020-12-4 13:59 编辑
网上找遍全网没找到一个现有的方案,自己写了一套,已经经过测试,代码提供两种列表显示,一种是国际ISO缩略语(en_US, cn_ZH),一种是全称(English_United States.1252)。公司电脑加密,直接上源码,随意取用,不谢
#include <WinNls.h>
#include <vector>
typedef std::vector<std::wstring> tLocales;
std::vector<std::wstring> g_locales;
BOOL CALLBACK MyLocaleEnumProc(LPTSTR szLocaleString)
{
g_locales.push_back(szLocaleString);
return TRUE;
}
//LCID码转语言和国家全称(English_United States.1252)
std::wstring& LCID2NLS(LCID lcid, std::wstring& bstrRetBuf)
{
TCHAR arcBuf[128];
memset(arcBuf, 0, sizeof(arcBuf));
GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, arcBuf, 127);
bstrRetBuf = arcBuf;
memset(arcBuf, 0, sizeof(arcBuf));
//Loading the English name for the country/region
GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, arcBuf, 127);
if (*arcBuf)
{
bstrRetBuf += TEXT("_");
bstrRetBuf += arcBuf;
}
//Loading the code page
memset(arcBuf, 0, sizeof(arcBuf));
if ((GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, arcBuf, 127)
|| GetLocaleInfo(lcid, LOCALE_IDEFAULTCODEPAGE, arcBuf, 127))
&& *arcBuf)
{
bstrRetBuf += TEXT(".");
bstrRetBuf += arcBuf;
}
return bstrRetBuf;
}
//LCID码转语言和国家缩写(en_US)
std::wstring& LCID2NLSAbbr(LCID lcid, std::wstring& bstrRetBuf)
{
TCHAR arcBuf[128];
memset(arcBuf, 0, sizeof(arcBuf));
GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, arcBuf, 127);
bstrRetBuf = arcBuf;
memset(arcBuf, 0, sizeof(arcBuf));
//Loading the English name for the country/region
GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, arcBuf, 127);
if (*arcBuf)
{
bstrRetBuf += TEXT("_");
bstrRetBuf += arcBuf;
}
return bstrRetBuf;
}
std::string WString2String(std::wstring ws)
{
std::string strLocale = setlocale(LC_ALL, "");
std::string strResult;
const __wchar_t* wchSrc = ws.c_str();
size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
char* chDest = new char[nDestSize];
memset(chDest, 0, nDestSize);
wcstombs(chDest, wchSrc, nDestSize);
strResult = chDest;
delete[]chDest;
setlocale(LC_ALL, strLocale.c_str());
return strResult;
}
std::vector<std::string> GetAllLocales()
{
EnumSystemLocales(&MyLocaleEnumProc, LCID_INSTALLED);
std::vector<std::string> vwNLS;
std::string strResult;
std::wstring ws;
for (tLocales::const_iterator i = g_locales.begin(); i != g_locales.end(); i++)
{
//目前调用LCID2NLSAbbr返回多语言国际标准中的缩写列表
LCID2NLSAbbr(wcstoul(i->c_str(), nullptr, 16), ws);
strResult = WString2String(ws);
vwNLS.emplace_back(strResult);
}
return vwNLS;
}
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。