#include
#include
#include
#define USE_INTNTEXT
#ifdef USE_INTNTEXT
#include /* gettext 函数支持 */
#include /* 本地化locale的翻译支持 */
#else
#define gettext(string) (string)
#endif
int main(int argc, char **argv)
{
char test_pack[32];
char file_path[256];
strcpy(test_pack, "test_pkg");
#ifdef USE_INTNTEXT
/* 设置locale为系统默认语言环境 */
/* setlocale (LC_ALL, ""); */
setlocale (LC_MESSAGES, "");
setlocale (LC_CTYPE, "");
setlocale (LC_COLLATE, "");
/* 关联包名称及其对应的国际化翻译语言所在路径 */
/* bindtextdomain (pgm_name, "/usr/share/locale/"); */
getcwd(file_path, 256);
strcat(file_path, "/i18n");
//bindtextdomain (pgm_name, NULL);
bindtextdomain (test_pack, file_path); /* $`pwd`/i18n/ll_CC/LC_MESSAGES */
textdomain (test_pack); /* 设定.mo文件的名字,省略“.mo”后缀(可以多个路径下的不同mo文件) */
#endif
printf( "i18n: %s\n", gettext("Hello") );
return 0;
}
/****************************************************************************
#!/bin/bash
#
# 备注:需先安装gettext软件包和strace软件包(debian系统)
#
cat > hello.cxx <// hello.cxx
#include
#include
#include
#include
int main ()
{
char* cwd = getenv("PWD");
std::cout << "getenv(PWD): " << ( (cwd != NULL) ? cwd : "NULL" ) << std::endl;
char* lang = getenv("LANG");
std::cout << "getenv(LANG): " << ( (lang != NULL) ? lang : "NULL" ) << std::endl;
char* loca = setlocale(LC_ALL, "");
std::cout << "setlocale(): " << ( (loca != NULL) ? loca : "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;
}
EOF
g++ -o test hello.cxx
xgettext -d hello -o hello.pot hello.cxx
msginit --no-translator -l zh_CN.GB18030 -o hello_zhCN.po -i hello.pot
sed -e '/#: /,$ s/""/"你好"/' hello_zhCN.po
sed -e 's/PACKAGE VERSION/hello 1.0/' hello_zhCN.po
mkdir -p ./zh_CN/LC_MESSAGES
msgfmt -c -v -o ./zh_CN/LC_MESSAGES/hello.mo hello_zhCN.po
export LANG=zh_CN.GB18030
ls -l $PWD/zh_CN/LC_MESSAGES/hello.mo
./test
strace -e trace=open ./test
# end
****************************************************************************/
阅读(1062) | 评论(0) | 转发(0) |