GTRANS

From uGFX Wiki
Revision as of 10:54, 9 February 2016 by Tectu (Talk | contribs) (Example)

Jump to: navigation, search

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)

Optimization

Calling gt() (or gtransString() directly) uses the strcmp() function of the standard C library to compare strings. This operation can take a lot of time. For performance critical applications or low-resource systems the application translation speed can be optimized by using gtransIndex() (instead of gtransString()) which directly takes the index of the string in the translation table. However, this introduces the limitation that each string in each of the translation tables need to be at the same position (the same index). Using gtransString() allows to have translation tables of unequal sizes due to the nature of the translation being based on string comparison rather than index accessing of the table.

Example

Complete examples can be found under /demos/modules/gtrans/ in the uGFX library directory.

Basic Example

The following code is a very simple usage example showing to use the GTRANS API.

#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;
}

Example with widgets

The following example demonstrates how the language of an application using widgets can be translated using GTRANS. The user can select the language using buttons.

ToDo