完整的 C++ i18n gettext() "hello world" 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1003360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:22:00  来源:igfitidea点击:

Complete C++ i18n gettext() "hello world" example

c++linuxinternationalizationgettext

提问by CW Holeman II

I am looking for a complete i18n gettext()hello world example. I have started a script based upon A tutorial on Native Language Support using GNU gettextby G. Mohanty. I am using Linux and G++.

我正在寻找一个完整的 i18n gettext()hello world 示例。我已经根据G. Mohanty 的使用 GNU gettext本地语言支持教程启动了一个脚本。我正在使用 Linux 和 G++。

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

The program compiles, the text is extracted, Spanish file is created, modified and binary created but hellogt still displays English. The trace shows no evidence of looking in the current working directory for es_MX nor any references to LC_MESSAGES directory.

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

采纳答案by CW Holeman II

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

Here is a description of the files created by the above:

下面是对上面创建的文件的描述:

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

回答by DaveR

Your problem is that hellogt.mois in the wrong location - your program isn't actually opening it. You can tell this by using straceto trace opensyscalls:

您的问题是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)

You can affect where gettext looks for message catalogs with the LOCPATHenvironment variable, but if you move it to where gettext is attempting to load it from your example works:

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

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

回答by Lunar Mushrooms

Here is a description of gettext from Fedora Project. It is simple to follow. But it is in C.

这是 Fedora 项目中 gettext 的描述。遵循起来很简单。但它在 C 中。

http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext

http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext