1

で設定する必要がある静的文字列を処理する C++ の標準または一般的な方法はありgettext()ますか?

これは、完全な C++ i18n gettext() “hello world”の例への回答をベースとして、リテラルhello worldを静的なchar* hwsandに変更するだけの例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
4

2 に答える 2

1

gettext の使用法を 2 つの部分に分割する必要があります。まず、xgettext が抽出できるように、gettext_noop などのマクロで文字列をマークするだけです。次に、グローバル変数を参照するときに、真の 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 に答える