【问题标题】:GTK+ - 2.0 Localization Guide for GUI and Code bothGTK+ - GUI 和代码的 2.0 本地化指南
【发布时间】:2011-11-20 22:22:00
【问题描述】:

2.0 + MinGW32 + Windows 平台 + Netbeans IDE 来创建我的应用程序。我已经创建了前端,但现在我需要添加语言选择选项,我想要自动翻译,我是 GTK 的新手,所以我需要详细的帮助。我在谷歌上搜索,但在 Windows 上没有找到任何帮助,所以请尽快提供帮助:( 我使用 gtkBuilder 设计了我的布局。

我想知道完成它的确切步骤...

请重点介绍如何在windows中使用gettext()或_()以及什么是.po文件以及如何处理它们...

** 抱歉英语不好...

【问题讨论】:

    标签: localization gtk glib mingw32


    【解决方案1】:

    嗯,这取决于您使用的 Makefile 生成器。我认为您没有为此使用自动工具,因此您可能需要为此实现自己的逻辑。

    我是在 Windows/MinGW 上使用 CMake 完成的。生成 .po 文件所需遵循的工作流程位于 GNU gettext overview 中。基本上,xgettext 会解析您的代码以提取您要翻译的字符串。您可以向它传递一个关键字,通常是“_”,它标识代码中用于标记要翻译的字符串的 _() 宏。它将生成 .pot(PO 模板)文件。然后,您可以复制该文件并将其重命名为 .po 文件,并使用 poedit 等工具翻译字符串。

    您需要一些机制来更新您的 po 文件。这使用 msgmerge,它将新的 .pot 文件与现有的 .po 文件合并。它将添加要翻译的新字符串,并注释掉消失的字符串。

    不幸的是,这一切都与您的构建系统相关,因此没有一种单一的方法可以做到这一点。我使用 CMake,但您可以使用 shell 脚本或任何能够调用命令并生成文件的系统。

    希望这会有所帮助。

    【讨论】:

    • 你可以使用 bash 来做到这一点,还是禁止添加 bash 依赖项?
    • 是的,我不能使用 bash 脚本:(
    • 那么我想你必须从 .bat 脚本中调用 xgettext、msgmerge、msgfmt...
    • 最后我通过一个非常好的教程完成了这一切,thnx :)
    • @Waqar Ali Khan,看看这似乎是答案,你为什么不成为朋友并接受它呢? ;)
    【解决方案2】:
    cat >hellogt.cxx <<EOF
    // hellogt.cxx
    #include <libintl.h>
    #include <locale.h>
    #include <iostream>
    int main (){
        setlocale(LC_ALL, "");
        bindtextdomain("hellogt", ".");
        textdomain( "hellogt");
        std::cout << gettext("hello, world!") << std::endl;
    }
    EOF
    g++ -o hellogt hellogt.cxx
    xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx
    msginit --no-translator --locale es_MX --output-file hellogt_spanish.po --input hellogt.pot
    sed --in-place hellogt_spanish.po --expression='/"hello, world!"/,/#: / s/""/"hola mundo"/'
    mkdir --parents ./es_MX.utf8/LC_MESSAGES
    msgfmt --check --verbose --output-file ./es_MX.utf8/LC_MESSAGES/hellogt.mo hellogt_spanish.po
    LANGUAGE=es_MX.utf8 ./hellogt
    

    下面是上面创建的文件的说明:

    hellogt.cxx         C++ source file
    hellogt             Executable image
    hellogt.pot         Extracted text from C++ source file (portable object template)
    hellogt_spanish.po  Modified text for Spanish with translations added (using sed)
    es_MX.utf8/
     LC_MESSAGES/
       hellogt.mo       Binary translated text for Spanish used at run-time
    

    来源:Complete C++ i18n gettext() "hello world" example

    【讨论】:

    • 好吧,“sed”这一步几乎没用,翻译通常是使用外部工具完成的,比如 poedit,或者手动编辑 .po 文件。 hellogt.cxx 中的 gettext 调用也替换为对 _() 宏的调用,后者将其隐藏(然后将 --keyword=_ 选项传递给 xgettext 以识别标记为翻译的字符串)。 _T() 用于不应翻译的字符串。
    猜你喜欢
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2010-09-08
    • 2018-04-12
    • 2011-04-14
    相关资源
    最近更新 更多