c++ - boost 翻译: po file not work

标签 c++ boost locale translate po

我曾使用 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: messages\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-04-26 20:50+0800\n"
"PO-Revision-Date: 2013-04-26 21:44+0800\n"
"Last-Translator: \n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.5\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: main.cpp:21
msgid "Hello World"
msgstr "您好"

最佳答案

我也是从 boost_locale 开始的,这是我如何让它工作的......

  1. 首先确保您的 .mo 文件的命名与您引用的域完全相同,在本例中为 hello.mo

  2. 将 .mo 文件放入正确的文件结构中,例如,如果您尝试翻译成西类牙语,这将是 ./es_ES/LC_MESSAGES/hello.mo

  3. 确保像这样实例化全局语言环境, locale::global(gen("es_ES.UTF-8"));

希望这对您有所帮助。

关于c++ - boost 翻译: po file not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16238610/

相关文章:

C++ 类 : pointer to a non-static func

c++ - Boost.Program_Options : When <bool> is specified as a command-line option, 什么是有效的命令行参数?

c++ - 将 boost::iequals 与 std::u16string 一起使用

R plot() 生成俄语日期标签

c++ - 为什么将 char 数组转换为 int 指针并使用指针写入它会使数据反转?

c++ - 使用抽象返回类型设计继承

c++ - 资源获取即初始化 (RAII) 是什么意思?

C++ 共享库创建 - 链接到其他共享库

windows - 如何使用 VS 在 Windows 7 上安装 Boost

java - 如何理解Java教程国际化定制资源包加载示例?