C++ – How to support multiple languages in Linux C/C++ programs?

How to support multiple languages in Linux C/C++ programs?… here is a solution to the problem.

How to support multiple languages in Linux C/C++ programs?

For example, in this simplest hello world program:

#include <iostream>
int main()
{
    std::cout<<"Hello World!" <<std::endl;
    return 0;
}

If the user’s environment LANG is set to fr_FR, I expect to see French, it might look like this:

$ ./a.out
Hello World!

$ LANG=fr_FR.utf8
$ ./a.out
Bonjour tout le monde!

Is there a guide on how to achieve this in Linux?

Solution

The key is to use “resources” (one per language, configured to read at runtime) with hard-coded strings. GUI frameworks such as Qt and GTK+ make this (relatively) easy.

This is a link to the Pango library used by GTK+ (but not, emphatically, exclusive GTK+):

Here is a tutorial using Pango:

Here’s a tutorial about “gettext()” (which I believe Pango uses):

Related Problems and Solutions