Working on translation context
This commit is contained in:
		@@ -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"
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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("_")
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										69
									
								
								scripts/TransContext.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								scripts/TransContext.gd
									
									
									
									
									
										Normal 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:
 | 
			
		||||
							
								
								
									
										1
									
								
								scripts/TransContext.gd.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								scripts/TransContext.gd.uid
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
uid://byt6ygesmade5
 | 
			
		||||
							
								
								
									
										1
									
								
								scripts/TranslationContext.gd.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								scripts/TranslationContext.gd.uid
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
uid://d1iobq2gqn8dt
 | 
			
		||||
		Reference in New Issue
	
	Block a user