代码之家  ›  专栏  ›  技术社区  ›  CW Holeman II

I18N C++ + hello世界

  •  2
  • CW Holeman II  · 技术社区  · 16 年前

    Complete C++ i18n gettext() “hello world” example 有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() for plural forms 描述语言处理复数的各种方式,例如:

    • 韩语-无复数
    • 英语-两种形式,单数仅用于一种
    • 法语-两种形式,单数表示零和一
    • 波兰语-三种形式,一的特例和一些以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.
    
    3 回复  |  直到 8 年前
        1
  •  3
  •   AProgrammer    16 年前

    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);
    

    我不知道这是否真的是你想要的。如果你想使用一个更为C++语法的GETTeX,你必须寻找像Booo::Frand这样的库。

        2
  •  2
  •   unwind    16 年前

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

    它所做的只是在应用程序的消息数据库中查找消息的翻译,因此本例假设存在这样一个文件(其中 gettext() 如果你能找到它,这可能有点棘手)。

    接下来,你用错了。您链接到的页面描述了 ngettext()

    您的呼叫应该如下所示:

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

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

        3
  •  0
  •   flodin    16 年前

    你真的为不同的复数形式制作了一个.po文件吗?见 description of the gettext workflow on Wikipedia . 也读 gettxt documentation about plural forms 尤其是包含复数形式的.po文件的示例。