您好我正在用c ++编写i18n的示例代码。我引用了以下link。
输出显示英文文本。这有什么问题?
#include "start.hpp"
#include <iostream>
#include <locale.h>
#include "/opt/local/share/gettext/gettext.h"
using namespace std;
int main(int argc, char *argv[]){
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("hello", cwd) << std::endl;
std::cout << "textdomain(): " << textdomain( "hello") << std::endl;
std::cout << gettext("hello, world!") << std::endl;
return 0;
}
$&GT; g ++ -o start start.cpp
$&GT; xgettext -d start -o start.pot start.cpp
$&GT; msginit --no-translator -l es_MX -o start_es.po -i start.pot
创建了start_es.po。
我使用poedit.app打开了start_es.po文件并翻译了文本并保存了
$&GT; mkdir -p es / LC_MESSAGES
$&GT; msgfmt -c -v -o ./es/LC_MESSAGES/start.mo start_es.po
1个已翻译的消息。
$&gt; export LANG = es_MX
$&GT; ./start
答案 0 :(得分:0)
绑定域时代码中存在错误。我改变了如下
std::cout << "setlocale(): " << (s ? s : "NULL") << std::endl;
std::cout << "bindtextdomain(): " << bindtextdomain(“start", cwd)<< std::endl;
std::cout << "textdomain(): " << textdomain( “start") << std::endl;
以下追踪帮助我找到了。
dhcp-140:Debug UserName $ sudo dtruss -f -t open ./start
getenv(PWD): /Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug
getenv(LANG): NULL
setlocale(): NULL
bindtextdomain():
/Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug
textdomain(): hello
hello, world!
PID/THRD SYSCALL(args) = return
7213/0x16cd1: open(".\0", 0x0, 0x1) = 3 0
7213/0x16cd1: open("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x0, 0x0) = 3 0
7213/0x16cd1: open("/opt/local/lib/libintl.8.dylib\0", 0x0, 0x0) = 3 0
7213/0x16cd1: open("/opt/local/lib/libiconv.2.dylib\0", 0x0, 0x0) = 3 0
7213/0x16cd1: open("/dev/dtracehelper\0", 0x2, 0x7FFF540FDA20) = 3 0
7213/0x16cd1: open("/Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug/./start\0", 0x0, 0x1FF) = 3 0
7213/0x16cd1: open("/Users/UserName/WorkPlace/lang/DerivedData/lang/Build/Products/Debug/en/LC_MESSAGES/hello.mo\0", 0x0, 0x8) = -1 Err#2