我正在寻找一个完整的i18n gettext() hello world示例。我已经开始使用基于使用GNU gettext的本地语言支持教程的脚本。莫汉蒂。我正在使用Linux和G ++。

代码:

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

程序编译,文本被提取,西班牙文件被创建,修改和二进制创建但是hellogt仍然显示英语。跟踪显示没有证据显示es_MX的当前工作目录以及对LC_MESSAGES目录的任何引用。

有帮助吗?

解决方案 2

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

其他提示

您的问题是 hellogt.mo 位于错误的位置 - 您的程序实际上并没有打开它。你可以通过 strace 来跟踪 open 系统调用来告诉你:

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)

您可以使用 LOCPATH 环境变量来影响gettext查找消息目录的位置,但是如果您将其移动到gettext尝试从示例工作中加载它的位置:

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

以下是来自Fedora Project的gettext的描述。它很容易遵循。但它在C中。

http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top