What about using JSON files?
global TextValues as TextValueType
type TextValueType
Welcome as string
endtype
Function LoadLanguage(LanguageCode as string)
If not GetFileExists("text-" + LanguageCode + ".json")
// Default language.
LanguageCode = "en"
endif
file as integer
file = OpenToRead("text-" + LanguageCode + ".json")
text as string
while not FileEOF(file)
text = text + ReadLine(file)
endwhile
TextValues.fromjson(text)
EndFunction
LoadLanguage(GetDeviceLanguage())
// This method uses a master list of hard-coded text variable names.
//~ Function ParseTextHC(text as string)
//~ if FindString(text, "$")
//~ // Go through the list of valid tags. Maybe this could be improved...
//~ if FindString(text, "$USERNAME") then text = ReplaceString(text, "$USERNAME", "Kevin Cross", 1)
//~ endif
//~ EndFunction text
//~ Message(ParseTextHC(TextValues.Welcome))
// This method does argument-based replacement using json for arguments.
Function ParseText(text as string, argsJson as string)
if Len(argsJson) > 0
args as string[]
args.fromjson(argsJson)
argIndex as integer
for argIndex = 0 to args.length
text = ReplaceString(text, "%" + str(argIndex + 1) + "%", args[argIndex], 1)
next
endif
EndFunction text
Message(ParseText(TextValues.Welcome, '["Kevin Cross"]'))
Then the language files look like:
{
"Welcome": "Welcome, %1%, to my cool app."
}
or with the hard-coded variable names (ParseTextHC):
{
"Welcome": "Welcome, $USERNAME, to my cool app."
}