ugfx/src/gtrans/gtrans.c

78 lines
1.4 KiB
C
Raw Normal View History

2016-02-07 20:57:03 +00:00
/*
* 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.org/license.html
*/
2016-02-07 21:59:35 +00:00
#include <string.h>
2016-02-07 20:57:03 +00:00
#include "../../gfx.h"
#if GFX_USE_GTRANS
2016-02-07 21:59:35 +00:00
static const transTable* _languageBase;
static const transTable* _languageCurrent;
2016-02-07 20:57:03 +00:00
void _gtransInit(void)
{
2016-02-07 21:59:35 +00:00
_languageBase = 0;
_languageCurrent = 0;
2016-02-07 20:57:03 +00:00
}
void _gtransDeinit(void)
{
}
2016-02-07 21:59:35 +00:00
const char* gtransString(const char* string)
{
2016-02-07 23:03:30 +00:00
// Find the index of the specified string in the base language table
2016-02-07 21:59:35 +00:00
size_t i = 0;
while (1) {
2016-02-07 23:03:30 +00:00
// Prevent overflow
2016-02-09 11:53:38 +00:00
if (i >= _languageBase->numEntries) {
2016-02-07 23:03:30 +00:00
return string;
2016-02-07 21:59:35 +00:00
}
2016-02-07 23:03:30 +00:00
// Check if we found the string
2016-02-07 21:59:35 +00:00
if (strcmp(string, _languageBase->strings[i]) == 0) {
break;
}
2016-02-07 23:03:30 +00:00
// Otherwise keep going
2016-02-07 21:59:35 +00:00
i++;
}
2016-02-07 23:03:30 +00:00
// Make sure that the index exists in the current language table
2016-02-09 11:53:38 +00:00
if (i >= _languageCurrent->numEntries) {
2016-02-07 23:03:30 +00:00
return string;
2016-02-07 21:59:35 +00:00
}
2016-02-07 23:03:30 +00:00
// Return the translated string
2016-02-07 21:59:35 +00:00
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;
}
2016-02-07 20:57:03 +00:00
#endif /* GFX_USE_GTRANS */