flickerstreak@1
|
1 --[[
|
flickerstreak@1
|
2 Name: AceLocale-2.1
|
flickerstreak@1
|
3 Revision: $Rev: 18753 $
|
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.1
|
flickerstreak@1
|
8 SVN: http://svn.wowace.com/root/trunk/Ace2/AceLocale-2.1
|
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.1"
|
flickerstreak@1
|
15 local MINOR_VERSION = "$Revision: 18753 $"
|
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 curTranslation, baseTranslation, translations, baseLocale, curLocale, strictTranslations, dynamic, reverseTranslation
|
flickerstreak@1
|
21 local AceLocale = {}
|
flickerstreak@1
|
22 local backbone = {}
|
flickerstreak@1
|
23 backbone.class, backbone.super = false, false
|
flickerstreak@1
|
24
|
flickerstreak@1
|
25 local function initReverse(self)
|
flickerstreak@1
|
26 self[reverseTranslation] = {}
|
flickerstreak@1
|
27
|
flickerstreak@1
|
28 for k, v in pairs(self[curTranslation]) do self[reverseTranslation][v] = k end
|
flickerstreak@1
|
29
|
flickerstreak@1
|
30 setmetatable(self[reverseTranslation], {
|
flickerstreak@1
|
31 __index = function(tbl, key)
|
flickerstreak@1
|
32 AceLocale:error("Reverse translation for %s not found", key)
|
flickerstreak@1
|
33 end
|
flickerstreak@1
|
34 })
|
flickerstreak@1
|
35 end
|
flickerstreak@1
|
36
|
flickerstreak@1
|
37 local function __call(obj, text, flag)
|
flickerstreak@1
|
38 if flag == nil then return obj[text] end
|
flickerstreak@1
|
39
|
flickerstreak@1
|
40 if flag == true then
|
flickerstreak@1
|
41 if rawget(obj[curTranslation], text) then AceLocale:error("Strict translation for %s not found", text) end
|
flickerstreak@1
|
42 return rawget(obj[curTranslation], text)
|
flickerstreak@1
|
43 elseif flag == false then
|
flickerstreak@1
|
44 return rawget(obj[curTranslation], arg2) or obj[baseTranslation][arg2]
|
flickerstreak@1
|
45 elseif flag == "reverse" then
|
flickerstreak@1
|
46 if not rawget(obj, reverseTranslation) then initReverse(obj) end
|
flickerstreak@1
|
47 return obj[reverseTranslation][text]
|
flickerstreak@1
|
48 else
|
flickerstreak@1
|
49 AceLocale:error("Invalid flag given to __call. Should be true/false/\"reverse\" but %s was given", flag)
|
flickerstreak@1
|
50 end
|
flickerstreak@1
|
51 end
|
flickerstreak@1
|
52
|
flickerstreak@1
|
53 local function NewInstance(self, uid)
|
flickerstreak@1
|
54 if self.registry[uid] then return self.registry[uid] end
|
flickerstreak@1
|
55
|
flickerstreak@1
|
56 self.registry[uid] = {}
|
flickerstreak@1
|
57 self.registry[uid][translations] = {}
|
flickerstreak@1
|
58
|
flickerstreak@1
|
59 setmetatable(self.registry[uid], {
|
flickerstreak@1
|
60 __tostring = function()
|
flickerstreak@1
|
61 return "AceLocale(" .. uid .. ")"
|
flickerstreak@1
|
62 end,
|
flickerstreak@1
|
63 __call = __call,
|
flickerstreak@1
|
64 __index = backbone
|
flickerstreak@1
|
65 })
|
flickerstreak@1
|
66
|
flickerstreak@1
|
67 return self.registry[uid]
|
flickerstreak@1
|
68 end
|
flickerstreak@1
|
69
|
flickerstreak@1
|
70 function AceLocale:RegisterTranslation(uid, locale, func)
|
flickerstreak@1
|
71 error(MAJOR_VERSION .. " is not supported in WoW 2.0", 2)
|
flickerstreak@1
|
72 self:argCheck(uid, 1, "string")
|
flickerstreak@1
|
73 self:argCheck(locale, 2, "string")
|
flickerstreak@1
|
74 self:argCheck(func, 3, "function")
|
flickerstreak@1
|
75
|
flickerstreak@1
|
76 local instance = self.registry[uid] or NewInstance(self, uid)
|
flickerstreak@1
|
77
|
flickerstreak@1
|
78 if instance[translations][locale] then
|
flickerstreak@1
|
79 self:error("Cannot provide the same locale more than once. %q provided twice for %s.", locale, uid)
|
flickerstreak@1
|
80 end
|
flickerstreak@1
|
81
|
flickerstreak@1
|
82 if rawget(instance, baseLocale) then
|
flickerstreak@1
|
83 for k, v in pairs(func()) do
|
flickerstreak@1
|
84 if not rawget(instance[baseTranslation], k) then
|
flickerstreak@1
|
85 self:error("Improper translation exists. %q is likely misspelled for locale %s.", k, locale)
|
flickerstreak@1
|
86 elseif value == true then
|
flickerstreak@1
|
87 self:error( "Can only accept true as a value on the base locale. %q is the base locale, %q is not.", instance[baseLocale], locale)
|
flickerstreak@1
|
88 end
|
flickerstreak@1
|
89 end
|
flickerstreak@1
|
90 else
|
flickerstreak@1
|
91 instance[baseTranslation] = func()
|
flickerstreak@1
|
92 instance[baseLocale] = locale
|
flickerstreak@1
|
93
|
flickerstreak@1
|
94 for k, v in pairs(instance[baseTranslation]) do
|
flickerstreak@1
|
95 if type(v) ~= "string" and type(v) ~= "table" then
|
flickerstreak@1
|
96 if type(v) == "boolean" then
|
flickerstreak@1
|
97 instance[baseTranslation][k] = k
|
flickerstreak@1
|
98 else
|
flickerstreak@1
|
99 self:error("Translation for %s is invalid. Must be either string or boolean", k)
|
flickerstreak@1
|
100 end
|
flickerstreak@1
|
101 end
|
flickerstreak@1
|
102 end
|
flickerstreak@1
|
103
|
flickerstreak@1
|
104 setmetatable(instance[baseTranslation], {__index = backbone})
|
flickerstreak@1
|
105 end
|
flickerstreak@1
|
106
|
flickerstreak@1
|
107 instance[translations][locale] = func
|
flickerstreak@1
|
108 end
|
flickerstreak@1
|
109
|
flickerstreak@1
|
110 function AceLocale:GetInstance(uid, locale)
|
flickerstreak@1
|
111 self:argCheck(uid, 1, "string")
|
flickerstreak@1
|
112
|
flickerstreak@1
|
113 local instance = self.registry[uid]
|
flickerstreak@1
|
114
|
flickerstreak@1
|
115 if not instance then self:error("At least one translation must be registered before you can GetInstance().") end
|
flickerstreak@1
|
116
|
flickerstreak@1
|
117 instance:SetLocale(locale)
|
flickerstreak@1
|
118
|
flickerstreak@1
|
119 return instance
|
flickerstreak@1
|
120 end
|
flickerstreak@1
|
121
|
flickerstreak@1
|
122 function AceLocale:HasInstance(uid)
|
flickerstreak@1
|
123 self:argCheck(uid, 1, "string")
|
flickerstreak@1
|
124 return self.registry[uid] and true or false
|
flickerstreak@1
|
125 end
|
flickerstreak@1
|
126
|
flickerstreak@1
|
127 setmetatable(backbone, {__index =
|
flickerstreak@1
|
128 function(tbl, key)
|
flickerstreak@1
|
129 AceLocale:error("Translation for %s not found", key)
|
flickerstreak@1
|
130 end})
|
flickerstreak@1
|
131
|
flickerstreak@1
|
132 function backbone:SetLocale(locale)
|
flickerstreak@1
|
133 local loose = false
|
flickerstreak@1
|
134 if locale == nil then return end
|
flickerstreak@1
|
135
|
flickerstreak@1
|
136 if locale == true then
|
flickerstreak@1
|
137 locale = GetLocale()
|
flickerstreak@1
|
138 if rawget(self, curLocale) and self[curLocale] == locale then return end
|
flickerstreak@1
|
139 if not self[translations][locale] then locale = self[baseLocale] end
|
flickerstreak@1
|
140 end
|
flickerstreak@1
|
141
|
flickerstreak@1
|
142 if rawget(self, curLocale) and self[curLocale] == locale then return end
|
flickerstreak@1
|
143
|
flickerstreak@1
|
144 if not self[translations][locale] then
|
flickerstreak@1
|
145 AceLocale:error("Cannot SetLocale to %s for %s, It has not been registered.", locale, tostring(self))
|
flickerstreak@1
|
146 end
|
flickerstreak@1
|
147
|
flickerstreak@1
|
148 if self[translations][locale] and self[baseLocale] == locale then
|
flickerstreak@1
|
149 self[curLocale] = self[baseLocale]
|
flickerstreak@1
|
150 self[curTranslation] = {}
|
flickerstreak@1
|
151 getmetatable(self).__index = self[baseTranslation]
|
flickerstreak@1
|
152 else
|
flickerstreak@1
|
153 self[curLocale] = locale
|
flickerstreak@1
|
154 self[curTranslation] = self[translations][locale]()
|
flickerstreak@1
|
155 getmetatable(self).__index = self[curTranslation]
|
flickerstreak@1
|
156 end
|
flickerstreak@1
|
157
|
flickerstreak@1
|
158 if rawget(self, strictTranslations) then
|
flickerstreak@1
|
159 setmetatable(self[curTranslation], {
|
flickerstreak@1
|
160 __index = function(tbl, key)
|
flickerstreak@1
|
161 AceLocale:error("Translation for %s not found", key)
|
flickerstreak@1
|
162 end
|
flickerstreak@1
|
163 })
|
flickerstreak@1
|
164 else
|
flickerstreak@1
|
165 setmetatable(self[curTranslation], {
|
flickerstreak@1
|
166 __index = self[baseTranslation]
|
flickerstreak@1
|
167 })
|
flickerstreak@1
|
168 end
|
flickerstreak@1
|
169
|
flickerstreak@1
|
170 if not rawget(self, dynamic) then
|
flickerstreak@1
|
171 self[translations] = {}
|
flickerstreak@1
|
172 end
|
flickerstreak@1
|
173
|
flickerstreak@1
|
174 if rawget(self, reverseTranslation) then
|
flickerstreak@1
|
175 self[reverseTranslation] = nil
|
flickerstreak@1
|
176 end
|
flickerstreak@1
|
177 end
|
flickerstreak@1
|
178
|
flickerstreak@1
|
179 function backbone:ClearLocales()
|
flickerstreak@1
|
180 self[translations] = {}
|
flickerstreak@1
|
181 self[curLocale] = nil
|
flickerstreak@1
|
182 self[baseLocale] = nil
|
flickerstreak@1
|
183 end
|
flickerstreak@1
|
184
|
flickerstreak@1
|
185 function backbone:SetDynamicLocales(flag)
|
flickerstreak@1
|
186 AceLocale:argCheck(flag, 1, "boolean")
|
flickerstreak@1
|
187 self[dynamic] = flag
|
flickerstreak@1
|
188 end
|
flickerstreak@1
|
189
|
flickerstreak@1
|
190 function backbone:SetStrictness(flag)
|
flickerstreak@1
|
191 AceLocale:argCheck(flag, 1, "boolean")
|
flickerstreak@1
|
192 local mt
|
flickerstreak@1
|
193
|
flickerstreak@1
|
194 if rawget(self, curTranslation) then
|
flickerstreak@1
|
195 mt = getmetatable(self[curTranslation])
|
flickerstreak@1
|
196 end
|
flickerstreak@1
|
197
|
flickerstreak@1
|
198 if strict and mt then
|
flickerstreak@1
|
199 mt.__index = function(tbl, key)
|
flickerstreak@1
|
200 AceLocale:error("Translation for %s not found", key)
|
flickerstreak@1
|
201 end
|
flickerstreak@1
|
202 elseif mt then
|
flickerstreak@1
|
203 mt.__index = self[baseTranslation]
|
flickerstreak@1
|
204 end
|
flickerstreak@1
|
205
|
flickerstreak@1
|
206 self[strictTranslations] = strict
|
flickerstreak@1
|
207 end
|
flickerstreak@1
|
208
|
flickerstreak@1
|
209 function backbone:HasTranslation(text)
|
flickerstreak@1
|
210 AceLocale:argCheck(text, 1, "string")
|
flickerstreak@1
|
211
|
flickerstreak@1
|
212 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call HasTranslation().") end
|
flickerstreak@1
|
213
|
flickerstreak@1
|
214 return rawget(self[curTranslation], text) and true or false
|
flickerstreak@1
|
215 end
|
flickerstreak@1
|
216
|
flickerstreak@1
|
217 function backbone:HasReverseTranslation(text)
|
flickerstreak@1
|
218 AceLocale:argCheck(text, 1, "string")
|
flickerstreak@1
|
219
|
flickerstreak@1
|
220 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call HasReverseTranslation().") end
|
flickerstreak@1
|
221
|
flickerstreak@1
|
222 if not rawget(self, reverseTranslation) then
|
flickerstreak@1
|
223 initReverse(self)
|
flickerstreak@1
|
224 end
|
flickerstreak@1
|
225
|
flickerstreak@1
|
226 return rawget(self[reverseTranslation], text) and true or false
|
flickerstreak@1
|
227 end
|
flickerstreak@1
|
228
|
flickerstreak@1
|
229 function backbone:GetIterator()
|
flickerstreak@1
|
230 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call GetIterator().") end
|
flickerstreak@1
|
231 return pairs(self[curTranslation])
|
flickerstreak@1
|
232 end
|
flickerstreak@1
|
233
|
flickerstreak@1
|
234 function backbone:GetReverseIterator()
|
flickerstreak@1
|
235 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call HasReverseTranslation().") end
|
flickerstreak@1
|
236
|
flickerstreak@1
|
237 if not rawget(self, reverseTranslation) then
|
flickerstreak@1
|
238 initReverse(self)
|
flickerstreak@1
|
239 end
|
flickerstreak@1
|
240
|
flickerstreak@1
|
241 return pairs(self[reverseTranslation])
|
flickerstreak@1
|
242 end
|
flickerstreak@1
|
243
|
flickerstreak@1
|
244 function backbone:GetLocaleList()
|
flickerstreak@1
|
245 local results = {}
|
flickerstreak@1
|
246 for k, v in pairs(self[translations]) do tinsert(results, k) end
|
flickerstreak@1
|
247 return results
|
flickerstreak@1
|
248 end
|
flickerstreak@1
|
249
|
flickerstreak@1
|
250 local function activate(self, oldLib, oldDeactivate)
|
flickerstreak@1
|
251 AceLocale = self
|
flickerstreak@1
|
252
|
flickerstreak@1
|
253 if oldLib then
|
flickerstreak@1
|
254 self.registry = oldLib.registry
|
flickerstreak@1
|
255 self.curTranslation = oldLib.curTranslation
|
flickerstreak@1
|
256 self.baseTranslation = oldLib.baseTranslation
|
flickerstreak@1
|
257 self.translations = oldLib.translations
|
flickerstreak@1
|
258 self.baseLocale = oldLib.baseLocale
|
flickerstreak@1
|
259 self.curLocale = oldLib.curLocale
|
flickerstreak@1
|
260 self.strictTranslations = oldLib.strictTranslations
|
flickerstreak@1
|
261 self.dynamic = oldLib.dynamic
|
flickerstreak@1
|
262 self.reverseTranslation = oldLib.reverseTranslation
|
flickerstreak@1
|
263 end
|
flickerstreak@1
|
264
|
flickerstreak@1
|
265 if not self.registry then self.registry = {} end
|
flickerstreak@1
|
266 if not self.curTranslation then self.curTranslation = {} end
|
flickerstreak@1
|
267 if not self.baseTranslation then self.baseTranslation = {} end
|
flickerstreak@1
|
268 if not self.translations then self.translations = {} end
|
flickerstreak@1
|
269 if not self.baseLocale then self.baseLocale = {} end
|
flickerstreak@1
|
270 if not self.curLocale then self.curLocale = {} end
|
flickerstreak@1
|
271 if not self.strictTranslations then self.strictTranslations = {} end
|
flickerstreak@1
|
272 if not self.dynamic then self.dynamic = {} end
|
flickerstreak@1
|
273 if not self.reverseTranslation then self.reverseTranslation = {} end
|
flickerstreak@1
|
274
|
flickerstreak@1
|
275 if oldDeactivate then
|
flickerstreak@1
|
276 oldDeactivate(oldLib)
|
flickerstreak@1
|
277 end
|
flickerstreak@1
|
278
|
flickerstreak@1
|
279 curTranslation = self.curTranslation
|
flickerstreak@1
|
280 baseTranslation = self.baseTranslation
|
flickerstreak@1
|
281 translations = self.translations
|
flickerstreak@1
|
282 baseLocale = self.baseLocale
|
flickerstreak@1
|
283 curLocale = self.curLocale
|
flickerstreak@1
|
284 strictTranslations = self.strictTranslations
|
flickerstreak@1
|
285 dynamic = self.dynamic
|
flickerstreak@1
|
286 reverseTranslation = self.reverseTranslation
|
flickerstreak@1
|
287 end
|
flickerstreak@1
|
288
|
flickerstreak@1
|
289 AceLibrary:Register(AceLocale, MAJOR_VERSION, MINOR_VERSION, activate)
|
flickerstreak@1
|
290 AceLocale = AceLibrary(MAJOR_VERSION) |