c ++ i18n getText ()“Hello World”예제 완료
-
05-07-2019 - |
문제
나는 완전한 i18n을 찾고 있습니다 gettext()
안녕하세요 세계 예. 나는 그에 따라 대본을 시작했습니다 GNU GetText를 사용한 모국어 지원에 대한 튜토리얼 G. Mohanty. 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
SYSCALLS :
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)
GetText가 메시지 카탈로그를 찾는 위치에 영향을 줄 수 있습니다. LOCPATH
환경 변수이지만 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
제휴하지 않습니다 StackOverflow