【问题标题】:How to initialize a static char* with gettetxt() using local operating system environment?如何使用本地操作系统环境使用 gettetxt() 初始化静态 char*?
【发布时间】:2009-07-11 22:17:19
【问题描述】:

C++ 中是否有标准或通用的方法来处理需要由gettext() 设置的静态字符串?

这是一个使用Complete C++ i18n gettext() “hello world” example 的答案作为基础的示例,只需将文字hello world 更改为静态char* hwschar* 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

【问题讨论】:

    标签: c++ linux internationalization gettext


    【解决方案1】:

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

    请参阅 gettext 手册中的Special Cases

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

    【讨论】:

    • 更改了“static char* hws”中“static”的格式。 B.Stroustrup,C++ 编程语言第三版,第 83 页指出,全局、命名空间和局部静态对象统称为静态对象。
    【解决方案2】:

    这是应用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-18
      • 2014-04-15
      • 2011-01-12
      • 2010-10-05
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多