Como inicializar um char * estático com gettetxt () usando ambiente de sistema operacional local?

StackOverflow https://stackoverflow.com/questions/1114792

Pergunta

Existe uma maneira padrão ou comum em C ++ para lidar com cordas estáticas que precisam ser definidas por gettext()?

Aqui está um exemplo usando a resposta para completa C ++ gettext () “Olá mundo” exemplo como base apenas mudando o hello world literal a um char* hws estática e char* hw. Parece que hws está sendo inicializado para o padrão texto em Inglês antes da localidade está definida a partir do ambiente de sistema operacional local. Enquanto hw está ficando definido após a localidade é alterada produzindo assim o texto em espanhol.

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
Foi útil?

Solução

Você precisa dividir o uso gettext em duas partes. Primeiro, você só marcar a corda com uma macro, como gettext_noop, de modo que xgettext vai extraí-lo. Então, quando você se referir à variável global, você enrolar o acesso com a chamada gettext verdade.

Consulte Casos Especiais no manual gettext .

NB. Seus hws variáveis ??não é uma variável estática (não "palavra-chave estática"); é uma variável global.

Outras dicas

Este é o código após a aplicação do resposta de 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

O resultado tem o desejado e esperado de saída:

hola mundo static
hola mundo
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top