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

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

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

    在C++中是否有一个标准或常见的方法来处理静态字符串,这些字符串需要由 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 回复  |  直到 7 年前
        1
  •  1
  •   Martin v. Löwis    15 年前

    您需要将gettext的用法分为两部分。首先,您只需使用宏标记字符串,例如gettext_noop,以便xgettext将其提取出来。然后,当您引用全局变量时,使用true gettext调用包装访问。

    看见 Special Cases

    注意:您的变量hws不是静态变量(无“静态关键字”);它是一个全局变量。

        2
  •  0
  •   Community CDub    7 年前

    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