【问题标题】:c++ boost locale - generator throws runtime_error with "Invalid file format" messagec++ boost locale - 生成器抛出带有“无效文件格式”消息的 runtime_error
【发布时间】: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文件有错误?

【问题讨论】:

    标签: c++ boost locale


    【解决方案1】:

    我忘记编译 .po 文件...xgettext 生成一个 .po 文本文件,需要将其编译成一个 .mo 文件。这可以通过msgfmt 来完成:

    msgfmt hello.po -o hello.mo

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多