2009-06-16 35 views
15

Tôi đang tìm một ví dụ thế giới i18n gettext() hoàn chỉnh. Tôi đã bắt đầu một tập lệnh dựa trên A tutorial on Native Language Support using GNU gettext bởi G. Mohanty. Tôi đang sử dụng Linux và G ++.Hoàn thành C++ i18n gettext() "hello world" ví dụ

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 

Chương trình biên dịch, các văn bản được chiết xuất, tập tin Tây Ban Nha được tạo ra, sửa đổi và nhị phân tạo nhưng hellogt vẫn hiển thị bằng tiếng Anh. Dấu vết cho thấy không có bằng chứng về tìm kiếm trong thư mục làm việc hiện tại cho es_MX cũng như không có bất kỳ tham chiếu đến thư mục LC_MESSAGES.

Trả lời

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 

Dưới đây là một mô tả của các tập tin được tạo ra bởi trên:

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

Vấn đề của bạn là hellogt.mo ở sai vị trí - chương trình của bạn không thực sự mở nó. Bạn có thể nói điều này bằng cách sử dụng strace để theo dõi 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) 

bạn có thể ảnh hưởng đến nơi gettext tìm kiếm catalog nhắn với biến LOCPATH môi trường, nhưng nếu bạn di chuyển nó đến nơi gettext đang cố gắng để tải nó từ ví dụ của bạn hoạt động:

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

Cảm ơn. Các dấu vết đã giúp xác định rằng mã tôi đã không nhìn vào các tập tin như tôi mong đợi. –