Complete C++ I18N Gettext() "Hello World" Example

Complete C++ i18n gettext() hello world example

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

Here is a description of the files created by the above:

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

I18n C++ hello world with plurals

I'm not sure what you want. If it is slight modification of your example that give your wanted output, just replace the printf line by

printf(ngettext("Hello world with %d moon\n", "Hello world with %d moons\n", ii), ii);

but as it is a trivial modification of unwind's answer and the gettext documentation has the very similar example,

printf (ngettext ("%d file removed", "%d files removed", n), n);

I wonder if it is really what you wanted. If you want to use gettext with a more C++ syntax, you'll have to look for libraries like Boost::Format.

How to initialize a static char* with gettetxt() using local operating system environment?

You need to split gettext usage into two parts. First, you just mark the string with a macro, such as gettext_noop, so that xgettext will extract it. Then, when you refer to the the global variable, you wrap the access with the true gettext call.

See Special Cases in the gettext manual.

N.B. Your variable hws is not a static variable (no "static keyword"); it is a global variable.

Proper Hello World in C

I believe this is a standard Hello World program in C:

#include <stdio.h>

int main(void)
{
printf("Hello World\n");
return 0;
}


Related Topics



Leave a reply



Submit