我可以使用下面在 Visual Studio 2013 中编译的代码获取系统的区域设置名称。如果我在 VS2015 中编译相同的代码,我将一无所获!这是一个错误吗?那么,如何使用 VS2015 获取当前系统区域设置的名称?
#include "stdafx.h"
#include <iostream>
#include <locale>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << std::locale("").name().c_str() << endl;
}
最佳答案
在 VS2015 中,如果语言环境始终等于您传递给构造函数的参数(如果它有效),他们会这样命名:
// c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xlocinfo line 360
void _Construct(const string &_Str,
category _Cat)
{ // construct a locale with named facets
bool _Bad = false;
_Init();
if (_Cat != none)
{ // worth adding, do it
_TRY_BEGIN
_BEGIN_LOCINFO(_Lobj(_Cat, _Str.c_str()))
if (_Badname(_Lobj))
_Bad = true;
else
{ // name okay, build the locale
_Locimp::_Makeloc(_Lobj, _Cat, _Ptr, 0);
// The two lines below were added in VS2015
_Ptr->_Catmask = _Cat;
_Ptr->_Name = _Str.c_str(); // <--- Here they set the name forcefully
}
我相信你必须使用 setlocale()相反:
std::cout << setlocale(LC_ALL, "") << endl;
要在 std::locale
中使用它,您可以这样做
std::locale loc(setlocale(LC_ALL, ""));
这适用于 VS2013 和 VS2015。
关于c++ - 如何使用 VS2015 获取语言环境名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778356/