如何使用gettext呢?
安装gettext
1
2brew install gettext
brew link gettext --force创建本地化po文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14cat >hello.cxx <<EOF
// hello.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
int main (){
setlocale(LC_ALL, "");
bindtextdomain("hello", ".");
textdomain( "hello");
std::cout << gettext("hello, world!") << std::endl;
}
EOF
xgettext --package-name hello --package-version 1.2 --default-domain hello --msgid-bugs-address="denley@justtodo.com" --output hello.pot hello.cxx
msginit --output-file hello.po --input hello.pot可能需要输入邮箱地址,任意输入一个即可
汉化内容: 可能替换不成功,直接修改即可
1
sed -e '/"hello, world!"/,/#: / s/""/"你好,世界!"/' hello.po
测试
1
2
3
4mkdir -p zh_CN/LC_MESSAGES
msgfmt --check --verbose --output-file zh_CN/LC_MESSAGES/hello.mo hello.po
g++ -o hello hello.cxx -lintl
./hello