GTRANS
The GTRANS module allows to manage language translations. The language of an application can be changed dynamically during run-time using the GTRANS module.
API reference
The API reference of the GTRANS module can be found here.
Intended usage
Each translation is specified by a transTable
struct which is essentially a table of strings:
typedef struct transTable { unsigned numEntries; // The number of strings that this table contains const char** strings; // The translated strings } transTable;
A translatable application needs to have a base language. All translations happen relative to that base language. The base language is specified using gtransSetBaseLanguage()
. The current language of the application is set using gtransSetLanguage()
.
The actual translations take place by calling gtransString()
. A string contained in the translation table of the base language is passed to gtransString()
. The function returns the corresponding string of the current language that was set using gtransSetLanguage()
or the passed string if none was found.
A wrapper macro named gt()
around gtransString()
is available to make writing and reading translatable applications easier:
#define gt(str) gtransString(str)
Example
#include "gfx.h" static const char* EnglishStrings[] = { "Welcome", "The number %s has the value %d", "Goodbye" }; static const transTable EnglishTranslation = { sizeof(EnglishStrings)/sizeof(EnglishStrings[0]), EnglishStrings }; static const char* GermanStrings[] = { "Herzlich Willkommen", "Die Zahl %s hat den Wert %d", "Auf Wiedersehen" }; static const transTable GermanTranslation = { sizeof(GermanStrings)/sizeof(GermanStrings[0]), GermanStrings }; int main(void) { size_t i, j; font_t font; gfxInit(); gdispClear(Silver); font = gdispOpenFont("*"); gtransSetBaseLanguage(&EnglishTranslation); gtransSetLanguage(&GermanTranslation); gtransSetLanguage(&EnglishTranslation); i = 0; for (j = 0; j < EnglishTranslation.numEntries; j++) { gdispFillStringBox(20+300*i, 35*j, 300, 35, gtransIndex(j), font, Black, Silver, justifyLeft); } gtransSetLanguage(&GermanTranslation); i = 1; for (j = 0; j < EnglishTranslation.numEntries; j++) { gdispFillStringBox(20+300*i, 35*j, 300, 35, gtransIndex(j), font, Black, Silver, justifyLeft); } gdispFillStringBox(20, 300, 300, 25, gt("Welcome"), font, Black, Silver, justifyLeft); while (TRUE) { gfxSleepMilliseconds(500); } return 0; }