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