2009-06-16 7 views
15

Je suis à la recherche d'un complet i18n gettext() exemple de bonjour monde. J'ai commencé un script basé sur A tutorial on Native Language Support using GNU gettext par G. Mohanty. J'utilise Linux et G ++.Complete C + + i18n gettext() "Bonjour tout le monde" exemple

code:

cat >hellogt.cxx <<EOF 
// hellogt.cxx 
#include <libintl.h> 
#include <locale.h> 
#include <iostream> 
#include <cstdlib> 
int main(){ 
    char* cwd = getenv("PWD"); 
    std::cout << "getenv(PWD): " << (cwd?cwd:"NULL") << std::endl; 
    char* l = getenv("LANG"); 
    std::cout << "getenv(LANG): " << (l?l:"NULL") << std::endl; 
    char* s = setlocale(LC_ALL, ""); 
    std::cout << "setlocale(): " << (s?s:"NULL") << std::endl; 
    std::cout << "bindtextdomain(): " << bindtextdomain("hellogt", cwd) << std::endl; 
    std::cout << "textdomain(): " << textdomain("hellogt") << std::endl; 
    std::cout << gettext("hello, world!") << std::endl; 
} 
EOF 
g++ -ohellogt hellogt.cxx 
xgettext -d hellogt -o hellogt.pot hellogt.cxx 
msginit --no-translator -l es_MX -o hellogt_spanish.po -i hellogt.pot 
sed --in-place hellogt_spanish.po --expression='/#: /,$ s/""/"hola mundo"/' 
sed --in-place hellogt_spanish.po --expression='s/PACKAGE VERSION/hellogt 1.0/' 
mkdir -p ./es_MX/LC_MESSAGES 
msgfmt -c -v -o ./es_MX/LC_MESSAGES/hellogt.mo hellogt_spanish.po 
export LANG=es_MX 
ls -l $PWD/es_MX/LC_MESSAGES/hellogt.mo 
./hellogt 
strace -e trace=open ./hellogt 

Le programme compile, le texte est extrait, le fichier espagnol est créé, modifié et binaire créé, mais hellogt affiche toujours l'anglais. La trace ne montre aucune indication de recherche dans le répertoire de travail actuel pour es_MX ni aucune référence au répertoire LC_MESSAGES.

Répondre

11
cat >hellogt.cxx <<EOF 
// hellogt.cxx 
#include <libintl.h> 
#include <locale.h> 
#include <iostream> 
int main(){ 
    setlocale(LC_ALL, ""); 
    bindtextdomain("hellogt", "."); 
    textdomain("hellogt"); 
    std::cout << gettext("hello, world!") << std::endl; 
} 
EOF 
g++ -o hellogt hellogt.cxx 
xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx 
msginit --no-translator --locale es_MX --output-file hellogt_spanish.po --input hellogt.pot 
sed --in-place hellogt_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/hellogt.mo hellogt_spanish.po 
LANGUAGE=es_MX.utf8 ./hellogt 

Voici une description des fichiers créés par ce qui précède:

hellogt.cxx   C++ source file 
hellogt    Executable image 
hellogt.pot   Extracted text from C++ source file (portable object template) 
hellogt_spanish.po Modified text for Spanish with translations added (using sed) 
es_MX.utf8/ 
LC_MESSAGES/ 
    hellogt.mo  Binary translated text for Spanish used at run-time 
13

Votre problème est que hellogt.mo est au mauvais endroit - votre programme ne l'ouvre pas réellement. Vous pouvez le dire en utilisant strace pour tracer open syscalls:

strace -e trace=open ./hellogt 
... 
open("/tmp/.//es_MX/LC_MESSAGES/hellogt.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 
open("/tmp/.//es/LC_MESSAGES/hellogt.mo", O_RDONLY) = -1 ENOENT (No such file or directory) 

Vous pouvez affecter l'endroit où gettext cherche des catalogues de messages avec la variable d'environnement LOCPATH, mais si vous déplacez à l'endroit où gettext tente de le charger de votre exemple fonctionne:

mkdir -p es/LC_MESSAGES 
cp hellogt.mo es/LC_MESSAGES 
./hellogt 
hola mundo 
+0

Merci. La trace m'a aidé à identifier que le code que j'avais ne regardait pas les fichiers comme je m'y attendais. –

Questions connexes