Added basic locale support

This commit is contained in:
2025-06-22 19:42:42 -05:00
parent 8ab17dae6c
commit 140f4f1ca2
6 changed files with 122 additions and 13 deletions

View File

@ -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)

View File

@ -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"));
}

View File

@ -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
)

View File

@ -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]);
}

View File

@ -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);

View File

@ -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")