aboutsummaryrefslogtreecommitdiffstats
path: root/src/gtrans/gtrans.c
blob: 15630b2032ca845b282754fda3977540ae863936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.io/license.html
 */

#include <string.h>
#include "../../gfx.h"

#if GFX_USE_GTRANS

static const transTable* _languageBase;
static const transTable* _languageCurrent;

void _gtransInit(void)
{
	_languageBase = 0;
	_languageCurrent = 0;
}

void _gtransDeinit(void)
{
}

const char* gtransString(const char* string)
{
	// Find the index of the specified string in the base language table
	unsigned i = 0;
	while (1) {
		// Prevent overflow
		if (i >= _languageBase->numEntries) {
			return string;
		}

		// Check if we found the string
		if (strcmp(string, _languageBase->strings[i]) == 0) {
			break;
		}

		// Otherwise keep going
		i++;
	}

	// Make sure that the index exists in the current language table
	if (i >= _languageCurrent->numEntries) {
		return string;
	}

	// Return the translated string
	return _languageCurrent->strings[i];
}

const char* gtransIndex(unsigned index)
{
	if (!_languageCurrent) {
		return 0;
	}

	if (index >= _languageCurrent->numEntries) {
		return 0;
	}

	return _languageCurrent->strings[index];
}

void gtransSetBaseLanguage(const transTable* const translation)
{
	_languageBase = translation;
}

void gtransSetLanguage(const transTable* const translation)
{
	_languageCurrent = translation;
}

#endif /* GFX_USE_GTRANS */