Changed all translation to use new TransContext
This commit is contained in:
		@@ -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"
 | 
			
		||||
msgstr "Test {item.title} String"
 | 
			
		||||
@@ -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"
 | 
			
		||||
 
 | 
			
		||||
@@ -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"]
 | 
			
		||||
 
 | 
			
		||||
@@ -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():
 | 
			
		||||
 
 | 
			
		||||
@@ -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):
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
class_name ItemResource extends Resource
 | 
			
		||||
 | 
			
		||||
@export var itemType:Item.Type = Item.Type.ONION;
 | 
			
		||||
@export var count:int = 1;
 | 
			
		||||
@export var quantity:int = 1;
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -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("_")
 | 
			
		||||
 
 | 
			
		||||
@@ -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:
 | 
			
		||||
	# 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))
 | 
			
		||||
 
 | 
			
		||||
@@ -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:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user