Just for reference, an embedded platform based on a MCU with integrated Flash, for example a Cortex-Mx device. Here I consider only western languages (left-to-right and european chars, english, french, german, spanish and so on).
The main problem is the translation of strings, maybe 10-100 strings.
I know something about gettext package that can't be used in those embeded platforms. However I like the approach of gettext.
print_to_display(x, y, "Hello world!");
is simply changed in:
#include ... print_to_display(x, y, _("Hello world!"));
In this way, the code stays highly readble as before introducing the multi-language support. If a member of a structure needs a string, it is a char * as usual.
The solution I found in embedded platforms is to use an array of array of strings: one index for the string and one index for the language.
enum lang_t { ENGLISH, ITALIAN, LANG_N }; enum string_t { STR_HELLO_WORLD, STR_HOW_ARE_YOU };
const char *strings[STRING_N][LANG_N] = { { // STR_HELLO_WORLD { "Hello world!", "Ciao mondo" } }, { // STR_HOW_ARE_YOU { "How are you?", "Come stai?" } }, };
static enum lang_t lang = ENGLISH;
const char *_(int string_idx) { return strings[string_idx][lang]; }
void set_language(enum lang_t new_language) { lang = new_language; }
I don't like too much this approach for two reasons. The first, the line:
print_to_display(x, y, _(STR_HELLO_WORLD));
is much less readable than
print_to_display(x, y, _("Hello world!"));
The second, I need to change the type of some members/variables from char * to int:
struct mystruct { int title; // Instead of char *title ... };
Another approach I'm thinking is to embed all the translations in the string, using a separator character that can't be used in normal strings.
print_to_display(x, y, _("Hello world!|Ciao Mondo!"));
The _() function will search the translated string based on the current language. If he can't find, it could return the first translation (english).
This approach has some disadvantages. It's difficult to exclude one language from the build. If the languages are more than a couple, the strings will be very long. The order of the translations (first english, than italian, ...) is important and you have to remember it for every string.
What approach do you use?