From 6b07b2af74b25f75e6006c13da4826568d0ea3c1 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Sun, 7 Feb 2016 21:57:03 +0100 Subject: GTRANS module skeleton --- src/gfx.c | 10 ++++++++++ src/gfx_mk.c | 1 + src/gtrans/gtrans.c | 20 ++++++++++++++++++++ src/gtrans/gtrans.h | 39 +++++++++++++++++++++++++++++++++++++++ src/gtrans/gtrans.mk | 6 ++++++ src/gtrans/gtrans_mk.c | 8 ++++++++ src/gtrans/gtrans_options.h | 21 +++++++++++++++++++++ src/gtrans/gtrans_rules.h | 22 ++++++++++++++++++++++ 8 files changed, 127 insertions(+) create mode 100644 src/gtrans/gtrans.c create mode 100644 src/gtrans/gtrans.h create mode 100644 src/gtrans/gtrans.mk create mode 100644 src/gtrans/gtrans_mk.c create mode 100644 src/gtrans/gtrans_options.h create mode 100644 src/gtrans/gtrans_rules.h (limited to 'src') diff --git a/src/gfx.c b/src/gfx.c index 34526e6b..ba7a581e 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -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..7e1105d6 --- /dev/null +++ b/src/gtrans/gtrans.c @@ -0,0 +1,20 @@ +/* + * 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 "../../gfx.h" + +#if GFX_USE_GTRANS + +void _gtransInit(void) +{ +} + +void _gtransDeinit(void) +{ +} + +#endif /* GFX_USE_GTRANS */ diff --git a/src/gtrans/gtrans.h b/src/gtrans/gtrans.h new file mode 100644 index 00000000..c46b5b28 --- /dev/null +++ b/src/gtrans/gtrans.h @@ -0,0 +1,39 @@ +/* + * 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 to allow 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__) + +#ifdef __cplusplus +extern "C" { +#endif + + + +#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 */ +/** @} */ -- cgit v1.2.3 From 7b6000346133a34600058931c7dbe2cfbbe1559d Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Sun, 7 Feb 2016 22:59:35 +0100 Subject: Preliminary version of GTRANS --- src/gtrans/gtrans.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/gtrans/gtrans.h | 10 +++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gtrans/gtrans.c b/src/gtrans/gtrans.c index 7e1105d6..4961cdcb 100644 --- a/src/gtrans/gtrans.c +++ b/src/gtrans/gtrans.c @@ -5,16 +5,67 @@ * http://ugfx.org/license.html */ +#include #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) +{ + size_t i = 0; + while (1) { + if (i >= _languageBase->numEntries-1) { + return 0; + } + + if (strcmp(string, _languageBase->strings[i]) == 0) { + break; + } + + i++; + } + + if (i >= _languageCurrent->numEntries-1) { + return 0; + } + + 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 index c46b5b28..f09d90e1 100644 --- a/src/gtrans/gtrans.h +++ b/src/gtrans/gtrans.h @@ -22,11 +22,19 @@ #if GFX_USE_GTRANS || defined(__DOXYGEN__) +typedef struct transTable { + unsigned numEntries; + const char** strings; +} transTable; + #ifdef __cplusplus extern "C" { #endif - +const char* gtransString(const char* string); +const char* gtransIndex(unsigned index); +void gtransSetBaseLanguage(const transTable* const translation); +void gtransSetLanguage(const transTable* const translation); #ifdef __cplusplus } -- cgit v1.2.3 From 39c13d16455d31040956952bce8e5fe3f81d161e Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Mon, 8 Feb 2016 00:03:30 +0100 Subject: Adding documentation to GTRANS --- src/gtrans/gtrans.c | 10 ++++++++-- src/gtrans/gtrans.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/gtrans/gtrans.c b/src/gtrans/gtrans.c index 4961cdcb..21508567 100644 --- a/src/gtrans/gtrans.c +++ b/src/gtrans/gtrans.c @@ -25,23 +25,29 @@ 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-1) { - return 0; + 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-1) { - return 0; + return string; } + // Return the translated string return _languageCurrent->strings[i]; } diff --git a/src/gtrans/gtrans.h b/src/gtrans/gtrans.h index f09d90e1..da3cae5e 100644 --- a/src/gtrans/gtrans.h +++ b/src/gtrans/gtrans.h @@ -10,7 +10,7 @@ * * @addtogroup GTRANS * - * @brief Module to allow changing the language of an application dynamically during run-time. + * @brief Module that allows changing the language of an application dynamically during run-time. * * @{ */ @@ -22,18 +22,66 @@ #if GFX_USE_GTRANS || defined(__DOXYGEN__) +/** + * @struct transTable + * @brief A table containing translated strings. + */ typedef struct transTable { - unsigned numEntries; - const char** strings; + 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 @@ -44,4 +92,3 @@ void gtransSetLanguage(const transTable* const translation); #endif /* _TRANS_H */ /** @} */ - -- cgit v1.2.3 From 249ffe781f826b2d39d634023ea5334dc900df26 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Tue, 9 Feb 2016 12:53:38 +0100 Subject: Fixing bug in GTRANS --- src/gtrans/gtrans.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/gtrans/gtrans.c b/src/gtrans/gtrans.c index 21508567..07409838 100644 --- a/src/gtrans/gtrans.c +++ b/src/gtrans/gtrans.c @@ -29,7 +29,7 @@ const char* gtransString(const char* string) size_t i = 0; while (1) { // Prevent overflow - if (i >= _languageBase->numEntries-1) { + if (i >= _languageBase->numEntries) { return string; } @@ -43,7 +43,7 @@ const char* gtransString(const char* string) } // Make sure that the index exists in the current language table - if (i >= _languageCurrent->numEntries-1) { + if (i >= _languageCurrent->numEntries) { return string; } -- cgit v1.2.3