Merge branch 'master' of git://cams.pavlovian.net/questhelper
[QuestHelper.git] / lang.lua
blob11609b7ec65012a254a014086a9b46efdde20c02
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 if op == "" and index == "" then return "%" end
26 local i = tonumber(index)
27 if i then
28 -- Pass the selected argument through a function and insert the result.
29 return (trans_func[op] or trans_func_fb[op] or QuestHelper.nop)(sub_array[i] or "") or "[???]"
30 end
31 return op..index
32 end
34 local next_free = 1
36 local doTranslation = nil
38 local function doNest(op, text)
39 next_free = next_free + 1
40 sub_array[next_free] = doTranslation(string.sub(text, 2, -2))
41 return string.format("%%%s%d", op, next_free)
42 end
44 doTranslation = function(text)
45 local old_next_free = next_free
46 text = string.gsub(string.gsub(text, "%%(%a*)(%b())", doNest), "%%(%a*)(%d*)", doSub)
47 next_free = old_next_free
48 return text
49 end
51 function QHFormatArray(text, array)
52 if not trans_table then
53 QHFormatSetLocale(GetLocale())
54 end
56 local old_array = sub_array -- Remember old value, so we can restore it incase this was called recursively.
57 sub_array = array
59 local old_next_free = next_free
60 next_free = #array
62 local trans = trans_table_force[text] or trans_table[text]
64 while type(trans) ~= "string" do
65 -- The translation doesn't need to be a string, it can be a function which returns a string,
66 -- or an array of strings and functions, of which one will be selected randomly.
67 if type(trans) == "function" then
68 trans = trans(text, array)
69 elseif type(trans) == "table" and #trans > 0 then
70 trans = trans[math.random(1, #trans)]
71 else
72 trans = trans_table_fb[text]
74 while type(trans) ~= "string" do
75 if type(trans) == "function" then
76 trans = trans(text, array)
77 elseif type(trans) == "table" and #trans > 0 then
78 trans = trans[math.random(1, #trans)]
79 else
80 trans = "???"
81 end
82 end
84 -- Uncomment this to have missing translations marked in text.
85 --trans = string.format("|cffff0000[%s|||r%s|cffff0000]|r", text, trans)
86 end
87 end
89 text = doTranslation(trans)
91 sub_array = old_array
92 next_free = old_next_free
94 return text
95 end
97 local arguments = {}
99 function QHFormat(text, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
100 -- This isn't a vardiac function, because that would create a table to store the arguments in, and I'm trying to avoid
101 -- creating any garbage here, by reusing the same table for the arguments. Although I'll admit that this isn't nearly
102 -- as effecient. Or pretty. Or stable. Let the foot shooting begin.
104 arguments[1] = a1 arguments[2] = a2 arguments[3] = a3 arguments[4] = a4
105 arguments[5] = a5 arguments[6] = a6 arguments[7] = a7 arguments[8] = a8
106 arguments[9] = a9 arguments[10] = a10 arguments[11] = a11 arguments[12] = a12
108 return QHFormatArray(text, arguments)
111 -- Translates a string, without any substitutions.
112 function QHText(text)
113 return QHFormatArray(text, empty_table)