update changes
[QuestHelper.git] / lang.lua
blobffd459ee42a57cd8dae67d7d81ec829fc9ec7b28
1 QuestHelper_File["lang.lua"] = "Development Version"
2 QuestHelper_Loadtime["lang.lua"] = GetTime()
4 -- These tables will be filled in later by their specific files.
5 QuestHelper_SubstituteFonts = {}
6 QuestHelper_Translations = {}
7 QuestHelper_ForcedTranslations = {}
8 QuestHelper_TranslationFunctions = {}
10 local empty_table = {}
12 local trans_table, trans_table_force, trans_func, trans_func_fb
14 -- Sets the locale used by QuestHelper. It needn't match the game's locale.
15 function QHFormatSetLocale(loc)
16 trans_table_force = QuestHelper_ForcedTranslations[GetLocale()] or empty_table
17 trans_table_fb = QuestHelper_Translations["enUS"] or empty_table
18 trans_table = QuestHelper_Translations[loc] or trans_table_fb
19 trans_func_fb = QuestHelper_TranslationFunctions["enUS"] or empty_table
20 trans_func = QuestHelper_TranslationFunctions[loc] or trans_func_fb
21 end
23 local sub_array = nil
24 local function doSub(op, index)
25 local i = tonumber(index)
26 if i then
27 -- Pass the selected argument through a function and insert the result.
28 return (trans_func[op] or trans_func_fb[op] or QuestHelper.nop)(sub_array[i] or "") or "[???]"
29 end
30 return op..index
31 end
33 local next_free = 1
35 local doTranslation = nil
37 local function doNest(op, text)
38 next_free = next_free + 1
39 sub_array[next_free] = doTranslation(string.sub(text, 2, -2))
40 return string.format("%%%s%d", op, next_free)
41 end
43 doTranslation = function(text)
44 local old_next_free = next_free
45 text = string.gsub(string.gsub(text, "%%(%a*)(%b())", doNest), "%%(%a*)(%d*)", doSub)
46 next_free = old_next_free
47 return text
48 end
50 function QHFormatArray(text, array)
51 if not trans_table then
52 QHFormatSetLocale(GetLocale())
53 end
55 local old_array = sub_array -- Remember old value, so we can restore it incase this was called recursively.
56 sub_array = array
58 local old_next_free = next_free
59 next_free = #array
61 local trans = trans_table_force[text] or trans_table[text]
63 while type(trans) ~= "string" do
64 -- The translation doesn't need to be a string, it can be a function which returns a string,
65 -- or an array of strings and functions, of which one will be selected randomly.
66 if type(trans) == "function" then
67 trans = trans(text, array)
68 elseif type(trans) == "table" and #trans > 0 then
69 trans = trans[math.random(1, #trans)]
70 else
71 trans = trans_table_fb[text]
73 while type(trans) ~= "string" do
74 if type(trans) == "function" then
75 trans = trans(text, array)
76 elseif type(trans) == "table" and #trans > 0 then
77 trans = trans[math.random(1, #trans)]
78 else
79 trans = "???"
80 end
81 end
83 -- Uncomment this to have missing translations marked in text.
84 --trans = string.format("|cffff0000[%s|||r%s|cffff0000]|r", text, trans)
85 end
86 end
88 text = doTranslation(trans)
90 sub_array = old_array
91 next_free = old_next_free
93 return text
94 end
96 local arguments = {}
98 function QHFormat(text, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
99 -- This isn't a vardiac function, because that would create a table to store the arguments in, and I'm trying to avoid
100 -- creating any garbage here, by reusing the same table for the arguments. Although I'll admit that this isn't nearly
101 -- as effecient. Or pretty. Or stable. Let the foot shooting begin.
103 arguments[1] = a1 arguments[2] = a2 arguments[3] = a3 arguments[4] = a4
104 arguments[5] = a5 arguments[6] = a6 arguments[7] = a7 arguments[8] = a8
105 arguments[9] = a9 arguments[10] = a10 arguments[11] = a11 arguments[12] = a12
107 return QHFormatArray(text, arguments)
110 -- Translates a string, without any substitutions.
111 function QHText(text)
112 return QHFormatArray(text, empty_table)