Wie eine statische char * mit gettetxt () unter Verwendung von lokaler Betriebssystemumgebung initialisieren?

StackOverflow https://stackoverflow.com/questions/1114792

Frage

Gibt es einen Standard oder gemeinsamen Weg in C ++ statische Strings zu behandeln, die durch gettext() eingestellt werden müssen?

Hier ist ein Beispiel der Antwort auf komplett C ++ i18n mit gettext () „Hallo Welt“ -Beispiel als Basis nur den wörtlichen hello world zu einer statischen char* hws und char* hw ändern. Es sieht aus wie hws wird auf den Standard englischen Text initialisiert zu werden, bevor das Gebietsschema von der festgelegt ist lokale Betriebssystemumgebung. Während hw gesetzt zu werden, nachdem die locale so geändert werden, um den spanischen Text zu erzeugen.

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
War es hilfreich?

Lösung

Sie müssen Verwendung in zwei Teile aufzuspalten gettext. Zuerst markieren Sie nur die Zeichenfolge mit einem Makro, wie gettext_noop, so dass xgettext es extrahieren. Dann, wenn Sie auf die globale Variable beziehen, können Sie den Zugriff mit dem wahren gettext Anruf wickeln.

Siehe Sonderfälle im gettext Handbuch .

N. B. Ihre variable hws ist keine statische Variable (kein „statisches Schlüsselwort“); es ist eine globale Variable.

Andere Tipps

Dies ist der Code nach der Antwort von 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

Das Ergebnis hat die gewünschte und erwartete Ausgabe:

hola mundo static
hola mundo
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top