[ACCEPTED]-Complete C++ i18n gettext() "hello world" example-gettext

Accepted answer
Score: 16

Your problem is that hellogt.mo is in the wrong location 6 - your program isn't actually opening it. You 5 can tell this by using strace to trace 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)

You 4 can affect where gettext looks for message 3 catalogs with the LOCPATH environment variable, but 2 if you move it to where gettext is attempting 1 to load it from your example works:

mkdir -p es/LC_MESSAGES
cp hellogt.mo es/LC_MESSAGES
./hellogt 
hola mundo
Score: 15
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

Here is a description of the files created 1 by the above:

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
Score: 3

Here is a description of gettext from Fedora 2 Project. It is simple to follow. But it 1 is in C.

http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext

More Related questions