2009-07-11 4 views
1

Есть ли стандартный или распространенный способ в C++ для обработки статических строк, которые должны быть установлены gettext()?Как инициализировать статический char * с помощью gettetxt() с использованием локальной операционной системы?

Вот пример, используя ответ на Complete C++ i18n gettext() “hello world” example в качестве базы только изменения буквального hello world к статическому char* hws и char* hw. Похоже, что hws инициализируется текстовым текстом по умолчанию, прежде чем языковой стандарт будет установлен в локальной среде операционной системы . Пока hw получает набор после изменения локали, создавая испанский текст.

cat >hellostaticgt.cxx <<EOF 
// hellostaticgt.cxx 
#include <libintl.h> 
#include <locale.h> 
#include <iostream> 
char* hws = gettext("hello, world static!"); 
int main(){ 
    setlocale(LC_ALL, ""); 
    bindtextdomain("hellostaticgt", "."); 
    textdomain("hellostaticgt"); 
    char* hw = gettext("hello, world!"); 
    std::cout << hws << std::endl; 
    std::cout << hw << std::endl; 
} 
EOF 
g++ -o hellostaticgt hellostaticgt.cxx 
xgettext --package-name hellostaticgt --package-version 1.0 --default-domain hellostaticgt --output hellostaticgt.pot hellostaticgt.cxx 
msginit --no-translator --locale es_MX --output-file hellostaticgt_spanish.po --input hellostaticgt.pot 
sed --in-place hellostaticgt_spanish.po --expression='/#: /,$ s/""/"hola mundo"/' 
mkdir --parents ./es_MX.utf8/LC_MESSAGES 
msgfmt --check --verbose --output-file ./es_MX.utf8/LC_MESSAGES/hellostaticgt.mo hellostaticgt_spanish.po 
LANGUAGE=es_MX.utf8 ./hellostaticgt 

ответ

1

Необходимо разделить использование gettext на две части. Во-первых, вы просто отмечаете строку макросом, например gettext_noop, так что xgettext будет извлекать его. Затем, когда вы ссылаетесь на глобальную переменную, вы завершаете доступ с помощью истинного вызова gettext.

См. Special Cases в руководстве gettext.

N.B. Ваша переменная hws не является статической переменной (нет «статического ключевого слова»); это глобальная переменная.

+0

Измененный форматирование "статических" в "статический символ * HWS". B.Stroustrup, C++ Programming Language Third Edition, стр. 83 утверждает, что глобальное, пространство имен и локальные статические объекты совместно называются статическими объектами. –

0

Это код после применения answer from Martin v. Löwis:

cat >hellostaticgt.cxx <<EOF 
// hellostaticgt.cxx 
#include <libintl.h> 
#include <locale.h> 
#include <iostream> 
#define gettext_noop(S) S 
const char* hws_eng = gettext_noop("hello, world static!"); 
int main(){ 
    setlocale(LC_ALL, ""); 
    bindtextdomain("hellostaticgt", "."); 
    textdomain("hellostaticgt"); 
    char* hw = gettext("hello, world!"); 
    std::cout << gettext(hws_eng) << std::endl; 
    std::cout << hw << std::endl; 
} 
EOF 
g++ -o hellostaticgt hellostaticgt.cxx 
xgettext --package-name hellostaticgt --package-version 1.0 --default-domain hellostaticgt --output hellostaticgt.pot hellostaticgt.cxx 
msginit --no-translator --locale es_MX --output-file hellostaticgt_spanish.po --input hellostaticgt.pot 
sed --in-place hellostaticgt_spanish.po --expression='/"hello, world static!"/,/#:/s/""/"hola mundo static"/' 
sed --in-place hellostaticgt_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/hellostaticgt.mo hellostaticgt_spanish.po 
LANGUAGE=es_MX.utf8 ./hellostaticgt 

Результат имеет желаемый и ожидаемый результат:

hola mundo static 
hola mundo