Browse Source

Preliminary version of GTRANS

Joel Bodenmann 3 years ago
parent
commit
7b60003461
5 changed files with 109 additions and 3 deletions
  1. 1 1
      demos/modules/gtrans/basic/gfxconf.h
  2. 38 1
      demos/modules/gtrans/basic/main.c
  3. 10 0
      gfx.h
  4. 51 0
      src/gtrans/gtrans.c
  5. 9 1
      src/gtrans/gtrans.h

+ 1 - 1
demos/modules/gtrans/basic/gfxconf.h

@@ -42,7 +42,7 @@
42 42
 #define GDISP_NEED_VALIDATION	TRUE
43 43
 #define GDISP_NEED_CLIP			TRUE
44 44
 #define GDISP_NEED_TEXT			TRUE
45
-#define GDISP_NEED_ANTIALIAS	TRUE
45
+#define GDISP_NEED_ANTIALIAS	FALSE
46 46
 
47 47
 #define GDISP_INCLUDE_USER_FONTS				FALSE
48 48
 #define GDISP_INCLUDE_FONT_UI1					FALSE

+ 38 - 1
demos/modules/gtrans/basic/main.c

@@ -28,10 +28,47 @@
28 28
  */
29 29
 
30 30
 #include "gfx.h"
31
- 
31
+
32
+static const char* EnglishStrings[] = {
33
+    "Welcome",
34
+    "The number %s has the value %d",
35
+    "Goodbye"
36
+};
37
+static const transTable EnglishTranslation = { sizeof(EnglishStrings)/sizeof(EnglishStrings[0]), EnglishStrings };
38
+
39
+static const char* GermanStrings[] = {
40
+    "Herzlich Willkommen",
41
+    "Die Zahl %s hat den Wert %d",
42
+    "Auf Wiedersehen"
43
+};
44
+static const transTable GermanTranslation = { sizeof(GermanStrings)/sizeof(GermanStrings[0]), GermanStrings };
45
+
32 46
 int main(void)
