سؤال

وأنا أبحث عن وI18N gettext() كاملة مرحبا سبيل المثال العالم. لقد بدأت السيناريو على أساس وهناك أمثلة توضيحية على دعم اللغة الأصلية باستخدام GNU gettext التي كتبها G. موهانتي. أنا أستخدم لينكس و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 لتتبع syscalls 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)

وأنت يمكن أن تؤثر حيث يبدو gettext لكتالوجات رسالة مع متغير البيئة LOCPATH، ولكن إذا كنت نقله إلى حيث gettext تحاول تحميله من المثال الخاص بك يعمل:

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

وهنا هو وصف لgettext من مشروع فيدورا. انها بسيطة لمتابعة. لكنه في C.

http://fedoraproject.org/wiki/How_to_do_I18N_through_gettext

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top