2009-06-16 119 views
15

我正在尋找一個完整的i18n gettext() hello world示例。我已經根據G. Mohanty的A tutorial on Native Language Support using GNU gettext開始了一個腳本。我正在使用Linux和G ++。完整的C++ i18n gettext()「hello world」示例

代碼:

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 

程序編譯,文字提取,西班牙語文件被創建,修改和二進制創建的,但hellogt仍顯示英文。跟蹤顯示沒有在當前工作目錄中查找es_MX的任何證據,也沒有任何對LC_MESSAGES目錄的引用。

回答

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 

這裏是由上面創建的文件的描述:

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

您的問題是hellogt.mo位於錯誤的位置 - 您的程序實際上並未打開它。您可以通過使用strace告訴這個跟蹤open系統調用:

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) 

可以影響地方的gettext查找與LOCPATH環境變量的消息目錄,但如果你將它移動到的gettext試圖從你的例子加載工作原理:

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

謝謝。跟蹤有助於確定我的代碼沒有按照我的預期查看這些文件。 – 2009-06-23 15:39:28