Changed all translation to use new TransContext
This commit is contained in:
@@ -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))
|
||||
|
Reference in New Issue
Block a user