1 -- These tables will be filled in later by their specific files.
2 QuestHelper_SubstituteFonts
= {}
3 QuestHelper_Translations
= {}
4 QuestHelper_ForcedTranslations
= {}
5 QuestHelper_TranslationFunctions
= {}
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_fb
= QuestHelper_TranslationFunctions
["enUS"] or empty_table
17 trans_func
= QuestHelper_TranslationFunctions
[loc
] or trans_func_fb
21 local function doSub(op
, index
)
22 local i
= tonumber(index
)
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 "") or "[???]"
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
)
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
47 function QHFormatArray(text
, array
)
48 if not trans_table
then
49 QHFormatSetLocale(GetLocale())
52 local old_array
= sub_array
-- Remember old value, so we can restore it incase this was called recursively.
55 local old_next_free
= next_free
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
)]
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
)]
80 -- Uncomment this to have missing translations marked in text.
81 --trans = string.format("|cffff0000[%s|||r%s|cffff0000]|r", text, trans)
85 text
= doTranslation(trans
)
88 next_free
= old_next_free
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
)