Search code examples
c++internationalizationgettext

How to trace the C++ GNU xgettex execution?


Hi I am writing a sample code for i18n in c++. I referenced the following link.

The output displays the text in english. What is the problem here?

 #include "start.hpp"
 #include <iostream>
 #include <locale.h>
 #include "/opt/local/share/gettext/gettext.h"
 using namespace std;

int main(int argc, char *argv[]){

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("hello", cwd) <<         std::endl;
std::cout << "textdomain(): " << textdomain( "hello") << std::endl;
std::cout << gettext("hello, world!") << std::endl;

return 0;

}

$> g++ -o start start.cpp

$> xgettext -d start -o start.pot start.cpp

$> msginit --no-translator -l es_MX -o start_es.po -i start.pot

Created start_es.po.

I opened the start_es.po files using poedit.app and translated the text and saved

$> mkdir -p es/LC_MESSAGES

$> msgfmt -c -v -o ./es/LC_MESSAGES/start.mo start_es.po

1 translated message.

$>export LANG=es_MX

$> ./start


Solution

  • There is a bug in the code while binding domains. I changed that as follows

    std::cout << "setlocale(): " << (s ? s : "NULL") << std::endl;
    std::cout << "bindtextdomain(): " << bindtextdomain(“start", cwd)<< std::endl;
    std::cout << "textdomain(): " << textdomain( “start") << std::endl;
    

    The following trace helped me to find that out.

     dhcp-140:Debug UserName $ sudo dtruss -f -t open ./start
    getenv(PWD): /Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug
    getenv(LANG): NULL
    setlocale(): NULL
    bindtextdomain(): 
    
    /Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug
    textdomain(): hello
    hello, world!
        PID/THRD  SYSCALL(args)          = return
     7213/0x16cd1:  open(".\0", 0x0, 0x1)        = 3 0
     7213/0x16cd1:  open("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x0, 0x0)     = 3 0
     7213/0x16cd1:  open("/opt/local/lib/libintl.8.dylib\0", 0x0, 0x0)       = 3 0
     7213/0x16cd1:  open("/opt/local/lib/libiconv.2.dylib\0", 0x0, 0x0)      = 3 0
     7213/0x16cd1:  open("/dev/dtracehelper\0", 0x2, 0x7FFF540FDA20)         = 3 0
     7213/0x16cd1:  open("/Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug/./start\0", 0x0, 0x1FF)       = 3 0
     7213/0x16cd1:  open("/Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug/en/LC_MESSAGES/hello.mo\0", 0x0, 0x8)         = -1 Err#2