c++11 标准模板(STL)本地化库 - 平面类别 (std::messages_byname )表示系统提供的具名本地环境的 std::messages

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

表示系统提供的具名本地环境的 std::messages

std::messages_byname

template< class CharT >
class messages_byname : public std::messages<CharT>;

std::messages_byname 是 std::messages 平面,它封装从在其构造时指定的 locale 的消息目录取得字符串。

标准库提供二个特化

定义于头文件 <locale>

std::messages_byname<char>窄/多字节字符串消息目录访问
std::messages_byname<wchar_t>宽字符串消息目录访问

成员类型

成员类型定义
catalogstd::messages_base<CharT>::catalog
string_typestd::basic_string<CharT>

成员函数

(构造函数)

构造新的 messages_byname 平面
(公开成员函数)

(析构函数)

析构 messages_byname 平面
(受保护成员函数)

std::messages_byname::messages_byname

explicit messages_byname( const char* name, std::size_t refs = 0 );

explicit messages_byname( const std::string& name, std::size_t refs = 0 );

(C++11 起)

为名为 name 的本地环境构造新的 std::messages_byname 平面。

refs 用于资源管理:在销毁最后一个保有平面的 std::locale 对象时,若 refs == 0 ,则实现销毁平面对象。否则,不销毁对象。

参数

name-本地环境的名称
refs-链接到该平面的引用数

std::messages_byname::~messages_byname

protected:
~messages_byname();

销毁平面。

继承自 std::messages

成员类型

成员类型定义
char_typecharT
string_typestd::basic_string<charT>

成员对象

成员名类型
id [静态]std::locale::id

成员函数

open

调用 do_open
(std::messages<CharT> 的公开成员函数)

get

调用 do_get
(std::messages<CharT> 的公开成员函数)

close

调用 do_close
(std::messages<CharT> 的公开成员函数)

受保护成员函数

do_open

[虚]

打开具名消息目录
(std::messages<CharT> 的虚受保护成员函数)

do_get

[虚]

从打开的消息目录获取消息
(std::messages<CharT> 的虚受保护成员函数)

do_close

[虚]

关闭消息目录
(std::messages<CharT> 的虚受保护成员函数)

调用示例 windows 

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <exception>

#include <Windows.h>

std::vector<std::wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

std::string stows(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

void try_with(const std::locale& locale)
{
    std::cout << locale.name() << " ";

    const std::messages<char>& facet =
        std::use_facet<std::messages<char> >(locale);

    std::messages<char>::catalog cat = facet.open("sed", std::cout.getloc());
    if (cat < 0)
    {
        std::cout << "Could not open \"sed\" message catalog" << std::endl;
    }
    else
    {
        std::cout << "\"No match\" "
                  << facet.get(cat, 0, 0, "No match") << std::endl
                  << "\"Memory exhausted\" "
                  << facet.get(cat, 0, 0, "Memory exhausted") << std::endl;
    }

    facet.close(cat);
}
int main()
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);

    for (std::vector<std::wstring>::const_iterator str = locals.begin();
            str != locals.end(); ++str)
    {
        std::locale locale(stows(*str));
        std::cout.imbue(locale);
        std::cout << locale.name() << " ";

        try_with(std::locale(std::locale("C"), new std::messages_byname<char>(locale.name())));
    }
    return 0;
}

输出

de-DE_phoneb * Could not open "sed" message catalog
es-ES_tradnl * Could not open "sed" message catalog
hu-HU_technl * Could not open "sed" message catalog
ja-JP_radstr * Could not open "sed" message catalog
ka-GE_modern * Could not open "sed" message catalog
x-IV_mathan * Could not open "sed" message catalog
zh-CN_phoneb * Could not open "sed" message catalog
zh-CN_stroke * Could not open "sed" message catalog
zh-HK_radstr * Could not open "sed" message catalog
zh-MO_radstr * Could not open "sed" message catalog
zh-MO_stroke * Could not open "sed" message catalog
zh-SG_phoneb * Could not open "sed" message catalog
zh-SG_stroke * Could not open "sed" message catalog
zh-TW_pronun * Could not open "sed" message catalog
zh-TW_radstr * Could not open "sed" message catalog

  • 17
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值