Locale script
Some checks failed
Build Dusk / run-tests (push) Failing after 2m12s
Build Dusk / build-linux (push) Successful in 1m49s
Build Dusk / build-psp (push) Failing after 1m39s

This commit is contained in:
2026-01-27 09:52:08 -06:00
parent 2b9be6675c
commit c7b9a53535
4 changed files with 78 additions and 2 deletions

View File

@@ -14,12 +14,14 @@ localemanager_t LOCALE;
errorret_t localeManagerInit() {
memoryZero(&LOCALE, sizeof(localemanager_t));
errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US));
// errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US));
errorOk();
}
errorret_t localeManagerSetLocale(const dusklocale_t locale) {
assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale.");
assertTrue(locale != DUSK_LOCALE_NULL, "Cannot set locale to NULL.");
LOCALE.locale = locale;
char_t languageFile[FILENAME_MAX];
snprintf(

View File

@@ -9,6 +9,69 @@
#include "script/scriptcontext.h"
#include "locale/localemanager.h"
int moduleLocaleGet(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// No arguments expected
dusklocale_t locale = LOCALE.locale;
lua_pushinteger(L, (lua_Integer)locale);
return 1;
}
int moduleLocaleSet(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Requires locale ID
if(!lua_isinteger(L, 1)) {
luaL_error(L, "localeSet: Expected locale ID as first argument");
return 0;
}
errorret_t err;
dusklocale_t locale = (dusklocale_t)lua_tointeger(L, 1);
if(locale >= DUSK_LOCALE_COUNT || locale == DUSK_LOCALE_NULL) {
luaL_error(L, "localeSet: Invalid locale ID");
return 0;
}
err = localeManagerSetLocale(locale);
if(err.code != ERROR_OK) {
luaL_error(L, "localeSet: Failed to set locale");
return 0;
}
return 0;
}
int moduleLocaleGetName(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
// Optional ID, otherwise return current locale name
dusklocale_t locale = LOCALE.locale;
if(lua_gettop(L) >= 1) {
if(!lua_isinteger(L, 1)) {
luaL_error(L, "localeGetName: Expected locale ID as first argument");
return 0;
}
locale = (dusklocale_t)lua_tointeger(L, 1);
if(locale >= DUSK_LOCALE_COUNT || locale == DUSK_LOCALE_NULL) {
luaL_error(L, "localeGetName: Invalid locale ID");
return 0;
}
}
const char_t *localeName = LOCALE_INFOS[locale].file;
lua_pushstring(L, localeName);
return 1;
}
void moduleLocale(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Execute the locale script definitions.
scriptContextExec(context, LOCALE_SCRIPT);
scriptContextRegFunc(context, "localeGet", moduleLocaleGet);
scriptContextRegFunc(context, "localeSet", moduleLocaleSet);
scriptContextRegFunc(context, "localeGetName", moduleLocaleGetName);
}