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.
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. –