C++ – How to use the features of C++ to make cleaner use of i18n library over direct use of gettext() with C

cgettextinternationalization

I would like to have an easy to use way to write code like:

#include <iostream>
int main (){
    std::cout << "hello, world!\n";
}

but that supports i18n. Here is an example using gettext():

#include <libintl.h>
#include <iostream>
int main (){
    std::cout << gettext("hello, world!\n");
}

This can then be processed by xgettext to produce a message catalog file that can be used
by translators to create various versions. These extra files can be handled on target
systems to allow the user to interact in a preferred language.

I would like to write the code something like this instead:

#include <i18n-iostream>
int main (){
    i18n::cout << "hello, world!\n";
}

At build time the quoted strings would be examined by a program like xgettext to produce the
base message catalog file. << operator with argument i18n::cout would take a string
literal as the key to lookup the run-time text to use from a message catalog.

Does it exist somewhere?

Best Answer

At build time the quoted strings would be examined by a program like xgettext to produce the base message catalog file. << operator with argument i18n::cout would take a string literal as the key to lookup the run-time text to use from a message catalog.

You try to convert a string like a single instance, but it isn't/

The point, you don't want something like this. Think of:

if(n=1)
    i18n::cout << "I need one apple"
else
    i18n::cout << "I need " << n << " apples" ;

So why this is would not work, because "n=1" or "n!=1" works only for English, many other languages have more then one plural form, also it requires translation of "I need X apples" as signle instance.

I suggest you just to learn to deal with gettext, it is quite simple and powerful, many people had thought about it.

Another point, you are usually do not call gettext but

#include <libintl.h>
#include <iostream>
#define _(x) gettext(x)

int main (){
    std::cout << _("hello, world!\n");
}

This makes the code much cleaner, also it is quite a "standard" feature to use "_" as gettext alias.

Just learn how to use it, before you try to make "nicer" API. Just to mention, gettext API is quite de-facto standard for many languages, not only C.