From 44dd3b7aa6151e64a95d5b8922ddb3e679c1f72c Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 25 May 2025 20:15:34 -0500 Subject: [PATCH] Working on translation context --- locale/en_AU.po | 5 +++ scripts/Item/Item.gd | 11 +++++ scripts/Singleton/ItemSystem.gd | 29 +++++-------- scripts/Singleton/LocaleSystem.gd | 8 ++++ scripts/Singleton/RecipeSystem.gd | 7 ++++ scripts/TransContext.gd | 69 +++++++++++++++++++++++++++++++ scripts/TransContext.gd.uid | 1 + scripts/TranslationContext.gd.uid | 1 + 8 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 scripts/TransContext.gd create mode 100644 scripts/TransContext.gd.uid create mode 100644 scripts/TranslationContext.gd.uid diff --git a/locale/en_AU.po b/locale/en_AU.po index 7425787..fc08e1d 100644 --- a/locale/en_AU.po +++ b/locale/en_AU.po @@ -119,3 +119,8 @@ msgstr "" "Thyme and Fire\n" "\n" "Press [input action=debug][/input] to open the debug menu" + + +# TEST +msgid "test" +msgstr "Test {title} String" \ No newline at end of file diff --git a/scripts/Item/Item.gd b/scripts/Item/Item.gd index 69f9350..85600b7 100644 --- a/scripts/Item/Item.gd +++ b/scripts/Item/Item.gd @@ -19,8 +19,19 @@ enum Category { FOOD }; +static func getCategoryTitleKey(cat:Category) -> String: + return "item.category." + str(cat).to_lower() + ".title" + @export var title:String = "" @export var description_text:String = "" @export var type:Type = Type.POTION @export var category:Category = Category.INGREDIENT @export var stackable:bool = true + +func getTranslationContext() -> TransContext: + var ctx:TransContext = TransContext.new() + ctx.addTransPlural("title", title) + ctx.addTrans("description", description_text) + ctx.addTrans("category", getCategoryTitleKey(category)) + ctx.addBool("stackable", stackable) + return ctx diff --git a/scripts/Singleton/ItemSystem.gd b/scripts/Singleton/ItemSystem.gd index fa65ab0..20998ac 100644 --- a/scripts/Singleton/ItemSystem.gd +++ b/scripts/Singleton/ItemSystem.gd @@ -24,33 +24,24 @@ func _enter_tree() -> void: if !ITEM_MAP.has(itemType): assert(false, "Missing item type: " + Item.Type.find_key(itemType)) +func getItem(type:Item.Type) -> Item: + if !ITEM_MAP.has(type): + assert(false, "Item type not found: " + Item.Type.find_key(type)) + return null + + return ITEM_MAP[type] func isStackable(itemType:Item.Type) -> bool: - if not ITEM_MAP.has(itemType): - return false - - var item:Item = ITEM_MAP[itemType] - return item.stackable + return getItem(itemType).stackable func getItemName(itemType:Item.Type, count:int = 1) -> String: - if not ITEM_MAP.has(itemType): - return "" - - var item:Item = ITEM_MAP[itemType] + var item = getItem(itemType) return tr_n(item.title, item.title + "_plural", count).format({ "count": count }) func getItemDescription(itemType:Item.Type) -> String: - if not ITEM_MAP.has(itemType): - return "" - - var item:Item = ITEM_MAP[itemType] - return item.description_text + return getItem(itemType).description_text func getItemCategory(itemType:Item.Type) -> Item.Category: - if not ITEM_MAP.has(itemType): - return Item.Category.INGREDIENT - - var item:Item = ITEM_MAP[itemType] - return item.category + return getItem(itemType).category diff --git a/scripts/Singleton/LocaleSystem.gd b/scripts/Singleton/LocaleSystem.gd index 3629b57..c61bc08 100644 --- a/scripts/Singleton/LocaleSystem.gd +++ b/scripts/Singleton/LocaleSystem.gd @@ -39,6 +39,14 @@ func _init() -> void: var preferred = OS.get_locale() self.setLocaleFromLocaleString(preferred) +func _enter_tree() -> void: + # Test + print("Hello World!") + + var ctx = TransContext.new() + ctx.addContext("item", ITEM.getItem(Item.Type.POTION).getTranslationContext()) + print(ctx.translate("test")) + func setLocaleFromLocaleString(localeString:String) -> void: var parts:PackedStringArray = localeString.split("_") diff --git a/scripts/Singleton/RecipeSystem.gd b/scripts/Singleton/RecipeSystem.gd index 6e52c86..71368c7 100644 --- a/scripts/Singleton/RecipeSystem.gd +++ b/scripts/Singleton/RecipeSystem.gd @@ -25,3 +25,10 @@ func _enter_tree() -> void: func _init() -> void: _updateRecipes() + +func getRecipe(recipeType:Recipe.Type) -> Recipe: + if RECIPE_MAP.has(recipeType): + return RECIPE_MAP[recipeType] + else: + assert(false, "Recipe type not found: " + Recipe.Type.find_key(recipeType)) + return null diff --git a/scripts/TransContext.gd b/scripts/TransContext.gd new file mode 100644 index 0000000..a652932 --- /dev/null +++ b/scripts/TransContext.gd @@ -0,0 +1,69 @@ +class_name TransContext + +var default:String = "title" +var pluralContext:String = "count" +var trans:Dictionary[String, String] = {} +var transPlural:Dictionary[String, String] = {} +var transBool:Dictionary[String, bool] = {} +var transInteger:Dictionary[String, int] = {} +var transFloat:Dictionary[String, float] = {} +var subContexts:Dictionary[String, TransContext] = {} + +func setDefault(key:String) -> void: + default = key + +func setPluralContext(key:String) -> void: + pluralContext = key + +func addContext(key:String, ctx:TransContext) -> void: + if subContexts.has(key): + assert(false, "Context already exists: " + key) + subContexts[key] = ctx + +func addTrans(key:String, transl:String) -> void: + if trans.has(key): + assert(false, "Trans String already exists: " + key) + trans[key] = transl + +func addTransPlural(key:String, transl:String, suffix:String = "_plural") -> void: + if transPlural.has(key + suffix) || transPlural.has(key): + assert(false, "Trans Plural String already exists: " + key) + trans[key] = transl + transPlural[key] = transl + suffix + +func addBool(key:String, value:bool) -> void: + if transBool.has(key): + assert(false, "Trans Bool String already exists: " + key) + transBool[key] = value + +func addInteger(key:String, value:int) -> void: + if transInteger.has(key): + assert(false, "Trans Integer String already exists: " + key) + transInteger[key] = value + +func addFloat(key:String, value:float) -> void: + if transFloat.has(key): + assert(false, "Trans Float String already exists: " + key) + transFloat[key] = value + +# func build(parentContext:TransContext = null) -> Dictionary[String, String]: +# var dict:Dictionary[String, String] = {} + +# for transKey in trans.keys(): +# dict[transKey] = trans[transKey] + +# for transKey in transPlural.keys(): +# dict[transKey] = transPlural[transKey] + +# for transKey in transBool.keys(): +# dict[transKey] = str(transBool[transKey]) + +# for transKey in transInteger.keys(): +# dict[transKey] = str(transInteger[transKey]) + +# for transKey in transFloat.keys(): +# dict[transKey] = str(transFloat[transKey]) + +# return dict + +func translate(key:String) -> String: \ No newline at end of file diff --git a/scripts/TransContext.gd.uid b/scripts/TransContext.gd.uid new file mode 100644 index 0000000..3fe6dae --- /dev/null +++ b/scripts/TransContext.gd.uid @@ -0,0 +1 @@ +uid://byt6ygesmade5 diff --git a/scripts/TranslationContext.gd.uid b/scripts/TranslationContext.gd.uid new file mode 100644 index 0000000..8ebcfeb --- /dev/null +++ b/scripts/TranslationContext.gd.uid @@ -0,0 +1 @@ +uid://d1iobq2gqn8dt