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
来源:Complete C++ i18n gettext() "hello world" example
那么,“sed”步骤通常是无用的,翻译通常使用外部工具(如poedit)或手动编辑.po文件来完成。 hellogt.cxx中的gettext调用也被对_ _()宏的调用所取代,后者隐藏它(然后将--keyword = _选项传递给xgettext以识别标记为翻译的字符串)。 _T()用于不应该被翻译的字符串。 – liberforce