From c1d8dd46d858a0ec29bd113ddc295e73dc74c9c8 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 26 May 2025 08:32:07 -0500 Subject: [PATCH] Changed all translation to use new TransContext --- locale/en_AU.po | 12 ++-- scenes/Singletons/Quest.tscn | 1 - scenes/Singletons/Recipe.tscn | 4 +- scripts/Event/Item/EventGetItem.gd | 10 +-- scripts/Item/Inventory.gd | 3 +- scripts/Item/Item.gd | 2 +- scripts/Item/ItemResource.gd | 2 +- scripts/Quest/Quest.gd | 7 +- scripts/Quest/QuestObjective.gd | 16 +++-- scripts/Scene/MainMenuScene.gd | 5 +- scripts/Singleton/ItemSystem.gd | 14 +--- scripts/Singleton/LocaleSystem.gd | 5 +- scripts/TransContext.gd | 110 +++++++++++++++++++++++------ scripts/UI/QuestMenu.gd | 6 +- 14 files changed, 128 insertions(+), 69 deletions(-) diff --git a/locale/en_AU.po b/locale/en_AU.po index fc08e1d..cb1e0b9 100644 --- a/locale/en_AU.po +++ b/locale/en_AU.po @@ -50,13 +50,13 @@ msgstr "Debug Battle" # msgid "event.get_item.found" msgid_plural "event.get_item.found_plural" -msgstr[0] "Found {item}." -msgstr[1] "Found {quantity} {item}." +msgstr[0] "Found {item.title}." +msgstr[1] "Found {quantity} {item.title}." msgid "event.get_item.given" msgid_plural "event.get_item.given_plural" -msgstr[0] "Received {item}." -msgstr[1] "Received {quantity} {item}." +msgstr[0] "Received {item.title}." +msgstr[1] "Received {quantity} {item.title}." # # QUESTS @@ -68,7 +68,7 @@ msgid "quest.test_quest.onion.title" msgstr "Scavenge Onions" msgid "quest.test_quest.onion.description" -msgstr "Find and collect {quantity} {item} and bring them back to {npc}." +msgstr "Find and collect {quantity} {item.title} and bring them back to {npc}." # # ITEMS @@ -123,4 +123,4 @@ msgstr "" # TEST msgid "test" -msgstr "Test {title} String" \ No newline at end of file +msgstr "Test {item.title} String" \ No newline at end of file diff --git a/scenes/Singletons/Quest.tscn b/scenes/Singletons/Quest.tscn index f7e2cf4..f8a240a 100644 --- a/scenes/Singletons/Quest.tscn +++ b/scenes/Singletons/Quest.tscn @@ -18,7 +18,6 @@ metadata/_custom_type_script = "uid://dn0kxbe85n40f" script = ExtResource("3_l8p7p") title = "quest.test_quest.onion.title" description = "quest.test_quest.onion.description" -objectiveType = null itemType = 2 quantity = 2 metadata/_custom_type_script = "uid://de1ao4huhy0hm" diff --git a/scenes/Singletons/Recipe.tscn b/scenes/Singletons/Recipe.tscn index d33af08..0cf2646 100644 --- a/scenes/Singletons/Recipe.tscn +++ b/scenes/Singletons/Recipe.tscn @@ -7,13 +7,13 @@ [sub_resource type="Resource" id="Resource_3dxl6"] script = ExtResource("3_b8y03") itemType = 3 -count = 1 +quantity = 1 metadata/_custom_type_script = "uid://c26aptwsjs044" [sub_resource type="Resource" id="Resource_b8y03"] script = ExtResource("3_b8y03") itemType = 4 -count = 1 +quantity = 1 metadata/_custom_type_script = "uid://c26aptwsjs044" [node name="RecipeSystem" type="Node"] diff --git a/scripts/Event/Item/EventGetItem.gd b/scripts/Event/Item/EventGetItem.gd index 36d936a..6a27ffd 100644 --- a/scripts/Event/Item/EventGetItem.gd +++ b/scripts/Event/Item/EventGetItem.gd @@ -23,11 +23,11 @@ func start() -> void: textKey = "event.get_item.given" _: pass - - VN.getTextbox().setText(tr_n(textKey, textKey + "_plural", quantity).format({ - "item": ITEM.getItemName(itemType, quantity), - "quantity": quantity - })); + + var ctx = TransContext.new() + ctx.addInteger("quantity", quantity) + ctx.addContext("item", ITEM.getItem(itemType).getTransContext()) + VN.getTextbox().setText(ctx.transPlural(textKey, textKey + "_plural", quantity)); func isDone() -> bool: if !super.isDone(): diff --git a/scripts/Item/Inventory.gd b/scripts/Item/Inventory.gd index 7e13177..5242043 100644 --- a/scripts/Item/Inventory.gd +++ b/scripts/Item/Inventory.gd @@ -7,7 +7,8 @@ enum ItemSortType { class ItemStackNameComparator: static func _sort(a, b): - return ITEM.getItemName(a).to_lower() < ITEM.getItemName(b).to_lower() + assert(false, "Going to change implementation of this later.") + # return ITEM.getItemName(a).to_lower() < ITEM.getItemName(b).to_lower() class ItemStackTypeComparator: static func _sort(a, b): diff --git a/scripts/Item/Item.gd b/scripts/Item/Item.gd index 85600b7..946dba1 100644 --- a/scripts/Item/Item.gd +++ b/scripts/Item/Item.gd @@ -28,7 +28,7 @@ static func getCategoryTitleKey(cat:Category) -> String: @export var category:Category = Category.INGREDIENT @export var stackable:bool = true -func getTranslationContext() -> TransContext: +func getTransContext() -> TransContext: var ctx:TransContext = TransContext.new() ctx.addTransPlural("title", title) ctx.addTrans("description", description_text) diff --git a/scripts/Item/ItemResource.gd b/scripts/Item/ItemResource.gd index e32af0d..729b9cf 100644 --- a/scripts/Item/ItemResource.gd +++ b/scripts/Item/ItemResource.gd @@ -1,4 +1,4 @@ class_name ItemResource extends Resource @export var itemType:Item.Type = Item.Type.ONION; -@export var count:int = 1; \ No newline at end of file +@export var quantity:int = 1; \ No newline at end of file diff --git a/scripts/Quest/Quest.gd b/scripts/Quest/Quest.gd index fc1f63d..75abe5b 100644 --- a/scripts/Quest/Quest.gd +++ b/scripts/Quest/Quest.gd @@ -30,6 +30,7 @@ func isStarted() -> bool: func objectiveUpdated(objective:QuestObjective) -> void: QUEST.questUpdated.emit(self) -func getLocaleData() -> Dictionary[String, String]: - var dict: Dictionary[String, String] = {} - return dict +func getTransContext() -> TransContext: + var ctx:TransContext = TransContext.new() + ctx.addTransPlural("title", title) + return ctx diff --git a/scripts/Quest/QuestObjective.gd b/scripts/Quest/QuestObjective.gd index d708d07..df81035 100644 --- a/scripts/Quest/QuestObjective.gd +++ b/scripts/Quest/QuestObjective.gd @@ -40,8 +40,14 @@ func _onPlayerInventoryUpdated() -> void: func isCompleted() -> bool: return completed -func getLocaleData() -> Dictionary[String, String]: - var dict:Dictionary[String, String] = {} - dict.item = ITEM.getItemName(itemType, quantity) - dict.quantity = LOCALE.formatInteger(quantity) - return dict +func getTransContext() -> TransContext: + var ctx = TransContext.new() + + ctx.addTrans("title", title) + ctx.addTrans("description", description) + + if objectiveType == Type.Item: + ctx.addInteger("quantity", quantity) + ctx.addContext("item", ITEM.getItem(itemType).getTransContext()) + + return ctx diff --git a/scripts/Scene/MainMenuScene.gd b/scripts/Scene/MainMenuScene.gd index 4503f2a..a26fca2 100644 --- a/scripts/Scene/MainMenuScene.gd +++ b/scripts/Scene/MainMenuScene.gd @@ -1,7 +1,4 @@ class_name MainMenuScene extends Node func _enter_tree() -> void: - pass - # $Control/Label.text = tr("main_menu.label").format({ - # inputIcon = "[img=32x32]res://icon.svg[/img]" - # }) + pass \ No newline at end of file diff --git a/scripts/Singleton/ItemSystem.gd b/scripts/Singleton/ItemSystem.gd index 20998ac..51978e3 100644 --- a/scripts/Singleton/ItemSystem.gd +++ b/scripts/Singleton/ItemSystem.gd @@ -32,16 +32,4 @@ func getItem(type:Item.Type) -> Item: return ITEM_MAP[type] func isStackable(itemType:Item.Type) -> bool: - return getItem(itemType).stackable - -func getItemName(itemType:Item.Type, count:int = 1) -> String: - var item = getItem(itemType) - return tr_n(item.title, item.title + "_plural", count).format({ - "count": count - }) - -func getItemDescription(itemType:Item.Type) -> String: - return getItem(itemType).description_text - -func getItemCategory(itemType:Item.Type) -> Item.Category: - return getItem(itemType).category + return getItem(itemType).stackable \ No newline at end of file diff --git a/scripts/Singleton/LocaleSystem.gd b/scripts/Singleton/LocaleSystem.gd index c61bc08..d020981 100644 --- a/scripts/Singleton/LocaleSystem.gd +++ b/scripts/Singleton/LocaleSystem.gd @@ -44,8 +44,9 @@ func _enter_tree() -> void: print("Hello World!") var ctx = TransContext.new() - ctx.addContext("item", ITEM.getItem(Item.Type.POTION).getTranslationContext()) - print(ctx.translate("test")) + ctx.addInteger("quantity", 2) + ctx.addContext("item", ITEM.getItem(Item.Type.POTION).getTransContext()) + print(ctx.trans("test")) func setLocaleFromLocaleString(localeString:String) -> void: var parts:PackedStringArray = localeString.split("_") diff --git a/scripts/TransContext.gd b/scripts/TransContext.gd index a652932..f419c34 100644 --- a/scripts/TransContext.gd +++ b/scripts/TransContext.gd @@ -1,12 +1,12 @@ class_name TransContext var default:String = "title" -var pluralContext:String = "count" -var trans:Dictionary[String, String] = {} -var transPlural:Dictionary[String, String] = {} +var pluralContext:String = "quantity" var transBool:Dictionary[String, bool] = {} var transInteger:Dictionary[String, int] = {} var transFloat:Dictionary[String, float] = {} +var transStrings:Dictionary[String, String] = {} +var transStringsPlural:Dictionary[String, String] = {} var subContexts:Dictionary[String, TransContext] = {} func setDefault(key:String) -> void: @@ -21,15 +21,15 @@ func addContext(key:String, ctx:TransContext) -> void: subContexts[key] = ctx func addTrans(key:String, transl:String) -> void: - if trans.has(key): + if transStrings.has(key): assert(false, "Trans String already exists: " + key) - trans[key] = transl + transStrings[key] = transl func addTransPlural(key:String, transl:String, suffix:String = "_plural") -> void: - if transPlural.has(key + suffix) || transPlural.has(key): + if transStringsPlural.has(key + suffix) || transStringsPlural.has(key): assert(false, "Trans Plural String already exists: " + key) - trans[key] = transl - transPlural[key] = transl + suffix + transStrings[key] = transl + transStringsPlural[key] = transl + suffix func addBool(key:String, value:bool) -> void: if transBool.has(key): @@ -46,24 +46,90 @@ func addFloat(key:String, value:float) -> void: assert(false, "Trans Float String already exists: " + key) transFloat[key] = value -# func build(parentContext:TransContext = null) -> Dictionary[String, String]: -# var dict:Dictionary[String, String] = {} +func build( + parent:TransContext = null, + parentDict:Dictionary[String, String] = {}, + key:String = "" +) -> Dictionary[String, String]: + var dict:Dictionary[String, String] = {} -# for transKey in trans.keys(): -# dict[transKey] = trans[transKey] + # Handle basic types + for boolKey in transBool.keys(): + var value:bool = transBool[boolKey] + dict[boolKey] = str(value).to_lower()# TODO: Change to yes/no? -# for transKey in transPlural.keys(): -# dict[transKey] = transPlural[transKey] + for intKey in transInteger.keys(): + var value:int = transInteger[intKey] + dict[intKey] = str(value) -# for transKey in transBool.keys(): -# dict[transKey] = str(transBool[transKey]) + for floatKey in transFloat.keys(): + var value:float = transFloat[floatKey] + dict[floatKey] = str(value) -# for transKey in transInteger.keys(): -# dict[transKey] = str(transInteger[transKey]) + # Determine the pluralized strings context + var count:int = -1 + if dict.has(pluralContext): + count = int(dict[pluralContext]) + elif parentDict.has(pluralContext): + count = int(parentDict[pluralContext]) + else: + count = 1 # Default to 1 if no count is specified -# for transKey in transFloat.keys(): -# dict[transKey] = str(transFloat[transKey]) + # Handle pluralized strings + for strKey in transStringsPlural.keys(): + assert(transStrings.has(strKey), "Missing singular translation for: " + strKey) + dict[strKey] = tr_n( + transStrings[strKey], + transStringsPlural[strKey], + count + ) -# return dict + # Handle non pluralized strings + for strKey in transStrings.keys(): + # Already handled in pluralized strings? + if dict.has(strKey): + continue + dict[strKey] = tr(transStrings[strKey]) -func translate(key:String) -> String: \ No newline at end of file + # Create a super context that contains all the parent translations and ours + var superDict = dict.duplicate() + superDict.merge(parentDict) + + # Generate sub contexts + for subKey in subContexts.keys(): + var subCtx:TransContext = subContexts[subKey] + var subDict:Dictionary[String, String] = subCtx.build( + self, + superDict, + subKey + "." + ) + # Merge with our dictionary + dict.merge(subDict) + + print("Trans context before key prepend: ", dict) + + if key != "": + # Now prepend the parent key to all keys in the dictionary + var newDict:Dictionary[String, String] = {} + for k in dict.keys(): + var newKey:String = key + k + newDict[newKey] = dict[k] + + # Handle default key, this turns say "item.title" into just "item" + if dict.has(default): + var k2 = key.substr(0, key.length() - 1) # Remove trailing dot + newDict[k2] = dict[default] + + dict = newDict + + return dict + +func _trReplace(val:String) -> String: + var dict = self.build() + return val.format(dict) + +func trans(key:String) -> String: + return _trReplace(tr(key)) + +func transPlural(keySingle:String, keyPlural:String, count:int) -> String: + return _trReplace(tr_n(keySingle, keyPlural, count)) diff --git a/scripts/UI/QuestMenu.gd b/scripts/UI/QuestMenu.gd index 90de31c..1f40d0a 100644 --- a/scripts/UI/QuestMenu.gd +++ b/scripts/UI/QuestMenu.gd @@ -63,9 +63,9 @@ func setQuestObjective(objective = null): questObjectiveList.select(objective) # Setup Description - var data = quest.getLocaleData() - data.merge(questObjective.getLocaleData()) - questObjectiveInfo.text = tr(questObjective.description).format(data) + var ctx = questObjective.getTransContext() + ctx.addContext("quest", quest.getTransContext()) + questObjectiveInfo.text = ctx.trans(questObjective.description) func open(questKey = null) -> void: