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

如何使用本地操作系统环境使用gettetxt()初始化静态char*?

  •  1
  • CW Holeman II  · 技术社区  · 17 年前

    gettext()

    以下是一个使用以下答案的示例 Complete C++ i18n gettext() “hello world” example 作为基础,只是改变字面意思 hello world char* hws char* hw . hws hw

    cat >hellostaticgt.cxx <<EOF
    // hellostaticgt.cxx
    #include <libintl.h>
    #include <locale.h>
    #include <iostream>
    char* hws = gettext("hello, world static!");
    int main (){
        setlocale(LC_ALL, "");
        bindtextdomain("hellostaticgt", ".");
        textdomain( "hellostaticgt");
        char* hw = gettext("hello, world!");
        std::cout << hws << std::endl;
        std::cout << hw << std::endl;
    }
    EOF
    g++ -o hellostaticgt hellostaticgt.cxx
    xgettext --package-name hellostaticgt --package-version 1.0 --default-domain hellostaticgt --output hellostaticgt.pot hellostaticgt.cxx
    msginit --no-translator --locale es_MX --output-file hellostaticgt_spanish.po --input hellostaticgt.pot
    sed --in-place hellostaticgt_spanish.po --expression='/#: /,$ s/""/"hola mundo"/'
    mkdir --parents ./es_MX.utf8/LC_MESSAGES
    msgfmt --check --verbose --output-file ./es_MX.utf8/LC_MESSAGES/hellostaticgt.mo hellostaticgt_spanish.po
    LANGUAGE=es_MX.utf8 ./hellostaticgt
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   Martin v. Löwis    17 年前

    您需要将gettext用法分为两部分。首先,你只需用一个宏标记字符串,比如gettext_noop,这样xgettext就会提取它。然后,当你引用全局变量时,你用真正的gettext调用来包装访问。

    Special Cases 在gettext手册中。

        2
  •  0
  •   Community Mohan Dere    9 年前

    这是应用后的代码 answer from Martin v. Löwis

    cat >hellostaticgt.cxx <<EOF
    // hellostaticgt.cxx
    #include <libintl.h>
    #include <locale.h>
    #include <iostream>
    #define gettext_noop(S) S
    const char* hws_eng = gettext_noop("hello, world static!");
    int main (){
        setlocale(LC_ALL, "");
        bindtextdomain("hellostaticgt", ".");
        textdomain( "hellostaticgt");
        char* hw = gettext("hello, world!");
        std::cout << gettext(hws_eng) << std::endl;
        std::cout << hw << std::endl;
    }
    EOF
    g++ -o hellostaticgt hellostaticgt.cxx
    xgettext --package-name hellostaticgt --package-version 1.0 --default-domain hellostaticgt --output hellostaticgt.pot hellostaticgt.cxx
    msginit --no-translator --locale es_MX --output-file hellostaticgt_spanish.po --input hellostaticgt.pot
    sed --in-place hellostaticgt_spanish.po --expression='/"hello, world static!"/,/#: / s/""/"hola mundo static"/'
    sed --in-place hellostaticgt_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/hellostaticgt.mo hellostaticgt_spanish.po
    LANGUAGE=es_MX.utf8 ./hellostaticgt
    

    结果具有期望和预期的输出:

    hola mundo static
    hola mundo