Is there a standard or common way in C++ to handle static strings that need to be set by gettext()
?
Here is an example using the answer to Complete C++ i18n gettext() “hello world” example as a base just changing the literal hello world
to a static char* hws
and char* hw
.
It looks like hws
is getting initialized to the default English text before the locale is set from the
local operating system environment. While hw
is getting set after the locale is changed thus producing the Spanish text.
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