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

时间:2016-01-13 22:34:13

标签: c++ windows visual-studio-2013 visual-studio-2015

我可以使用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;
}

1 个答案:

答案 0 :(得分:0)

在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。

相关问题