Difference between revisions of "GTRANS"

From uGFX Wiki
Jump to: navigation, search
(Optimization)
Line 22: Line 22:
  
 
== Optimization ==
 
== Optimization ==
Calling <code>gt()</code> (or <code>gtransString()</code> directly) uses the <code>strcmp()</code> 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 <code>gtransIndex()</code> (instead of <code>gtransString()</code>) which directly takes the index of the string in the translation table.
+
Calling <code>gt()</code> (or <code>gtransString()</code> directly) uses the <code>strcmp()</code> 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 <code>gtransIndex()</code> (instead of <code>gtransString()</code>) 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 <code>gtransString()</code> allows to have translation tables of unequal sizes due to the nature of the translation being based on string compare rather than index accessing of the table.
  
 
== Example ==
 
== Example ==

Revision as of 10:50, 9 February 2016

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 compare rather than index accessing of the table.

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