Boost翻译:Po文件不工作

boost translate: po file not work

本文关键字:工作 文件 Po 翻译 Boost      更新时间:2023-10-16

我使用boost::locale来制作多语言exe,但它不起作用。exe总是输出"Hello World"。它如何输出"请问"?

我使用了来自http://www.boost.org/doc/libs/1_53_0/libs/locale/doc/html/messages_formatting.html

的示例代码
    #include <boost/locale.hpp>
    #include <iostream>
    using namespace std;
    using namespace boost::locale;
    int main()
    {
        generator gen;
        // Specify location of dictionaries
        gen.add_messages_path(".");
        gen.add_messages_domain("hello");
        // Generate locales and imbue them to iostream
        locale::global(gen(""));
        cout.imbue(locale());
        // Display a message using current system locale
        cout << translate("Hello World") << endl;
    }

创建一个po文件和一个mo文件。Po文件是:

# 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.
#
msgid ""
msgstr ""
"Project-Id-Version: messagesn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2013-04-26 20:50+0800n"
"PO-Revision-Date: 2013-04-26 21:44+0800n"
"Last-Translator: n"
"Language-Team: LANGUAGE <LL@li.org>n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
"Content-Transfer-Encoding: 8bitn"
"X-Generator: Poedit 1.5.5n"
"X-Poedit-SourceCharset: UTF-8n"
#: main.cpp:21
msgid "Hello World"
msgstr "您好"

我也从boost_locale开始,这是我如何使它工作的…

  1. 首先确保你的。mo文件的命名与你引用的域名完全一致,在本例中是hello.mo

  2. 将。mo文件放入正确的文件结构中,例如,如果您试图将其翻译为西班牙语,则该文件为。/es_ES/LC_MESSAGES/hello.mo

  3. 确保像这样实例化全局语言环境,地区:全球(创("es_ES.UTF-8"));