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