Comment initialiser un char * statique avec gettetxt () en utilisant l'environnement du système d'exploitation local?

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

Question

Y at-il un moyen standard ou commune en C ++ pour gérer les chaînes statiques qui doivent être fixés par gettext()?

Voici un exemple en utilisant la réponse à complète C ++ i18n gettext () « Bonjour tout le monde » par exemple comme base juste changer le hello world littéral à un char* hws statique et char* hw. Il ressemble à hws est initialisé à obtenir le texte anglais par défaut avant le paramètre régional est défini à partir du environnement du système d'exploitation local. Alors que hw se prépare après la locale est modifiée produisant ainsi le texte espagnol.

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
Était-ce utile?

La solution

Vous devez diviser gettext utilisation en deux parties. Tout d'abord, vous marquez simplement la chaîne avec une macro, comme gettext_noop, de sorte que xgettext va extraire. Ensuite, lorsque vous faites référence à la variable globale, vous enveloppez l'accès à la véritable appel gettext.

Voir Cas particuliers dans le manuel gettext .

N.B.. Votre hws variable n'est pas une variable statique (pas de « mot-clé statique »); il est une variable globale.

Autres conseils

Ceci est le code après l'application de la réponse de 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

Le résultat est la sortie désirée et attendue:

hola mundo static
hola mundo
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top