vanilla-wow-addons – Blame information for rev 1

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