From 5e6a95866aac8d3fbcda7f2ee1b56e1259c4591c Mon Sep 17 00:00:00 2001 From: AzaezelX Date: Thu, 22 Jun 2023 17:52:32 -0500 Subject: [PATCH] localization utiity methods to better adress https://github.com/TorqueGameEngines/Torque3D/issues/1036 --- Templates/BaseGame/game/data/UI/UI.tscript | 9 +++++- .../game/data/UI/scripts/utility.tscript | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Templates/BaseGame/game/data/UI/UI.tscript b/Templates/BaseGame/game/data/UI/UI.tscript index 352cd91bd..3baad2bd5 100644 --- a/Templates/BaseGame/game/data/UI/UI.tscript +++ b/Templates/BaseGame/game/data/UI/UI.tscript @@ -14,6 +14,14 @@ function UI::onCreate( %this ) { + exec("./scripts/utility"); + // create and set as core a defaultTable LanguageTable for localization purposes + // example usage for quick language appending + // addLanguage("defaultTable","check","ch"); + // addLanguage("defaultTable","try","tr"); + createLangTable("defaultTable"); + setCoreLangTable("defaultTable"); + exec("./langs/languageMap"); } function UI::onDestroy( %this ) @@ -81,7 +89,6 @@ function UI::initClient(%this) %this.queueExec("./scripts/messageBoxes"); %this.queueExec("./scripts/help"); %this.queueExec("./scripts/cursors"); - %this.queueExec("./scripts/utility"); if(isToolBuild()) %this.queueExec("./tools/creator.tscript"); diff --git a/Templates/BaseGame/game/data/UI/scripts/utility.tscript b/Templates/BaseGame/game/data/UI/scripts/utility.tscript index e73d1c080..4245651f0 100644 --- a/Templates/BaseGame/game/data/UI/scripts/utility.tscript +++ b/Templates/BaseGame/game/data/UI/scripts/utility.tscript @@ -130,3 +130,33 @@ function getButtonBitmap(%device, %button) return %assetId; } + +function createLangTable(%name) +{ + // create a langtable, and global variable to be referenced by GUI elements + // opeates as follows: + // root gui element contains langTableMod = mylang; + // subelements inherit or override + // langTableMod then informs the underlying system to look for a $I18N::mylang + // this holds a reference to a table defined in .lso files, using data/UI/langs/languageMap global defines as a key + new LangTable(%name){}; + eval("$I18N::"@ %name @"="@ %name @".getId();"); +} + +function addLanguage(%langTable, %filename, %alias) +{ + // generate an .lso file and if a languageMap file does not exist, it as well + %needLangMap = true; + if(isFile("data/UI/langs/languageMap")) + %needLangMap = false; + + CompileLanguage("data/UI/langs/"@ %filename @".txt", %needLangMap); + %langTable.addLanguage("data/UI/langs/"@ %filename @".lso", %alias); +} + +function switchLanguage(%language) //use here the #n as it's the order of inclusion +{ + // swap existing language from the current core langtable and refresh the gui contents + getCoreLangTable().setCurrentLanguage(%language); + Canvas.setContent(Canvas.getContent()); +} \ No newline at end of file