Working on translation context

This commit is contained in:
2025-05-25 20:15:34 -05:00
parent 470fd62e2f
commit 44dd3b7aa6
8 changed files with 112 additions and 19 deletions

View File

@@ -119,3 +119,8 @@ msgstr ""
"Thyme and Fire\n" "Thyme and Fire\n"
"\n" "\n"
"Press [input action=debug][/input] to open the debug menu" "Press [input action=debug][/input] to open the debug menu"
# TEST
msgid "test"
msgstr "Test {title} String"

View File

@@ -19,8 +19,19 @@ enum Category {
FOOD FOOD
}; };
static func getCategoryTitleKey(cat:Category) -> String:
return "item.category." + str(cat).to_lower() + ".title"
@export var title:String = "" @export var title:String = ""
@export var description_text:String = "" @export var description_text:String = ""
@export var type:Type = Type.POTION @export var type:Type = Type.POTION
@export var category:Category = Category.INGREDIENT @export var category:Category = Category.INGREDIENT
@export var stackable:bool = true @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

View File

@@ -24,33 +24,24 @@ func _enter_tree() -> void:
if !ITEM_MAP.has(itemType): if !ITEM_MAP.has(itemType):
assert(false, "Missing item type: " + Item.Type.find_key(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: func isStackable(itemType:Item.Type) -> bool:
if not ITEM_MAP.has(itemType): return getItem(itemType).stackable
return false
var item:Item = ITEM_MAP[itemType]
return item.stackable
func getItemName(itemType:Item.Type, count:int = 1) -> String: func getItemName(itemType:Item.Type, count:int = 1) -> String:
if not ITEM_MAP.has(itemType): var item = getItem(itemType)
return ""
var item:Item = ITEM_MAP[itemType]
return tr_n(item.title, item.title + "_plural", count).format({ return tr_n(item.title, item.title + "_plural", count).format({
"count": count "count": count
}) })
func getItemDescription(itemType:Item.Type) -> String: func getItemDescription(itemType:Item.Type) -> String:
if not ITEM_MAP.has(itemType): return getItem(itemType).description_text
return ""
var item:Item = ITEM_MAP[itemType]
return item.description_text
func getItemCategory(itemType:Item.Type) -> Item.Category: func getItemCategory(itemType:Item.Type) -> Item.Category:
if not ITEM_MAP.has(itemType): return getItem(itemType).category
return Item.Category.INGREDIENT
var item:Item = ITEM_MAP[itemType]
return item.category

View File

@@ -39,6 +39,14 @@ func _init() -> void:
var preferred = OS.get_locale() var preferred = OS.get_locale()
self.setLocaleFromLocaleString(preferred) 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: func setLocaleFromLocaleString(localeString:String) -> void:
var parts:PackedStringArray = localeString.split("_") var parts:PackedStringArray = localeString.split("_")

View File

@@ -25,3 +25,10 @@ func _enter_tree() -> void:
func _init() -> void: func _init() -> void:
_updateRecipes() _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

69
scripts/TransContext.gd Normal file
View File

@@ -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:

View File

@@ -0,0 +1 @@
uid://byt6ygesmade5

View File

@@ -0,0 +1 @@
uid://d1iobq2gqn8dt