µGFX  2.9
version 2.9
gtrans.c
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 #include <string.h>
9 #include "../../gfx.h"
10 
11 #if GFX_USE_GTRANS
12 
13 static const transTable* _languageBase;
14 static const transTable* _languageCurrent;
15 
16 void _gtransInit(void)
17 {
18  _languageBase = 0;
19  _languageCurrent = 0;
20 }
21 
22 void _gtransDeinit(void)
23 {
24 }
25 
26 const char* gtransString(const char* string)
27 {
28  // Find the index of the specified string in the base language table
29  unsigned i = 0;
30  while (1) {
31  // Prevent overflow
32  if (i >= _languageBase->numEntries) {
33  return string;
34  }
35 
36  // Check if we found the string
37  if (strcmp(string, _languageBase->strings[i]) == 0) {
38  break;
39  }
40 
41  // Otherwise keep going
42  i++;
43  }
44 
45  // Make sure that the index exists in the current language table
46  if (i >= _languageCurrent->numEntries) {
47  return string;
48  }
49 
50  // Return the translated string
51  return _languageCurrent->strings[i];
52 }
53 
54 const char* gtransIndex(unsigned index)
55 {
56  if (!_languageCurrent) {
57  return 0;
58  }
59 
60  if (index >= _languageCurrent->numEntries) {
61  return 0;
62  }
63 
64  return _languageCurrent->strings[index];
65 }
66 
67 void gtransSetBaseLanguage(const transTable* const translation)
68 {
69  _languageBase = translation;
70 }
71 
72 void gtransSetLanguage(const transTable* const translation)
73 {
74  _languageCurrent = translation;
75 }
76 
77 #endif /* GFX_USE_GTRANS */
const char * gtransString(const char *string)
Get the string of the current language specified by the string of the base language.
void gtransSetLanguage(const transTable *const translation)
Set the current language.
const char * gtransIndex(unsigned index)
Get the string at the specified index position of the current language.
void gtransSetBaseLanguage(const transTable *const translation)
Set the base language.
A table containing translated strings.
Definition: gtrans.h:29
unsigned numEntries
Definition: gtrans.h:30
const char ** strings
Definition: gtrans.h:31