flickerstreak@1
|
1 --[[
|
flickerstreak@1
|
2 Name: AceLocale-2.2
|
flickerstreak@1
|
3 Revision: $Rev: 18708 $
|
flickerstreak@1
|
4 Developed by: The Ace Development Team (http://www.wowace.com/index.php/The_Ace_Development_Team)
|
flickerstreak@1
|
5 Inspired By: Ace 1.x by Turan (turan@gryphon.com)
|
flickerstreak@1
|
6 Website: http://www.wowace.com/
|
flickerstreak@1
|
7 Documentation: http://www.wowace.com/index.php/AceLocale-2.2
|
flickerstreak@1
|
8 SVN: http://svn.wowace.com/root/trunk/Ace2/AceLocale-2.2
|
flickerstreak@1
|
9 Description: Localization library for addons to use to handle proper
|
flickerstreak@1
|
10 localization and internationalization.
|
flickerstreak@1
|
11 Dependencies: AceLibrary
|
flickerstreak@1
|
12 ]]
|
flickerstreak@1
|
13
|
flickerstreak@1
|
14 local MAJOR_VERSION = "AceLocale-2.2"
|
flickerstreak@1
|
15 local MINOR_VERSION = "$Revision: 18708 $"
|
flickerstreak@1
|
16
|
flickerstreak@1
|
17 if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary.") end
|
flickerstreak@1
|
18 if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
|
flickerstreak@1
|
19
|
flickerstreak@1
|
20 local AceLocale = {}
|
flickerstreak@1
|
21
|
flickerstreak@1
|
22 local DEFAULT_LOCALE = "enUS"
|
flickerstreak@1
|
23 local _G = getfenv(0)
|
flickerstreak@1
|
24
|
flickerstreak@1
|
25 local BASE_TRANSLATIONS, DEBUGGING, TRANSLATIONS, BASE_LOCALE, TRANSLATION_TABLES, REVERSE_TRANSLATIONS, STRICTNESS, DYNAMIC_LOCALES, CURRENT_LOCALE, NAME
|
flickerstreak@1
|
26
|
flickerstreak@1
|
27 local rawget = rawget
|
flickerstreak@1
|
28 local rawset = rawset
|
flickerstreak@1
|
29 local type = type
|
flickerstreak@1
|
30
|
flickerstreak@1
|
31 local newRegistries = {}
|
flickerstreak@1
|
32 local scheduleClear
|
flickerstreak@1
|
33
|
flickerstreak@1
|
34 local lastSelf
|
flickerstreak@1
|
35 local __index = function(self, key)
|
flickerstreak@1
|
36 lastSelf = self
|
flickerstreak@1
|
37 local value = (rawget(self, TRANSLATIONS) or AceLocale.prototype)[key]
|
flickerstreak@1
|
38 rawset(self, key, value)
|
flickerstreak@1
|
39 return value
|
flickerstreak@1
|
40 end
|
flickerstreak@1
|
41
|
flickerstreak@1
|
42 local __newindex = function(self, k, v)
|
flickerstreak@1
|
43 if type(v) ~= "function" and type(k) ~= "table" then
|
flickerstreak@1
|
44 AceLocale.error(self, "Cannot change the values of an AceLocale instance.")
|
flickerstreak@1
|
45 end
|
flickerstreak@1
|
46 rawset(self, k, v)
|
flickerstreak@1
|
47 end
|
flickerstreak@1
|
48
|
flickerstreak@1
|
49 local __tostring = function(self)
|
flickerstreak@1
|
50 if type(rawget(self, 'GetLibraryVersion')) == "function" then
|
flickerstreak@1
|
51 return self:GetLibraryVersion()
|
flickerstreak@1
|
52 else
|
flickerstreak@1
|
53 return "AceLocale(" .. self[NAME] .. ")"
|
flickerstreak@1
|
54 end
|
flickerstreak@1
|
55 end
|
flickerstreak@1
|
56
|
flickerstreak@1
|
57 local function clearCache(self)
|
flickerstreak@1
|
58 if not rawget(self, BASE_TRANSLATIONS) then
|
flickerstreak@1
|
59 return
|
flickerstreak@1
|
60 end
|
flickerstreak@1
|
61
|
flickerstreak@1
|
62 local cache = self[BASE_TRANSLATIONS]
|
flickerstreak@1
|
63 rawset(self, REVERSE_TRANSLATIONS, nil)
|
flickerstreak@1
|
64
|
flickerstreak@1
|
65 for k in pairs(self) do
|
flickerstreak@1
|
66 if rawget(cache, k) ~= nil then
|
flickerstreak@1
|
67 self[k] = nil
|
flickerstreak@1
|
68 end
|
flickerstreak@1
|
69 end
|
flickerstreak@1
|
70 rawset(self, 'tmp', true)
|
flickerstreak@1
|
71 self.tmp = nil
|
flickerstreak@1
|
72 end
|
flickerstreak@1
|
73
|
flickerstreak@1
|
74 local function refixInstance(instance)
|
flickerstreak@1
|
75 if getmetatable(instance) then
|
flickerstreak@1
|
76 setmetatable(instance, nil)
|
flickerstreak@1
|
77 end
|
flickerstreak@1
|
78 local translations = instance[TRANSLATIONS]
|
flickerstreak@1
|
79 if translations then
|
flickerstreak@1
|
80 if getmetatable(translations) then
|
flickerstreak@1
|
81 setmetatable(translations, nil)
|
flickerstreak@1
|
82 end
|
flickerstreak@1
|
83 local baseTranslations = instance[BASE_TRANSLATIONS]
|
flickerstreak@1
|
84 if getmetatable(baseTranslations) then
|
flickerstreak@1
|
85 setmetatable(baseTranslations, nil)
|
flickerstreak@1
|
86 end
|
flickerstreak@1
|
87 if translations == baseTranslations or instance[STRICTNESS] then
|
flickerstreak@1
|
88 setmetatable(instance, {
|
flickerstreak@1
|
89 __index = __index,
|
flickerstreak@1
|
90 __newindex = __newindex,
|
flickerstreak@1
|
91 __tostring = __tostring
|
flickerstreak@1
|
92 })
|
flickerstreak@1
|
93
|
flickerstreak@1
|
94 setmetatable(translations, {
|
flickerstreak@1
|
95 __index = AceLocale.prototype
|
flickerstreak@1
|
96 })
|
flickerstreak@1
|
97 else
|
flickerstreak@1
|
98 setmetatable(instance, {
|
flickerstreak@1
|
99 __index = __index,
|
flickerstreak@1
|
100 __newindex = __newindex,
|
flickerstreak@1
|
101 __tostring = __tostring
|
flickerstreak@1
|
102 })
|
flickerstreak@1
|
103
|
flickerstreak@1
|
104 setmetatable(translations, {
|
flickerstreak@1
|
105 __index = baseTranslations,
|
flickerstreak@1
|
106 })
|
flickerstreak@1
|
107
|
flickerstreak@1
|
108 setmetatable(baseTranslations, {
|
flickerstreak@1
|
109 __index = AceLocale.prototype,
|
flickerstreak@1
|
110 })
|
flickerstreak@1
|
111 end
|
flickerstreak@1
|
112 else
|
flickerstreak@1
|
113 setmetatable(instance, {
|
flickerstreak@1
|
114 __index = __index,
|
flickerstreak@1
|
115 __newindex = __newindex,
|
flickerstreak@1
|
116 __tostring = __tostring,
|
flickerstreak@1
|
117 })
|
flickerstreak@1
|
118 end
|
flickerstreak@1
|
119 clearCache(instance)
|
flickerstreak@1
|
120 newRegistries[instance] = true
|
flickerstreak@1
|
121 scheduleClear()
|
flickerstreak@1
|
122 return instance
|
flickerstreak@1
|
123 end
|
flickerstreak@1
|
124
|
flickerstreak@1
|
125 function AceLocale:new(name)
|
flickerstreak@1
|
126 self:argCheck(name, 2, "string")
|
flickerstreak@1
|
127
|
flickerstreak@1
|
128 if self.registry[name] and type(rawget(self.registry[name], 'GetLibraryVersion')) ~= "function" then
|
flickerstreak@1
|
129 return self.registry[name]
|
flickerstreak@1
|
130 end
|
flickerstreak@1
|
131
|
flickerstreak@1
|
132 AceLocale.registry[name] = refixInstance({
|
flickerstreak@1
|
133 [STRICTNESS] = false,
|
flickerstreak@1
|
134 [NAME] = name,
|
flickerstreak@1
|
135 })
|
flickerstreak@1
|
136 newRegistries[AceLocale.registry[name]] = true
|
flickerstreak@1
|
137 return AceLocale.registry[name]
|
flickerstreak@1
|
138 end
|
flickerstreak@1
|
139
|
flickerstreak@1
|
140 AceLocale.prototype = { class = AceLocale }
|
flickerstreak@1
|
141
|
flickerstreak@1
|
142 function AceLocale.prototype:EnableDebugging()
|
flickerstreak@1
|
143 if rawget(self, BASE_TRANSLATIONS) then
|
flickerstreak@1
|
144 AceLocale.error(self, "Cannot enable debugging after a translation has been registered.")
|
flickerstreak@1
|
145 end
|
flickerstreak@1
|
146 rawset(self, DEBUGGING, true)
|
flickerstreak@1
|
147 end
|
flickerstreak@1
|
148
|
flickerstreak@1
|
149 function AceLocale.prototype:EnableDynamicLocales(override)
|
flickerstreak@1
|
150 AceLocale.argCheck(self, override, 2, "boolean", "nil")
|
flickerstreak@1
|
151 if not override and rawget(self, BASE_TRANSLATIONS) then
|
flickerstreak@1
|
152 AceLocale.error(self, "Cannot enable dynamic locales after a translation has been registered.")
|
flickerstreak@1
|
153 end
|
flickerstreak@1
|
154 if not rawget(self, DYNAMIC_LOCALES) then
|
flickerstreak@1
|
155 rawset(self, DYNAMIC_LOCALES, true)
|
flickerstreak@1
|
156 if rawget(self, BASE_LOCALE) then
|
flickerstreak@1
|
157 if not rawget(self, TRANSLATION_TABLES) then
|
flickerstreak@1
|
158 rawset(self, TRANSLATION_TABLES, {})
|
flickerstreak@1
|
159 end
|
flickerstreak@1
|
160 self[TRANSLATION_TABLES][self[BASE_LOCALE]] = self[BASE_TRANSLATIONS]
|
flickerstreak@1
|
161 self[TRANSLATION_TABLES][self[CURRENT_LOCALE]] = self[TRANSLATIONS]
|
flickerstreak@1
|
162 end
|
flickerstreak@1
|
163 end
|
flickerstreak@1
|
164 end
|
flickerstreak@1
|
165
|
flickerstreak@1
|
166 function AceLocale.prototype:RegisterTranslations(locale, func)
|
flickerstreak@1
|
167 AceLocale.argCheck(self, locale, 2, "string")
|
flickerstreak@1
|
168 AceLocale.argCheck(self, func, 3, "function")
|
flickerstreak@1
|
169
|
flickerstreak@1
|
170 if locale == rawget(self, BASE_LOCALE) then
|
flickerstreak@1
|
171 AceLocale.error(self, "Cannot provide the same locale more than once. %q provided twice.", locale)
|
flickerstreak@1
|
172 end
|
flickerstreak@1
|
173
|
flickerstreak@1
|
174 if rawget(self, BASE_TRANSLATIONS) and GetLocale() ~= locale then
|
flickerstreak@1
|
175 if rawget(self, DEBUGGING) or rawget(self, DYNAMIC_LOCALES) then
|
flickerstreak@1
|
176 if not rawget(self, TRANSLATION_TABLES) then
|
flickerstreak@1
|
177 rawset(self, TRANSLATION_TABLES, {})
|
flickerstreak@1
|
178 end
|
flickerstreak@1
|
179 if self[TRANSLATION_TABLES][locale] then
|
flickerstreak@1
|
180 AceLocale.error(self, "Cannot provide the same locale more than once. %q provided twice.", locale)
|
flickerstreak@1
|
181 end
|
flickerstreak@1
|
182 local t = func()
|
flickerstreak@1
|
183 func = nil
|
flickerstreak@1
|
184 if type(t) ~= "table" then
|
flickerstreak@1
|
185 AceLocale.error(self, "Bad argument #3 to `RegisterTranslations'. function did not return a table. (expected table, got %s)", type(t))
|
flickerstreak@1
|
186 end
|
flickerstreak@1
|
187 self[TRANSLATION_TABLES][locale] = t
|
flickerstreak@1
|
188 t = nil
|
flickerstreak@1
|
189 end
|
flickerstreak@1
|
190 func = nil
|
flickerstreak@1
|
191 return
|
flickerstreak@1
|
192 end
|
flickerstreak@1
|
193 local t = func()
|
flickerstreak@1
|
194 func = nil
|
flickerstreak@1
|
195 if type(t) ~= "table" then
|
flickerstreak@1
|
196 AceLocale.error(self, "Bad argument #3 to `RegisterTranslations'. function did not return a table. (expected table, got %s)", type(t))
|
flickerstreak@1
|
197 end
|
flickerstreak@1
|
198
|
flickerstreak@1
|
199 rawset(self, TRANSLATIONS, t)
|
flickerstreak@1
|
200 if not rawget(self, BASE_TRANSLATIONS) then
|
flickerstreak@1
|
201 rawset(self, BASE_TRANSLATIONS, t)
|
flickerstreak@1
|
202 rawset(self, BASE_LOCALE, locale)
|
flickerstreak@1
|
203 for key,value in pairs(t) do
|
flickerstreak@1
|
204 if value == true then
|
flickerstreak@1
|
205 t[key] = key
|
flickerstreak@1
|
206 end
|
flickerstreak@1
|
207 end
|
flickerstreak@1
|
208 else
|
flickerstreak@1
|
209 for key, value in pairs(self[TRANSLATIONS]) do
|
flickerstreak@1
|
210 if not rawget(self[BASE_TRANSLATIONS], key) then
|
flickerstreak@1
|
211 AceLocale.error(self, "Improper translation exists. %q is likely misspelled for locale %s.", key, locale)
|
flickerstreak@1
|
212 end
|
flickerstreak@1
|
213 if value == true then
|
flickerstreak@1
|
214 AceLocale.error(self, "Can only accept true as a value on the base locale. %q is the base locale, %q is not.", rawget(self, BASE_LOCALE), locale)
|
flickerstreak@1
|
215 end
|
flickerstreak@1
|
216 end
|
flickerstreak@1
|
217 end
|
flickerstreak@1
|
218 rawset(self, CURRENT_LOCALE, locale)
|
flickerstreak@1
|
219 refixInstance(self)
|
flickerstreak@1
|
220 if rawget(self, DEBUGGING) or rawget(self, DYNAMIC_LOCALES) then
|
flickerstreak@1
|
221 if not rawget(self, TRANSLATION_TABLES) then
|
flickerstreak@1
|
222 rawset(self, TRANSLATION_TABLES, {})
|
flickerstreak@1
|
223 end
|
flickerstreak@1
|
224 self[TRANSLATION_TABLES][locale] = t
|
flickerstreak@1
|
225 end
|
flickerstreak@1
|
226 t = nil
|
flickerstreak@1
|
227 end
|
flickerstreak@1
|
228
|
flickerstreak@1
|
229 function AceLocale.prototype:SetLocale(locale)
|
flickerstreak@1
|
230 AceLocale.argCheck(self, locale, 2, "string", "boolean")
|
flickerstreak@1
|
231 if not rawget(self, DYNAMIC_LOCALES) then
|
flickerstreak@1
|
232 AceLocale.error(self, "Cannot call `SetLocale' without first calling `EnableDynamicLocales'.")
|
flickerstreak@1
|
233 end
|
flickerstreak@1
|
234 if not rawget(self, TRANSLATION_TABLES) then
|
flickerstreak@1
|
235 AceLocale.error(self, "Cannot call `SetLocale' without first calling `RegisterTranslations'.")
|
flickerstreak@1
|
236 end
|
flickerstreak@1
|
237 if locale == true then
|
flickerstreak@1
|
238 locale = GetLocale()
|
flickerstreak@1
|
239 if not self[TRANSLATION_TABLES][locale] then
|
flickerstreak@1
|
240 locale = self[BASE_LOCALE]
|
flickerstreak@1
|
241 end
|
flickerstreak@1
|
242 end
|
flickerstreak@1
|
243
|
flickerstreak@1
|
244 if self[CURRENT_LOCALE] == locale then
|
flickerstreak@1
|
245 return
|
flickerstreak@1
|
246 end
|
flickerstreak@1
|
247
|
flickerstreak@1
|
248 if not self[TRANSLATION_TABLES][locale] then
|
flickerstreak@1
|
249 AceLocale.error(self, "Locale %q not registered.", locale)
|
flickerstreak@1
|
250 end
|
flickerstreak@1
|
251
|
flickerstreak@1
|
252 self[TRANSLATIONS] = self[TRANSLATION_TABLES][locale]
|
flickerstreak@1
|
253 self[CURRENT_LOCALE] = locale
|
flickerstreak@1
|
254 refixInstance(self)
|
flickerstreak@1
|
255 end
|
flickerstreak@1
|
256
|
flickerstreak@1
|
257 function AceLocale.prototype:GetLocale()
|
flickerstreak@1
|
258 if not rawget(self, TRANSLATION_TABLES) then
|
flickerstreak@1
|
259 AceLocale.error(self, "Cannot call `GetLocale' without first calling `RegisterTranslations'.")
|
flickerstreak@1
|
260 end
|
flickerstreak@1
|
261 return self[CURRENT_LOCALE]
|
flickerstreak@1
|
262 end
|
flickerstreak@1
|
263
|
flickerstreak@1
|
264 local function iter(t, position)
|
flickerstreak@1
|
265 return (next(t, position))
|
flickerstreak@1
|
266 end
|
flickerstreak@1
|
267
|
flickerstreak@1
|
268 function AceLocale.prototype:IterateAvailableLocales()
|
flickerstreak@1
|
269 if not rawget(self, DYNAMIC_LOCALES) then
|
flickerstreak@1
|
270 AceLocale.error(self, "Cannot call `IterateAvailableLocales' without first calling `EnableDynamicLocales'.")
|
flickerstreak@1
|
271 end
|
flickerstreak@1
|
272 if not rawget(self, TRANSLATION_TABLES) then
|
flickerstreak@1
|
273 AceLocale.error(self, "Cannot call `IterateAvailableLocales' without first calling `RegisterTranslations'.")
|
flickerstreak@1
|
274 end
|
flickerstreak@1
|
275 return iter, self[TRANSLATION_TABLES], nil
|
flickerstreak@1
|
276 end
|
flickerstreak@1
|
277
|
flickerstreak@1
|
278 function AceLocale.prototype:HasLocale(locale)
|
flickerstreak@1
|
279 if not rawget(self, DYNAMIC_LOCALES) then
|
flickerstreak@1
|
280 AceLocale.error(self, "Cannot call `HasLocale' without first calling `EnableDynamicLocales'.")
|
flickerstreak@1
|
281 end
|
flickerstreak@1
|
282 AceLocale.argCheck(self, locale, 2, "string")
|
flickerstreak@1
|
283 return rawget(self, TRANSLATION_TABLES) and self[TRANSLATION_TABLES][locale] ~= nil
|
flickerstreak@1
|
284 end
|
flickerstreak@1
|
285
|
flickerstreak@1
|
286 function AceLocale.prototype:SetStrictness(strict)
|
flickerstreak@1
|
287 AceLocale.argCheck(self, strict, 2, "boolean")
|
flickerstreak@1
|
288 local mt = getmetatable(self)
|
flickerstreak@1
|
289 if not mt then
|
flickerstreak@1
|
290 AceLocale.error(self, "Cannot call `SetStrictness' without a metatable.")
|
flickerstreak@1
|
291 end
|
flickerstreak@1
|
292 if not rawget(self, TRANSLATIONS) then
|
flickerstreak@1
|
293 AceLocale.error(self, "No translations registered.")
|
flickerstreak@1
|
294 end
|
flickerstreak@1
|
295 rawset(self, STRICTNESS, strict)
|
flickerstreak@1
|
296 refixInstance(self)
|
flickerstreak@1
|
297 end
|
flickerstreak@1
|
298
|
flickerstreak@1
|
299 local function initReverse(self)
|
flickerstreak@1
|
300 rawset(self, REVERSE_TRANSLATIONS, {})
|
flickerstreak@1
|
301 local alpha = self[TRANSLATIONS]
|
flickerstreak@1
|
302 local bravo = self[REVERSE_TRANSLATIONS]
|
flickerstreak@1
|
303 for base, localized in pairs(alpha) do
|
flickerstreak@1
|
304 bravo[localized] = base
|
flickerstreak@1
|
305 end
|
flickerstreak@1
|
306 end
|
flickerstreak@1
|
307
|
flickerstreak@1
|
308 function AceLocale.prototype:GetTranslation(text)
|
flickerstreak@1
|
309 AceLocale.argCheck(self, text, 1, "string", "number")
|
flickerstreak@1
|
310 if not rawget(self, TRANSLATIONS) then
|
flickerstreak@1
|
311 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
312 end
|
flickerstreak@1
|
313 return self[text]
|
flickerstreak@1
|
314 end
|
flickerstreak@1
|
315
|
flickerstreak@1
|
316 function AceLocale.prototype:GetStrictTranslation(text)
|
flickerstreak@1
|
317 AceLocale.argCheck(self, text, 1, "string", "number")
|
flickerstreak@1
|
318 local x = rawget(self, TRANSLATIONS)
|
flickerstreak@1
|
319 if not x then
|
flickerstreak@1
|
320 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
321 end
|
flickerstreak@1
|
322 local value = rawget(x, text)
|
flickerstreak@1
|
323 if value == nil then
|
flickerstreak@1
|
324 AceLocale.error(self, "Translation %q does not exist for locale %s", text, self[CURRENT_LOCALE])
|
flickerstreak@1
|
325 end
|
flickerstreak@1
|
326 return value
|
flickerstreak@1
|
327 end
|
flickerstreak@1
|
328
|
flickerstreak@1
|
329 function AceLocale.prototype:GetReverseTranslation(text)
|
flickerstreak@1
|
330 local x = rawget(self, REVERSE_TRANSLATIONS)
|
flickerstreak@1
|
331 if not x then
|
flickerstreak@1
|
332 if not rawget(self, TRANSLATIONS) then
|
flickerstreak@1
|
333 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
334 end
|
flickerstreak@1
|
335 initReverse(self)
|
flickerstreak@1
|
336 x = self[REVERSE_TRANSLATIONS]
|
flickerstreak@1
|
337 end
|
flickerstreak@1
|
338 local translation = x[text]
|
flickerstreak@1
|
339 if not translation then
|
flickerstreak@1
|
340 AceLocale.error(self, "Reverse translation for %q does not exist", text)
|
flickerstreak@1
|
341 end
|
flickerstreak@1
|
342 return translation
|
flickerstreak@1
|
343 end
|
flickerstreak@1
|
344
|
flickerstreak@1
|
345 function AceLocale.prototype:GetIterator()
|
flickerstreak@1
|
346 local x = rawget(self, TRANSLATIONS)
|
flickerstreak@1
|
347 if not x then
|
flickerstreak@1
|
348 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
349 end
|
flickerstreak@1
|
350 return next, x, nil
|
flickerstreak@1
|
351 end
|
flickerstreak@1
|
352
|
flickerstreak@1
|
353 function AceLocale.prototype:GetReverseIterator()
|
flickerstreak@1
|
354 local x = rawget(self, REVERSE_TRANSLATIONS)
|
flickerstreak@1
|
355 if not x then
|
flickerstreak@1
|
356 if not rawget(self, TRANSLATIONS) then
|
flickerstreak@1
|
357 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
358 end
|
flickerstreak@1
|
359 initReverse(self)
|
flickerstreak@1
|
360 x = self[REVERSE_TRANSLATIONS]
|
flickerstreak@1
|
361 end
|
flickerstreak@1
|
362 return next, x, nil
|
flickerstreak@1
|
363 end
|
flickerstreak@1
|
364
|
flickerstreak@1
|
365 function AceLocale.prototype:HasTranslation(text)
|
flickerstreak@1
|
366 AceLocale.argCheck(self, text, 1, "string", "number")
|
flickerstreak@1
|
367 local x = rawget(self, TRANSLATIONS)
|
flickerstreak@1
|
368 if not x then
|
flickerstreak@1
|
369 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
370 end
|
flickerstreak@1
|
371 return rawget(x, text) and true
|
flickerstreak@1
|
372 end
|
flickerstreak@1
|
373
|
flickerstreak@1
|
374 function AceLocale.prototype:HasReverseTranslation(text)
|
flickerstreak@1
|
375 local x = rawget(self, REVERSE_TRANSLATIONS)
|
flickerstreak@1
|
376 if not x then
|
flickerstreak@1
|
377 if not rawget(self, TRANSLATIONS) then
|
flickerstreak@1
|
378 AceLocale.error(self, "No translations registered")
|
flickerstreak@1
|
379 end
|
flickerstreak@1
|
380 initReverse(self)
|
flickerstreak@1
|
381 x = self[REVERSE_TRANSLATIONS]
|
flickerstreak@1
|
382 end
|
flickerstreak@1
|
383 return x[text] and true
|
flickerstreak@1
|
384 end
|
flickerstreak@1
|
385
|
flickerstreak@1
|
386 function AceLocale.prototype:Debug()
|
flickerstreak@1
|
387 if not rawget(self, DEBUGGING) then
|
flickerstreak@1
|
388 return
|
flickerstreak@1
|
389 end
|
flickerstreak@1
|
390 local words = {}
|
flickerstreak@1
|
391 local locales = {"enUS", "deDE", "frFR", "koKR", "zhCN", "zhTW", "esES"}
|
flickerstreak@1
|
392 local localizations = {}
|
flickerstreak@1
|
393 DEFAULT_CHAT_FRAME:AddMessage("--- AceLocale Debug ---")
|
flickerstreak@1
|
394 for _,locale in ipairs(locales) do
|
flickerstreak@1
|
395 if not self[TRANSLATION_TABLES][locale] then
|
flickerstreak@1
|
396 DEFAULT_CHAT_FRAME:AddMessage(string.format("Locale %q not found", locale))
|
flickerstreak@1
|
397 else
|
flickerstreak@1
|
398 localizations[locale] = self[TRANSLATION_TABLES][locale]
|
flickerstreak@1
|
399 end
|
flickerstreak@1
|
400 end
|
flickerstreak@1
|
401 local localeDebug = {}
|
flickerstreak@1
|
402 for locale, localization in pairs(localizations) do
|
flickerstreak@1
|
403 localeDebug[locale] = {}
|
flickerstreak@1
|
404 for word in pairs(localization) do
|
flickerstreak@1
|
405 if type(localization[word]) == "table" then
|
flickerstreak@1
|
406 if type(words[word]) ~= "table" then
|
flickerstreak@1
|
407 words[word] = {}
|
flickerstreak@1
|
408 end
|
flickerstreak@1
|
409 for bit in pairs(localization[word]) do
|
flickerstreak@1
|
410 if type(localization[word][bit]) == "string" then
|
flickerstreak@1
|
411 words[word][bit] = true
|
flickerstreak@1
|
412 end
|
flickerstreak@1
|
413 end
|
flickerstreak@1
|
414 elseif type(localization[word]) == "string" then
|
flickerstreak@1
|
415 words[word] = true
|
flickerstreak@1
|
416 end
|
flickerstreak@1
|
417 end
|
flickerstreak@1
|
418 end
|
flickerstreak@1
|
419 for word in pairs(words) do
|
flickerstreak@1
|
420 if type(words[word]) == "table" then
|
flickerstreak@1
|
421 for bit in pairs(words[word]) do
|
flickerstreak@1
|
422 for locale, localization in pairs(localizations) do
|
flickerstreak@1
|
423 if not rawget(localization, word) or not localization[word][bit] then
|
flickerstreak@1
|
424 localeDebug[locale][word .. "::" .. bit] = true
|
flickerstreak@1
|
425 end
|
flickerstreak@1
|
426 end
|
flickerstreak@1
|
427 end
|
flickerstreak@1
|
428 else
|
flickerstreak@1
|
429 for locale, localization in pairs(localizations) do
|
flickerstreak@1
|
430 if not rawget(localization, word) then
|
flickerstreak@1
|
431 localeDebug[locale][word] = true
|
flickerstreak@1
|
432 end
|
flickerstreak@1
|
433 end
|
flickerstreak@1
|
434 end
|
flickerstreak@1
|
435 end
|
flickerstreak@1
|
436 for locale, t in pairs(localeDebug) do
|
flickerstreak@1
|
437 if not next(t) then
|
flickerstreak@1
|
438 DEFAULT_CHAT_FRAME:AddMessage(string.format("Locale %q complete", locale))
|
flickerstreak@1
|
439 else
|
flickerstreak@1
|
440 DEFAULT_CHAT_FRAME:AddMessage(string.format("Locale %q missing:", locale))
|
flickerstreak@1
|
441 for word in pairs(t) do
|
flickerstreak@1
|
442 DEFAULT_CHAT_FRAME:AddMessage(string.format(" %q", word))
|
flickerstreak@1
|
443 end
|
flickerstreak@1
|
444 end
|
flickerstreak@1
|
445 end
|
flickerstreak@1
|
446 DEFAULT_CHAT_FRAME:AddMessage("--- End AceLocale Debug ---")
|
flickerstreak@1
|
447 end
|
flickerstreak@1
|
448
|
flickerstreak@1
|
449 setmetatable(AceLocale.prototype, {
|
flickerstreak@1
|
450 __index = function(self, k)
|
flickerstreak@1
|
451 if type(k) ~= "table" and k ~= 0 and k ~= "GetLibraryVersion" and k ~= "error" and k ~= "assert" and k ~= "argCheck" and k ~= "pcall" then -- HACK: remove "GetLibraryVersion" and such later.
|
flickerstreak@1
|
452 AceLocale.error(lastSelf or self, "Translation %q does not exist.", k)
|
flickerstreak@1
|
453 end
|
flickerstreak@1
|
454 return nil
|
flickerstreak@1
|
455 end
|
flickerstreak@1
|
456 })
|
flickerstreak@1
|
457
|
flickerstreak@1
|
458 local function activate(self, oldLib, oldDeactivate)
|
flickerstreak@1
|
459 AceLocale = self
|
flickerstreak@1
|
460
|
flickerstreak@1
|
461 self.frame = oldLib and oldLib.frame or CreateFrame("Frame")
|
flickerstreak@1
|
462 self.registry = oldLib and oldLib.registry or {}
|
flickerstreak@1
|
463 self.BASE_TRANSLATIONS = oldLib and oldLib.BASE_TRANSLATIONS or {}
|
flickerstreak@1
|
464 self.DEBUGGING = oldLib and oldLib.DEBUGGING or {}
|
flickerstreak@1
|
465 self.TRANSLATIONS = oldLib and oldLib.TRANSLATIONS or {}
|
flickerstreak@1
|
466 self.BASE_LOCALE = oldLib and oldLib.BASE_LOCALE or {}
|
flickerstreak@1
|
467 self.TRANSLATION_TABLES = oldLib and oldLib.TRANSLATION_TABLES or {}
|
flickerstreak@1
|
468 self.REVERSE_TRANSLATIONS = oldLib and oldLib.REVERSE_TRANSLATIONS or {}
|
flickerstreak@1
|
469 self.STRICTNESS = oldLib and oldLib.STRICTNESS or {}
|
flickerstreak@1
|
470 self.NAME = oldLib and oldLib.NAME or {}
|
flickerstreak@1
|
471 self.DYNAMIC_LOCALES = oldLib and oldLib.DYNAMIC_LOCALES or {}
|
flickerstreak@1
|
472 self.CURRENT_LOCALE = oldLib and oldLib.CURRENT_LOCALE or {}
|
flickerstreak@1
|
473
|
flickerstreak@1
|
474 BASE_TRANSLATIONS = self.BASE_TRANSLATIONS
|
flickerstreak@1
|
475 DEBUGGING = self.DEBUGGING
|
flickerstreak@1
|
476 TRANSLATIONS = self.TRANSLATIONS
|
flickerstreak@1
|
477 BASE_LOCALE = self.BASE_LOCALE
|
flickerstreak@1
|
478 TRANSLATION_TABLES = self.TRANSLATION_TABLES
|
flickerstreak@1
|
479 REVERSE_TRANSLATIONS = self.REVERSE_TRANSLATIONS
|
flickerstreak@1
|
480 STRICTNESS = self.STRICTNESS
|
flickerstreak@1
|
481 NAME = self.NAME
|
flickerstreak@1
|
482 DYNAMIC_LOCALES = self.DYNAMIC_LOCALES
|
flickerstreak@1
|
483 CURRENT_LOCALE = self.CURRENT_LOCALE
|
flickerstreak@1
|
484
|
flickerstreak@1
|
485
|
flickerstreak@1
|
486 local GetTime = GetTime
|
flickerstreak@1
|
487 local timeUntilClear = GetTime() + 5
|
flickerstreak@1
|
488 scheduleClear = function()
|
flickerstreak@1
|
489 if next(newRegistries) then
|
flickerstreak@1
|
490 self.frame:Show()
|
flickerstreak@1
|
491 timeUntilClear = GetTime() + 5
|
flickerstreak@1
|
492 end
|
flickerstreak@1
|
493 end
|
flickerstreak@1
|
494
|
flickerstreak@1
|
495 if not self.registry then
|
flickerstreak@1
|
496 self.registry = {}
|
flickerstreak@1
|
497 else
|
flickerstreak@1
|
498 for name, instance in pairs(self.registry) do
|
flickerstreak@1
|
499 local name = name
|
flickerstreak@1
|
500 local mt = getmetatable(instance)
|
flickerstreak@1
|
501 setmetatable(instance, nil)
|
flickerstreak@1
|
502 instance[NAME] = name
|
flickerstreak@1
|
503 local strict
|
flickerstreak@1
|
504 if instance[STRICTNESS] ~= nil then
|
flickerstreak@1
|
505 strict = instance[STRICTNESS]
|
flickerstreak@1
|
506 elseif instance[TRANSLATIONS] ~= instance[BASE_TRANSLATIONS] then
|
flickerstreak@1
|
507 if getmetatable(instance[TRANSLATIONS]).__index == oldLib.prototype then
|
flickerstreak@1
|
508 strict = true
|
flickerstreak@1
|
509 end
|
flickerstreak@1
|
510 end
|
flickerstreak@1
|
511 instance[STRICTNESS] = strict and true or false
|
flickerstreak@1
|
512 refixInstance(instance)
|
flickerstreak@1
|
513 end
|
flickerstreak@1
|
514 end
|
flickerstreak@1
|
515
|
flickerstreak@1
|
516 self.frame:SetScript("OnEvent", scheduleClear)
|
flickerstreak@1
|
517 self.frame:SetScript("OnUpdate", function() -- (this, elapsed)
|
flickerstreak@1
|
518 if timeUntilClear - GetTime() <= 0 then
|
flickerstreak@1
|
519 self.frame:Hide()
|
flickerstreak@1
|
520 for k in pairs(newRegistries) do
|
flickerstreak@1
|
521 clearCache(k)
|
flickerstreak@1
|
522 newRegistries[k] = nil
|
flickerstreak@1
|
523 k = nil
|
flickerstreak@1
|
524 end
|
flickerstreak@1
|
525 end
|
flickerstreak@1
|
526 end)
|
flickerstreak@1
|
527 self.frame:UnregisterAllEvents()
|
flickerstreak@1
|
528 self.frame:RegisterEvent("ADDON_LOADED")
|
flickerstreak@1
|
529 self.frame:RegisterEvent("PLAYER_ENTERING_WORLD")
|
flickerstreak@1
|
530 self.frame:Show()
|
flickerstreak@1
|
531
|
flickerstreak@1
|
532 if oldDeactivate then
|
flickerstreak@1
|
533 oldDeactivate(oldLib)
|
flickerstreak@1
|
534 end
|
flickerstreak@1
|
535 end
|
flickerstreak@1
|
536
|
flickerstreak@1
|
537 AceLibrary:Register(AceLocale, MAJOR_VERSION, MINOR_VERSION, activate)
|