c++ - Boost Locale 翻译入门

标签 c++ boost boost-locale

这可能是一个简单的问题,但我已经为此苦苦挣扎了很长一段时间,这就是为什么我希望你们中的一个人可以帮助我。

我正在尝试让 Boost locale 的函数 translate 正常工作。引用此示例代码:

#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;
}
  • 假设我想翻译成德语。我是否必须像 locale::global(gen("de")); 一样传递 "de"

  • 我读了一些 *.po*.mo 文件。 有人可以解释一下这一切是如何运作的吗?

提前致谢。

最佳答案

Each locale is defined by a specific locale identifier, which contains a mandatory part (Language) and several optional parts (Country, Variant, keywords and character encoding of std::string). Boost.Locale uses the POSIX naming convention for locales, i.e. a locale is defined as language[_COUNTRY][.encoding][@variant], where lang is ISO-639 language name like "en" or "ru", COUNTRY is the ISO-3166 country identifier like "US" or "DE", encoding is the eight-bit character encoding like UTF-8 or ISO-8859-1, and variant is additional options for specializing the locale, like euro or calendar=hebrew, see Variant. Note that each locale should include the encoding in order to handle char based strings correctly. https://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/locale_gen.html

在您的情况下,它可能是 locale::global(gen("de_DE.UTF-8"));


我会向您推荐这些来源以获取更多信息:

关于c++ - Boost Locale 翻译入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70381091/

相关文章:

c++ - opencv中的imshow窗口卡住

c++ - 为什么boost::locale::date_time中的std::bad_cast异常适用于全局而不是本地对象?

c++ - 用于字符串编码的 boost::locale to_utf 函数,喜欢抛出

c++ - 返回父类的 C++11 unique_ptr 的函数指针

c++ - 为什么我的顶点缓冲区对象出现访问冲突错误?

C++/Qt - 无法从 qrc 文件打开 .txt

c++ - boost::locale::to_lower 抛出 bad_cast 异常

c++ - 拥有所有派生自同一基类的类实例的集合的最佳方法是什么?

c++ - 将 boost 与自己的 header 和源代码链接起来

c++ - Boost 图切割上的 3 个类(0、1、4)