diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gfx.c | 10 | ||||
-rw-r--r-- | src/gfx_mk.c | 1 | ||||
-rw-r--r-- | src/gtrans/gtrans.c | 77 | ||||
-rw-r--r-- | src/gtrans/gtrans.h | 94 | ||||
-rw-r--r-- | src/gtrans/gtrans.mk | 6 | ||||
-rw-r--r-- | src/gtrans/gtrans_mk.c | 8 | ||||
-rw-r--r-- | src/gtrans/gtrans_options.h | 21 | ||||
-rw-r--r-- | src/gtrans/gtrans_rules.h | 22 |
8 files changed, 239 insertions, 0 deletions
@@ -73,6 +73,10 @@ extern void _gosDeinit(void); extern void _gmiscInit(void); extern void _gmiscDeinit(void); #endif +#if GFX_USE_GTRANS + extern void _gtransInit(void); + extern void _gtransDeinit(void); +#endif void gfxInit(void) { @@ -96,6 +100,9 @@ void gfxInit(void) #if GFX_USE_GMISC _gmiscInit(); #endif + #if GFX_USE_GTRANS + _gtransInit(); + #endif #if GFX_USE_GEVENT _geventInit(); #endif @@ -159,6 +166,9 @@ void gfxDeinit(void) #if GFX_USE_GEVENT _geventDeinit(); #endif + #if GFX_USE_GTRANS + _gtransDeinit(); + #endif #if GFX_USE_GMISC _gmiscDeinit(); #endif diff --git a/src/gfx_mk.c b/src/gfx_mk.c index 34e6afd8..625ba680 100644 --- a/src/gfx_mk.c +++ b/src/gfx_mk.c @@ -18,3 +18,4 @@ #include "gaudio/gaudio_mk.c" #include "gmisc/gmisc_mk.c" #include "gfile/gfile_mk.c" +#include "gtrans/gtrans_mk.c" diff --git a/src/gtrans/gtrans.c b/src/gtrans/gtrans.c new file mode 100644 index 00000000..07409838 --- /dev/null +++ b/src/gtrans/gtrans.c @@ -0,0 +1,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.org/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 + size_t 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 */ diff --git a/src/gtrans/gtrans.h b/src/gtrans/gtrans.h new file mode 100644 index 00000000..da3cae5e --- /dev/null +++ b/src/gtrans/gtrans.h @@ -0,0 +1,94 @@ +/* + * 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 + */ + +/** + * @file src/gtrans/gtrans.h + * + * @addtogroup GTRANS + * + * @brief Module that allows changing the language of an application dynamically during run-time. + * + * @{ + */ + +#ifndef _TRANS_H +#define _TRANS_H + +#include "../../gfx.h" + +#if GFX_USE_GTRANS || defined(__DOXYGEN__) + +/** + * @struct transTable + * @brief A table containing translated strings. + */ +typedef struct transTable { + unsigned numEntries; /**< The number of strings that this table contains */ + const char** strings; /**< The translated strings */ +} transTable; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief A wrapper macro to make writing and reading translatable applications easier. + */ +#define gt(str) gtransString(str) + +/** + * @brief Get the string of the current language specified by the string of the base language. + * + * @details This function will return the string of the current language that corresponds to + * the specified string in the base language. + * @details This function uses strcmp() internally to compare strings. + * + * @param[in] string The string to translate. + * + * @return The corresponding string of the current language or the string passed as a parameter if it doesn't exist. + */ +const char* gtransString(const char* string); + +/** + * @brief Get the string at the specified index position of the current language. + * + * @details Getting translation strings is a lot faster using the index as an accessor rather + * than the string in the base language. + * + * @param[in] index The index of the string in the current language translation table. + * + * @return The string at the given index of the current language or 0 if it doesn't exist. + */ +const char* gtransIndex(unsigned index); + +/** + * @brief Set the base language. + * + * @details A translatable application needs to have a base language. All translations will + * be relative to this base language. + * + * @param[in] translation The translation table + */ +void gtransSetBaseLanguage(const transTable* const translation); + +/** + * @brief Set the current language. + * + * @details All translations will refer to the current language set by calling this function. + * + * @param[in] translation The translation table + */ +void gtransSetLanguage(const transTable* const translation); + +#ifdef __cplusplus +} +#endif + +#endif /* GFX_USE_GTRANS */ + +#endif /* _TRANS_H */ +/** @} */ diff --git a/src/gtrans/gtrans.mk b/src/gtrans/gtrans.mk new file mode 100644 index 00000000..e2f474f3 --- /dev/null +++ b/src/gtrans/gtrans.mk @@ -0,0 +1,6 @@ +# 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 + +GFXSRC += $(GFXLIB)/src/gtrans/gtrans.c diff --git a/src/gtrans/gtrans_mk.c b/src/gtrans/gtrans_mk.c new file mode 100644 index 00000000..57557509 --- /dev/null +++ b/src/gtrans/gtrans_mk.c @@ -0,0 +1,8 @@ +/* + * 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 + */ + +#include "gtrans.c" diff --git a/src/gtrans/gtrans_options.h b/src/gtrans/gtrans_options.h new file mode 100644 index 00000000..04193f85 --- /dev/null +++ b/src/gtrans/gtrans_options.h @@ -0,0 +1,21 @@ +/* + * 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 + */ + +/** + * @file src/gtrans/gtrans_options.h + * + * @addtogroup GTRANS + * @{ + */ + +#ifndef _GTRANS_OPTIONS_H +#define _GTRANS_OPTIONS_H + + + +#endif /* _GTRANS_OPTIONS_H */ +/** @} */ diff --git a/src/gtrans/gtrans_rules.h b/src/gtrans/gtrans_rules.h new file mode 100644 index 00000000..e5d383ce --- /dev/null +++ b/src/gtrans/gtrans_rules.h @@ -0,0 +1,22 @@ +/* + * 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 + */ + +/** + * @file src/gtrans/gtrans_rules.h + * + * @addtogroup GTRANS + * @{ + */ + +#ifndef _GTRANS_RULES_H +#define _GTRANS_RULES_H + +#if GFX_USE_GTRANS +#endif + +#endif /* _GTRANS_RULES_H */ +/** @} */ |