Thread Tools Display Modes
Prev Previous Post   Next Post Next
08/16/15, 10:04 AM   #21
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
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:
  1. local strings = {
  2.     ["de"] = {
  3.         ["SI_MYADDON_NAME"] = "My German Addon name",
  4.     },
  5.     ["en"] = {
  6.         ["SI_MYADDON_NAME"] = "My Addon name",
  7.         ["SI_MYADDON_GENERAL_OPTIONS_HEADER"] = "General Options",
  8.     },
  9. }
  10.  
  11. setmetatable(strings["de"], {__index = strings["en"]})
  12.  
  13. local function AddStringsToGame()
  14.     local lang = GetCVar("language.2")
  15.     if strings[lang] == nil then lang = "en" end
  16.  
  17.     for i,v in pairs(strings[lang]) do
  18.         ZO_CreateStringId(i, v)
  19.     end
  20. end
  21.  
  22. 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:
  1. strings["de"] = {
  2.     ["SI_MYADDON_NAME"] = "My German Addon name",
  3.     ["SI_MYADDON_GENERAL_OPTIONS_HEADER"] = "General Options",
  4. }

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.
  Reply With Quote
 

ESOUI » Developer Discussions » General Authoring Discussion » What is a Scene, the SceneManager and other concepts


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off