如何使用VS2015获取区域设置名称

How do I get the locale name using VS2015?

本文关键字:设置 区域 获取 何使用 VS2015      更新时间:2023-10-16

我可以使用下面在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.0VCincludexlocinfo 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。