C++ 标准定义了六类构面:collate、ctype、monetary、numeric、time和messages.
我知道前五个的用法,但不知道何时以及如何使用最后一个:std::locale::messages.
有什么说明性的例子吗?
C++ 标准定义了六类构面:collate、ctype、monetary、numeric、time和messages.
我知道前五个的用法,但不知道何时以及如何使用最后一个:std::locale::messages.
有什么说明性的例子吗?
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);
}
输出:
"No match" in German: Keine Übereinstimmung
"Memory exhausted" in German: Speicher erschöpft
编辑:根据此评论进行澄清。