diff --git a/src/dusk/CMakeLists.txt b/src/dusk/CMakeLists.txt index 168bf64..dbb7017 100644 --- a/src/dusk/CMakeLists.txt +++ b/src/dusk/CMakeLists.txt @@ -26,6 +26,7 @@ add_subdirectory(assert) add_subdirectory(display) add_subdirectory(entity) add_subdirectory(item) +add_subdirectory(locale) add_subdirectory(physics) add_subdirectory(ui) add_subdirectory(util) diff --git a/src/dusk/entity/npc.c b/src/dusk/entity/npc.c index 0e4f74d..217a24f 100644 --- a/src/dusk/entity/npc.c +++ b/src/dusk/entity/npc.c @@ -6,8 +6,8 @@ */ #include "npc.h" - #include "ui/uitextbox.h" +#include "locale/language.h" void npcInit(entity_t *entity) { @@ -18,10 +18,11 @@ void npcUpdate(entity_t *entity) { } void npcInteract(entity_t *player, entity_t *self) { - uiTextboxSetText( - "Hello World how are you today? Hope things are going well for you! I am " - "having a great day, hope you are too. Did I ever tell you about the tragedy " - "of Darth Plagueis the Wise? He was a dark lord of the Sith, " - "so powerful and so wise he could use the Force to influence the midichlorians" - ); + // uiTextboxSetText( + // "Hello World how are you today? Hope things are going well for you! I am " + // "having a great day, hope you are too. Did I ever tell you about the tragedy " + // "of Darth Plagueis the Wise? He was a dark lord of the Sith, " + // "so powerful and so wise he could use the Force to influence the midichlorians" + // ); + uiTextboxSetText(languageGet("meta.language.name")); } \ No newline at end of file diff --git a/src/dusk/locale/CMakeLists.txt b/src/dusk/locale/CMakeLists.txt new file mode 100644 index 0000000..ef60e6d --- /dev/null +++ b/src/dusk/locale/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + language.c +) \ No newline at end of file diff --git a/src/dusk/locale/language.c b/src/dusk/locale/language.c new file mode 100644 index 0000000..0fc6899 --- /dev/null +++ b/src/dusk/locale/language.c @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "language.h" +#include "assert/assert.h" + +language_t LANGUAGE; + +void languageInit(void) { + LANGUAGE.current = LANGUAGE_EN; +} + +const char_t * languageGet(const char_t *key) { + assertNotNull(key, "Key cannot be NULL"); + assertTrue(LANGUAGE.current < LANGUAGE_COUNT, "Invalid language index"); + + int16_t keyIndex = -1; + for(uint16_t i = 0; i < LANGUAGE_COUNT; i++) { + if(strcmp(LANGUAGE_KEYS[LANGUAGE.current][i], key) != 0) { + continue; + } + keyIndex = i; + break; + } + assertTrue(keyIndex != -1, "Key not found in language"); + return LANGUAGE_VALUES[LANGUAGE.current][keyIndex]; +} + +uint16_t langaugeGetLength(const char_t *key) { + assertNotNull(key, "Key cannot be NULL"); + assertTrue(LANGUAGE.current < LANGUAGE_COUNT, "Invalid language index"); + + int16_t keyIndex = -1; + for(uint16_t i = 0; i < LANGUAGE_COUNT; i++) { + if(strcmp(LANGUAGE_KEYS[LANGUAGE.current][i], key) != 0) { + continue; + } + keyIndex = i; + break; + } + assertTrue(keyIndex != -1, "Key not found in language"); + return strlen(LANGUAGE_VALUES[LANGUAGE.current][keyIndex]); +} \ No newline at end of file diff --git a/src/dusk/locale/language.h b/src/dusk/locale/language.h new file mode 100644 index 0000000..ce521ed --- /dev/null +++ b/src/dusk/locale/language.h @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "locale/language/languages.h" + +typedef struct { + uint8_t current; +} language_t; + +extern language_t LANGUAGE; + +/** + * Initializes the language system. + * + * This function sets up the language system, loading the default language + * and preparing any necessary resources for language handling. + */ +void languageInit(void); + +/** + * Gets a language string by its key. + * + * @param key The key for the language string. + * @return The language string associated with the key. + */ +const char_t * languageGet(const char_t *key); + +/** + * Gets the length of a language string by its key. + * + * @param key The key for the language string. + * @return The length of the language string associated with the key. + */ +uint16_t langaugeGetLength(const char_t *key); \ No newline at end of file diff --git a/tools/languagecompile/languagecompile.py b/tools/languagecompile/languagecompile.py index 61e26e4..d745e80 100644 --- a/tools/languagecompile/languagecompile.py +++ b/tools/languagecompile/languagecompile.py @@ -77,7 +77,7 @@ for jsonFile in jsonFiles: f.write(f"#define LANGUAGE_{langUpper}_CODE \"{languageName}\"\n") f.write(f"#define LANGUAGE_{langUpper}_NAME \"{keyValues['meta.language.name']}\"\n") - f.write(f"#define LANGUAGE_{langUpper}_COUNT_KEYS {len(keyValues)}\n\n") + f.write(f"#define LANGUAGE_{langUpper}_COUNT {len(keyValues)}\n\n") # Write keys f.write(f"static const char_t *LANGUAGE_{langUpper}_KEYS[] = {{\n") @@ -113,24 +113,35 @@ with open(mainOutputFile, 'w', encoding='utf-8') as f: f.write(f'#include "locale/language/{lang.lower()}.h"\n') f.write("\n") - f.write(f"#define LANGUAGES_COUNT {len(languages)}\n\n") + f.write(f"#define LANGUAGE_COUNT {len(languages)}\n\n") - f.write("static const char_t *LANGUAGE_CODES[] = {\n") + index = 0 + for lang in languages: + f.write(f"#define LANGUAGE_{lang} {index}\n") + index += 1 + f.write("\n") + + f.write("static const char_t* LANGUAGE_CODES[] = {\n") for lang in languages: f.write(f' LANGUAGE_{lang}_CODE,\n') f.write("};\n\n") - f.write("static const char_t *LANGUAGE_NAMES[] = {\n") + f.write("static const char_t* LANGUAGE_NAMES[] = {\n") for lang in languages: f.write(f' LANGUAGE_{lang}_NAME,\n') f.write("};\n\n") - f.write("static const char_t *LANGUAGE_KEYS[] = {\n") + f.write("static const char_t** LANGUAGE_KEYS[] = {\n") for lang in languages: f.write(f' LANGUAGE_{lang}_KEYS,\n') f.write("};\n\n") - f.write("static const char_t *LANGUAGE_VALUES[] = {\n") + f.write("static const int LANGUAGE_COUNTS[] = {\n") + for lang in languages: + f.write(f' LANGUAGE_{lang}_COUNT,\n') + f.write("};\n\n") + + f.write("static const char_t** LANGUAGE_VALUES[] = {\n") for lang in languages: f.write(f' LANGUAGE_{lang}_VALUES,\n') f.write("};\n\n") \ No newline at end of file