C++ 提升区域设置 - 生成器抛出带有"Invalid file format"消息的runtime_error



我试图用以下代码重现boost语言环境文档的示例:

#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 VERSIONn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2022-01-13 17:30+0100n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>n"
"Language-Team: LANGUAGE <LL@li.org>n"
"Language: n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=CHARSETn"
"Content-Transfer-Encoding: 8bitn"
#: test.cpp:95
msgid "Hello World"
msgstr "Hola a todos"

当我运行代码时,行locale::global(gen("es_ES"));抛出一个带有以下消息的std::runtime_error:;无效的文件格式"。这是否意味着.mo文件中存在错误?

我忘了编译.po文件。。。xgettext生成一个.po文本文件,该文件需要编译为.mo文件。这可以通过msgfmt:完成

msgfmt hello.po -o hello.mo

相关内容

最新更新