33 47
 {
48
+    size_t i, j;
49
+    font_t font;
50
+
34 51
     gfxInit();
52
+    gdispClear(Silver);
53
+
54
+    font = gdispOpenFont("*");
55
+
56
+    gtransSetBaseLanguage(&EnglishTranslation);
57
+    gtransSetLanguage(&GermanTranslation);
58
+
59
+    gtransSetLanguage(&EnglishTranslation);
60
+    i = 0;
61
+    for (j = 0; j < EnglishTranslation.numEntries; j++) {
62
+        gdispFillStringBox(20+300*i, 35*j, 300, 35, gtransIndex(j), font, Black, Silver, justifyLeft);
63
+    }
64
+
65
+    gtransSetLanguage(&GermanTranslation);
66
+    i = 1;
67
+    for (j = 0; j < EnglishTranslation.numEntries; j++) {
68
+        gdispFillStringBox(20+300*i, 35*j, 300, 35, gtransIndex(j), font, Black, Silver, justifyLeft);
69
+    }
70
+
71
+    gdispFillStringBox(20, 300, 300, 25, gtransString("Welcome"), font, Black, Silver, justifyLeft);
35 72
 
36 73
 	while (TRUE) {
37 74
 		gfxSleepMilliseconds(500);

+ 10 - 0
gfx.h

@@ -145,6 +145,13 @@
145 145
 	#ifndef GFX_USE_GFILE
146 146
 		#define GFX_USE_GFILE	FALSE
147 147
 	#endif
148
+	/**
149
+	 * @brief   GFX Translation Support API
150
+	 * @details	Defaults to FALSE
151
+	 */
152
+	#ifndef GFX_USE_GTRANS
153
+		#define GFX_USE_GTRANS	FALSE
154
+	#endif
148 155
 /** @} */
149 156
 
150 157
 /**
@@ -155,6 +162,7 @@
155 162
 #include "src/gdriver/gdriver_options.h"
156 163
 #include "src/gfile/gfile_options.h"
157 164
 #include "src/gmisc/gmisc_options.h"
165
+#include "src/gtrans/gtrans_options.h"
158 166
 #include "src/gqueue/gqueue_options.h"
159 167
 #include "src/gevent/gevent_options.h"
160 168
 #include "src/gtimer/gtimer_options.h"
@@ -181,6 +189,7 @@
181 189
 #include "src/gtimer/gtimer_rules.h"
182 190
 #include "src/gqueue/gqueue_rules.h"
183 191
 #include "src/gmisc/gmisc_rules.h"
192
+#include "src/gtrans/gtrans_rules.h"
184 193
 #include "src/gfile/gfile_rules.h"
185 194
 #include "src/gdriver/gdriver_rules.h"
186 195
 #include "src/gos/gos_rules.h"
@@ -192,6 +201,7 @@
192 201
 //#include "src/gdriver/gdriver.h"			// This module is only included by source that needs it.
193 202
 #include "src/gfile/gfile.h"
194 203
 #include "src/gmisc/gmisc.h"
204
+#include "src/gtrans/gtrans.h"
195 205
 #include "src/gqueue/gqueue.h"
196 206
 #include "src/gevent/gevent.h"
197 207
 #include "src/gtimer/gtimer.h"

+ 51 - 0
src/gtrans/gtrans.c

@@ -5,16 +5,67 @@
5 5
  *              http://ugfx.org/license.html
6 6
  */
7 7
 
8
+#include <string.h>
8 9
 #include "../../gfx.h"
9 10
 
10 11
 #if GFX_USE_GTRANS
11 12
 
13
+static const transTable* _languageBase;
14
+static const transTable* _languageCurrent;
15
+
12 16
 void _gtransInit(void)
13 17
 {
18
+	_languageBase = 0;
19
+	_languageCurrent = 0;
14 20
 }
15 21
 
16 22
 void _gtransDeinit(void)
17 23
 {
18 24
 }
19 25
 
26
+const char* gtransString(const char* string)
27
+{
28
+	size_t i = 0;
29
+	while (1) {
30
+		if (i >= _languageBase->numEntries-1) {
31
+			return 0;
32
+		}
33
+
34
+		if (strcmp(string, _languageBase->strings[i]) == 0) {
35
+			break;
36
+		}
37
+
38
+		i++;
39
+	}
40
+
41
+	if (i >= _languageCurrent->numEntries-1) {
42
+		return 0;
43
+	}
44
+
45
+	return _languageCurrent->strings[i];
46
+}
47
+
48
+const char* gtransIndex(unsigned index)
49
+{
50
+	if (!_languageCurrent) {
51
+		return 0;
52
+	}
53
+
54
+	if (index >= _languageCurrent->numEntries) {
55
+		return 0;
56
+	}
57
+
58
+	return _languageCurrent->strings[index];
59
+}
60
+
61
+void gtransSetBaseLanguage(const transTable* const translation)
62
+{
63
+	_languageBase = translation;
64
+}
65
+
66
+void gtransSetLanguage(const transTable* const translation)
67
+{
68
+	_languageCurrent = translation;
69
+}
70
+
20 71
 #endif /* GFX_USE_GTRANS */

+ 9 - 1
src/gtrans/gtrans.h

@@ -22,11 +22,19 @@
22 22
 
23 23
 #if GFX_USE_GTRANS || defined(__DOXYGEN__)
24 24
 
25
+typedef struct transTable {
26
+	unsigned numEntries;
27
+	const char** strings;
28
+} transTable;
29
+
25 30
 #ifdef __cplusplus
26 31
 extern "C" {
27 32
 #endif
28 33
 
29
-
34
+const char* gtransString(const char* string);
35
+const char* gtransIndex(unsigned index);
36
+void gtransSetBaseLanguage(const transTable* const translation);
37
+void gtransSetLanguage(const transTable* const translation);
30 38
 
31 39
 #ifdef __cplusplus
32 40
 }