Locale script
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
module('platform')
|
module('platform')
|
||||||
module('input')
|
module('input')
|
||||||
module('scene')
|
module('scene')
|
||||||
|
module('locale')
|
||||||
|
|
||||||
-- Default Input bindings.
|
-- Default Input bindings.
|
||||||
if PLATFORM == "psp" then
|
if PLATFORM == "psp" then
|
||||||
@@ -38,4 +39,5 @@ else
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- sceneSet('map')
|
-- sceneSet('map')
|
||||||
-- mapLoad('map/testmap/testmap.dmf')
|
-- mapLoad('map/testmap/testmap.dmf')
|
||||||
|
localeSet(DUSK_LOCALE_EN_US)
|
||||||
@@ -14,12 +14,14 @@ localemanager_t LOCALE;
|
|||||||
|
|
||||||
errorret_t localeManagerInit() {
|
errorret_t localeManagerInit() {
|
||||||
memoryZero(&LOCALE, sizeof(localemanager_t));
|
memoryZero(&LOCALE, sizeof(localemanager_t));
|
||||||
errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US));
|
// errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US));
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t localeManagerSetLocale(const dusklocale_t locale) {
|
errorret_t localeManagerSetLocale(const dusklocale_t locale) {
|
||||||
assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale.");
|
assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale.");
|
||||||
|
assertTrue(locale != DUSK_LOCALE_NULL, "Cannot set locale to NULL.");
|
||||||
|
|
||||||
LOCALE.locale = locale;
|
LOCALE.locale = locale;
|
||||||
char_t languageFile[FILENAME_MAX];
|
char_t languageFile[FILENAME_MAX];
|
||||||
snprintf(
|
snprintf(
|
||||||
|
|||||||
@@ -9,6 +9,69 @@
|
|||||||
#include "script/scriptcontext.h"
|
#include "script/scriptcontext.h"
|
||||||
#include "locale/localemanager.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) {
|
void moduleLocale(scriptcontext_t *context) {
|
||||||
assertNotNull(context, "Script context cannot be NULL");
|
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);
|
||||||
}
|
}
|
||||||
@@ -149,6 +149,7 @@ def processLanguageList():
|
|||||||
f.write(headerKeys)
|
f.write(headerKeys)
|
||||||
|
|
||||||
# Generate language list.
|
# Generate language list.
|
||||||
|
langValues = {}
|
||||||
headerLocale = "#pragma once\n#include \"locale/localeinfo.h\"\n\n"
|
headerLocale = "#pragma once\n#include \"locale/localeinfo.h\"\n\n"
|
||||||
headerLocale += "typedef enum {\n"
|
headerLocale += "typedef enum {\n"
|
||||||
count = 0
|
count = 0
|
||||||
@@ -156,6 +157,7 @@ def processLanguageList():
|
|||||||
count += 1
|
count += 1
|
||||||
for lang in LANGUAGE_DATA:
|
for lang in LANGUAGE_DATA:
|
||||||
langKey = lang.replace('-', '_').replace(' ', '_').upper()
|
langKey = lang.replace('-', '_').replace(' ', '_').upper()
|
||||||
|
langValues[lang] = count
|
||||||
headerLocale += f" DUSK_LOCALE_{langKey} = {count},\n"
|
headerLocale += f" DUSK_LOCALE_{langKey} = {count},\n"
|
||||||
count += 1
|
count += 1
|
||||||
headerLocale += f" DUSK_LOCALE_COUNT = {count}\n"
|
headerLocale += f" DUSK_LOCALE_COUNT = {count}\n"
|
||||||
@@ -169,6 +171,13 @@ def processLanguageList():
|
|||||||
headerLocale += f" }},\n"
|
headerLocale += f" }},\n"
|
||||||
headerLocale += "};\n"
|
headerLocale += "};\n"
|
||||||
|
|
||||||
|
headerLocale += f"static const char_t *LOCALE_SCRIPT = \n"
|
||||||
|
for lang in LANGUAGE_DATA:
|
||||||
|
langKey = lang.replace('-', '_').replace(' ', '_').upper()
|
||||||
|
langValue = langValues[lang]
|
||||||
|
headerLocale += f" \"DUSK_LOCALE_{langKey} = {langValue}\\n\"\n"
|
||||||
|
headerLocale += ";\n"
|
||||||
|
|
||||||
# Write out the locale enum header file
|
# Write out the locale enum header file
|
||||||
outputFile = os.path.join(args.headers_dir, "locale", "locale.h")
|
outputFile = os.path.join(args.headers_dir, "locale", "locale.h")
|
||||||
os.makedirs(os.path.dirname(outputFile), exist_ok=True)
|
os.makedirs(os.path.dirname(outputFile), exist_ok=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user