【发布时间】:2022-01-13 17:26:45
【问题描述】:
我尝试用这段代码重现boost locale documentation 的例子:
#include <boost/locale.hpp>
#include <iostream>
#include <stdexcept>
using namespace boost::locale;
using namespace std;
int main()
{
generator gen;
// Specify location of dictionaries
gen.add_messages_path("./languages");
gen.add_messages_domain("hello");
// Generate locales and imbue them to iostream
locale::global(gen("es_ES"));
cout.imbue(locale());
// Display a message using current system locale
cout << translate("Hello World") << endl;
return 0;
}
我已经用 xgettext 生成了这个hello.mo 文件并将它放在一个文件夹./languages/es_ES/LC_MESSAGES 中:
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-01-13 17:30+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: test.cpp:95
msgid "Hello World"
msgstr "Hola a todos"
当我运行代码时,locale::global(gen("es_ES")); 行会抛出 std::runtime_error 并带有以下消息:“文件格式无效”。是不是说明.mo文件有错误?
【问题讨论】: