何时以及如何使用std :: locale :: messages?

xml*_*lmx 8 c++ locale internationalization facet c++11

C++标准定义了六个类别方面的:collate,ctype,monetary,numeric,time,和messages.

我已经知道了前五个的用法,但我不知道何时以及如何使用最后一个:std::locale::messages.

任何说明性例子?

She*_*vin 8

std::locale::messages用于打开消息目录(最常见的GNU gettext),包括翻译的字符串.下面是一个示例,它使用Linux(for sed)在德语中打开现有消息目录,检索(使用get())并输出英语字符串的翻译:

#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);
}
Run Code Online (Sandbox Code Playgroud)

哪个输出:

"No match" in German: Keine Übereinstimmung
"Memory exhausted" in German: Speicher erschöpft
Run Code Online (Sandbox Code Playgroud)

编辑:根据此评论澄清.

  • 不只是GNU gettext.消息目录是POSIX的一部分(例如,gencat(1),catopen(3)).Windows也有消息目录,但我不确定他们是否曾经打算实现标准的区域设置消息方面. (3认同)