Continuing with localization, I like to have all of my strings in the same file so they are all in the same place when I need to edit them. Then I can use metatables to set a fallback option if I haven't gotten a particular string translated yet.
Lua Code:
local strings = {
["de"] = {
["SI_MYADDON_NAME"] = "My German Addon name",
},
["en"] = {
["SI_MYADDON_NAME"] = "My Addon name",
["SI_MYADDON_GENERAL_OPTIONS_HEADER"] = "General Options",
},
}
setmetatable(strings["de"], {__index = strings["en"]})
local function AddStringsToGame()
local lang = GetCVar("language.2")
if strings[lang] == nil then lang = "en" end
for i,v in pairs(strings[lang]) do
ZO_CreateStringId(i, v)
end
end
AddStringsToGame()
After the index metatabe is set, strings["de"] essentially becomes a union of the original strings["de"] and strings["en"] with any matching keys pulled from strings["de"] rather than strings["en"]:
Lua Code:
strings["de"] = {
["SI_MYADDON_NAME"] = "My German Addon name",
["SI_MYADDON_GENERAL_OPTIONS_HEADER"] = "General Options",
}
As for
exactly why this works or what else you can use metatables for, I couldn't tell you. Metatables are basically magic to me right now
I think making the decision to use this over votan's SafeAddString example really just comes down to personal preference. I suppose my method hogs more memory, but that shouldn't be an issue unless there are
a lot of strings. If it did become an issue you could just nil the local strings table after the necessary ones were added to the game.