2

完整的 C++ i18n gettext() “hello world”示例包含适用于简单固定字符串的 C++ 代码。我现在正在寻找一个适用于复数的示例程序。此示例代码显示六行。只有一个是英语正确的。它不能正确处理复数。

cat >helloplurals.cxx <<EOF
// hellopurals.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
#include <stdio.h>
int main (){
    setlocale(LC_ALL, "");
    bindtextdomain("helloplurals", ".");
    textdomain( "helloplurals");
    for (int ii=0; ii<5; ii++)
        printf (gettext("Hello world with %d moon.\n"), ii);
}
EOF
g++ -o helloplurals helloplurals.cxx
./helloplurals

用于复数形式的 GNU gettext()描述了语言处理复数的各种方式,例如:

  • 韩语 - 无复数
  • 英语 - 两种形式,单数只用于一种
  • 法语 - 两种形式,单数用于零和一
  • 波兰语 - 三种形式,一种特殊情况和一些以 2、3 或 4 结尾的数字

我的期望是代码将能够专门针对上述所有情况以及此处未列出的其他几种变体工作(给定消息目录)。用英语执行时的正确输出是:

Hello world with 0 moons.
Hello world with 1 moon.
Hello world with 2 moons.
Hello world with 3 moons.
Hello world with 4 moons.
4

3 回答 3

3

我不确定你想要什么。如果对您的示例稍作修改即可提供所需的输出,只需将 printf 行替换为

printf(ngettext("Hello world with %d moon\n", "Hello world with %d moons\n", ii), ii);

但由于它是对 unwind 答案的简单修改,并且 gettext 文档有非常相似的示例,

printf (ngettext ("%d file removed", "%d files removed", n), n);

I wonder if it is really what you wanted. If you want to use gettext with a more C++ syntax, you'll have to look for libraries like Boost::Format.

于 2009-07-10T11:27:12.580 回答
2

首先,gettext()并不神奇。它不包含所有语言中所有单词的全球词典。

它所做的只是在应用程序的消息数据库中查找消息的翻译,所以这个例子假设有这样一个文件(在哪里gettext()可以找到它,这可能有点棘手)。

接下来,你用错了。您链接到的页面描述了该ngettext()功能,您必须使用该功能才能获得随计数变化的翻译。

您的电话应如下所示:

printf("%s", ngettext("moon", "moons", ii));

这让 gettext 根据 count 参数决定使用哪种形式。

于 2009-07-10T07:42:26.480 回答
0

那么你真的为不同的复数形式制作了一个 .po 文件吗?请参阅Wikipedia 上对 gettext 工作流程的描述。还要阅读所有关于复数形式的gettxt 文档,特别是包含复数形式的 .po 文件的示例。

于 2009-07-10T07:39:41.213 回答