如何使用本地操作系统环境使用getetxt()初始化静态字符*? C++中有一种标准的或通用的方法来处理需要由代码> GETTrEXE()/?< /P>设置的静态字符串。
下面是一个示例,使用的答案作为基础,只是将文本如何使用本地操作系统环境使用getetxt()初始化静态字符*? C++中有一种标准的或通用的方法来处理需要由代码> GETTrEXE()/?< /P>设置的静态字符串。,c++,linux,internationalization,gettext,C++,Linux,Internationalization,Gettext,下面是一个示例,使用的答案作为基础,只是将文本hello world更改为静态char*hws和char*hw。 似乎在从设置语言环境之前,hws已初始化为默认英文文本 本地操作系统环境。而hw则在更改区域设置后进行设置,从而生成西班牙语文本 cat >hellostaticgt.cxx <<EOF // hellostaticgt.cxx #include <libintl.h> #include <locale.h> #include <ios
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
cat>hellostaticgt.cxx您需要将gettext的用法分为两部分。首先,您只需使用宏标记字符串,例如gettext_noop,以便xgettext将其提取出来。然后,当您引用全局变量时,使用true gettext调用包装访问
请参阅gettext手册中的
注意:您的变量hws不是静态变量(无“静态关键字”);它是一个全局变量。您需要将gettext用法分为两部分。首先,您只需使用宏标记字符串,例如gettext_noop,以便xgettext将其提取出来。然后,当您引用全局变量时,使用true gettext调用包装访问
请参阅gettext手册中的
注意:您的变量hws不是静态变量(无“静态关键字”);它是一个全局变量。这是应用以下命令后的代码:
这是应用以下命令后的代码:
更改了“static char*hws”中“static”的格式。B. Stroustrup,C++编程语言第三版,PG 83声明全局、命名空间和本地静态对象统称为静态对象。B. Stroustrup,C++编程语言第三版,PG 83声明全局、命名空间和本地静态对象统称为静态对象。
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