2009-07-11 11 views
1

Existe-t-il un moyen standard ou commun en C++ de gérer les chaînes statiques qui doivent être définies par gettext()?Comment initialiser un char statique * avec gettetxt() en utilisant un environnement de système d'exploitation local?

Voici un exemple en utilisant la réponse à Complete C++ i18n gettext() “hello world” example comme base changeant juste le hello world littéral à un char* hws et char* hw statique. Il semble que hws soit initialisé au texte anglais par défaut avant que les paramètres régionaux ne soient définis dans l'environnement de système d'exploitation local . Alors que hw est défini après la modification des paramètres régionaux, le texte en espagnol est généré.

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 

Répondre

1

Vous devez diviser l'utilisation de gettext en deux parties. D'abord, vous marquez simplement la chaîne avec une macro, telle que gettext_noop, de sorte que xgettext l'extraira. Ensuite, lorsque vous faites référence à la variable globale, vous enveloppez l'accès avec l'appel true gettext.

Voir Special Cases dans le manuel gettext.

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

+0

mise en forme Changed de "statique" dans "static char * hws". B.Stroustrup, La troisième édition du langage de programmation C++, page 83, indique que les objets globaux, les espaces de noms et les objets statiques locaux sont collectivement appelés des objets statiques. –

0

Ceci est le code après application du answer from 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 a souhaité et attendu sortie:

hola mundo static 
hola mundo 
Questions connexes