Namespaces
Variants

std::messages<CharT>:: open, std::messages<CharT>:: do_open

From cppreference.net
定义于头文件 <locale>
public :
catalog open ( const std:: string & name, const std:: locale & loc ) const ;
(1)
protected :
virtual catalog do_open ( const std:: string & name, const std:: locale & loc ) const ;
(2)
1) 公共成员函数,调用最派生类的受保护虚成员函数 do_open
2) 获取一个类型为 catalog (继承自 std::messages_base )的值,该值可传递给 get() 以从名为 name 的消息目录中检索消息。该值在传递给 close() 之前持续有效。

目录

参数

name - 要打开的消息目录的名称
loc - 提供读取目录消息时可能需要的额外facet的区域设置对象,例如用于执行宽字符/多字节转换的 std::codecvt

返回值

类型为 catalog 的非负值,可用于 get() close() 。如果无法打开目录,则返回负值。

注释

在POSIX系统中,此函数调用通常转换为对 catopen() 的调用。在GNU libstdc++中,它调用 textdomain

实际目录位置由实现定义:例如在德语区域设置中,对于 "sed" 目录(随 Unix 实用程序 'sed' 安装的消息目录),此函数调用打开的文件可能是 /usr/lib/nls/msg/de_DE/sed.cat /usr/lib/locale/de_DE/LC_MESSAGES/sed.cat /usr/share/locale/de/LC_MESSAGES/sed.mo

示例

以下示例演示了消息检索功能:在典型的 GNU/Linux 系统上,该程序会从 /usr/share/locale/de/LC_MESSAGES/sed.mo 读取消息。

#include <iostream>
#include <locale>
int main()
{
    std::locale loc("de_DE.utf8");
    std::cout.imbue(loc);
    auto& facet = std::use_facet<std::messages<char>>(loc);
    auto cat = facet.open("sed", loc);
    if (cat < 0)
        std::cout << "Could not open german \"sed\" message catalog\n";
    else
        std::cout << "\"No match\" in German: "
                  << facet.get(cat, 0, 0, "No match") << '\n'
                  << "\"Memory exhausted\" in German: "
                  << facet.get(cat, 0, 0, "Memory exhausted") << '\n';
    facet.close(cat);
}

可能的输出:

"No match" in German: Keine Übereinstimmung
"Memory exhausted" in German: Speicher erschöpft

参见