c++ - 如何使用 VS2015 获取语言环境名称?

标签 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;
}

最佳答案

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

相关文章:

c++ - move 构造函数 C++11

c++ - 跳过未使用的虚函数

windows - 在提示选择的批处理文件中,答案应该触发脚本忽略一个选项

C : How to make the size of an array dynamic?

AjaxControlToolkit v7.1213 依赖增加

c++ - 尝试/捕捉引用分配

C# 字符串编码和 LocalAlloc

c++ - 在 Windows 中对套接字的 closesocket() 调用需要 20 秒

c - mainCRTStartup 的签名是什么

c# - Visual Studio C++ 类库中的回调