136 lines
3.9 KiB
GDScript
136 lines
3.9 KiB
GDScript
class_name TransContext
|
|
|
|
var default:String = "title"
|
|
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:
|
|
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 transStrings.has(key):
|
|
assert(false, "Trans String already exists: " + key)
|
|
transStrings[key] = transl
|
|
|
|
func addTransPlural(key:String, transl:String, suffix:String = "_plural") -> void:
|
|
if transStringsPlural.has(key + suffix) || transStringsPlural.has(key):
|
|
assert(false, "Trans Plural String already exists: " + key)
|
|
transStrings[key] = transl
|
|
transStringsPlural[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(
|
|
parent:TransContext = null,
|
|
parentDict:Dictionary[String, String] = {},
|
|
key:String = ""
|
|
) -> Dictionary[String, String]:
|
|
var dict:Dictionary[String, String] = {}
|
|
|
|
# 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 intKey in transInteger.keys():
|
|
var value:int = transInteger[intKey]
|
|
dict[intKey] = str(value)
|
|
|
|
for floatKey in transFloat.keys():
|
|
var value:float = transFloat[floatKey]
|
|
dict[floatKey] = str(value)
|
|
|
|
# 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
|
|
|
|
# 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
|
|
)
|
|
|
|
# Handle non pluralized strings
|
|
for strKey in transStrings.keys():
|
|
# Already handled in pluralized strings?
|
|
if dict.has(strKey):
|
|
continue
|
|
dict[strKey] = tr(transStrings[strKey])
|
|
|
|
# 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))
|