1

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

这是一个使用Complete C++ i18n gettext() “hello world” 示例的答案作为基础的示例,只是将文字更改hello world为静态char* hwschar* hw. hws在从本地操作系统环境设置语言环境之前,它看起来正在初始化为默认的英文文本。Whilehw是在更改语言环境后设置的,从而生成西班牙语文本。

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
4

2 回答 2

1

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

请参阅gettext 手册中的特殊情况

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

于 2009-07-12T04:52:32.793 回答
0

这是应用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
于 2009-07-12T16:57:28.397 回答