Mercurial > wow > reaction
comparison libs/AceLocale-2.1/AceLocale-2.1.lua @ 1:c11ca1d8ed91
Version 0.1
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Tue, 20 Mar 2007 21:03:57 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:4e2ce2894c21 | 1:c11ca1d8ed91 |
---|---|
1 --[[ | |
2 Name: AceLocale-2.1 | |
3 Revision: $Rev: 18753 $ | |
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: 18753 $" | |
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 error(MAJOR_VERSION .. " is not supported in WoW 2.0", 2) | |
72 self:argCheck(uid, 1, "string") | |
73 self:argCheck(locale, 2, "string") | |
74 self:argCheck(func, 3, "function") | |
75 | |
76 local instance = self.registry[uid] or NewInstance(self, uid) | |
77 | |
78 if instance[translations][locale] then | |
79 self:error("Cannot provide the same locale more than once. %q provided twice for %s.", locale, uid) | |
80 end | |
81 | |
82 if rawget(instance, baseLocale) then | |
83 for k, v in pairs(func()) do | |
84 if not rawget(instance[baseTranslation], k) then | |
85 self:error("Improper translation exists. %q is likely misspelled for locale %s.", k, locale) | |
86 elseif value == true then | |
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) | |
88 end | |
89 end | |
90 else | |
91 instance[baseTranslation] = func() | |
92 instance[baseLocale] = locale | |
93 | |
94 for k, v in pairs(instance[baseTranslation]) do | |
95 if type(v) ~= "string" and type(v) ~= "table" then | |
96 if type(v) == "boolean" then | |
97 instance[baseTranslation][k] = k | |
98 else | |
99 self:error("Translation for %s is invalid. Must be either string or boolean", k) | |
100 end | |
101 end | |
102 end | |
103 | |
104 setmetatable(instance[baseTranslation], {__index = backbone}) | |
105 end | |
106 | |
107 instance[translations][locale] = func | |
108 end | |
109 | |
110 function AceLocale:GetInstance(uid, locale) | |
111 self:argCheck(uid, 1, "string") | |
112 | |
113 local instance = self.registry[uid] | |
114 | |
115 if not instance then self:error("At least one translation must be registered before you can GetInstance().") end | |
116 | |
117 instance:SetLocale(locale) | |
118 | |
119 return instance | |
120 end | |
121 | |
122 function AceLocale:HasInstance(uid) | |
123 self:argCheck(uid, 1, "string") | |
124 return self.registry[uid] and true or false | |
125 end | |
126 | |
127 setmetatable(backbone, {__index = | |
128 function(tbl, key) | |
129 AceLocale:error("Translation for %s not found", key) | |
130 end}) | |
131 | |
132 function backbone:SetLocale(locale) | |
133 local loose = false | |
134 if locale == nil then return end | |
135 | |
136 if locale == true then | |
137 locale = GetLocale() | |
138 if rawget(self, curLocale) and self[curLocale] == locale then return end | |
139 if not self[translations][locale] then locale = self[baseLocale] end | |
140 end | |
141 | |
142 if rawget(self, curLocale) and self[curLocale] == locale then return end | |
143 | |
144 if not self[translations][locale] then | |
145 AceLocale:error("Cannot SetLocale to %s for %s, It has not been registered.", locale, tostring(self)) | |
146 end | |
147 | |
148 if self[translations][locale] and self[baseLocale] == locale then | |
149 self[curLocale] = self[baseLocale] | |
150 self[curTranslation] = {} | |
151 getmetatable(self).__index = self[baseTranslation] | |
152 else | |
153 self[curLocale] = locale | |
154 self[curTranslation] = self[translations][locale]() | |
155 getmetatable(self).__index = self[curTranslation] | |
156 end | |
157 | |
158 if rawget(self, strictTranslations) then | |
159 setmetatable(self[curTranslation], { | |
160 __index = function(tbl, key) | |
161 AceLocale:error("Translation for %s not found", key) | |
162 end | |
163 }) | |
164 else | |
165 setmetatable(self[curTranslation], { | |
166 __index = self[baseTranslation] | |
167 }) | |
168 end | |
169 | |
170 if not rawget(self, dynamic) then | |
171 self[translations] = {} | |
172 end | |
173 | |
174 if rawget(self, reverseTranslation) then | |
175 self[reverseTranslation] = nil | |
176 end | |
177 end | |
178 | |
179 function backbone:ClearLocales() | |
180 self[translations] = {} | |
181 self[curLocale] = nil | |
182 self[baseLocale] = nil | |
183 end | |
184 | |
185 function backbone:SetDynamicLocales(flag) | |
186 AceLocale:argCheck(flag, 1, "boolean") | |
187 self[dynamic] = flag | |
188 end | |
189 | |
190 function backbone:SetStrictness(flag) | |
191 AceLocale:argCheck(flag, 1, "boolean") | |
192 local mt | |
193 | |
194 if rawget(self, curTranslation) then | |
195 mt = getmetatable(self[curTranslation]) | |
196 end | |
197 | |
198 if strict and mt then | |
199 mt.__index = function(tbl, key) | |
200 AceLocale:error("Translation for %s not found", key) | |
201 end | |
202 elseif mt then | |
203 mt.__index = self[baseTranslation] | |
204 end | |
205 | |
206 self[strictTranslations] = strict | |
207 end | |
208 | |
209 function backbone:HasTranslation(text) | |
210 AceLocale:argCheck(text, 1, "string") | |
211 | |
212 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call HasTranslation().") end | |
213 | |
214 return rawget(self[curTranslation], text) and true or false | |
215 end | |
216 | |
217 function backbone:HasReverseTranslation(text) | |
218 AceLocale:argCheck(text, 1, "string") | |
219 | |
220 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call HasReverseTranslation().") end | |
221 | |
222 if not rawget(self, reverseTranslation) then | |
223 initReverse(self) | |
224 end | |
225 | |
226 return rawget(self[reverseTranslation], text) and true or false | |
227 end | |
228 | |
229 function backbone:GetIterator() | |
230 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call GetIterator().") end | |
231 return pairs(self[curTranslation]) | |
232 end | |
233 | |
234 function backbone:GetReverseIterator() | |
235 if not rawget(self, curTranslation) then AceLocale:error("A locale must be chosen before you can call HasReverseTranslation().") end | |
236 | |
237 if not rawget(self, reverseTranslation) then | |
238 initReverse(self) | |
239 end | |
240 | |
241 return pairs(self[reverseTranslation]) | |
242 end | |
243 | |
244 function backbone:GetLocaleList() | |
245 local results = {} | |
246 for k, v in pairs(self[translations]) do tinsert(results, k) end | |
247 return results | |
248 end | |
249 | |
250 local function activate(self, oldLib, oldDeactivate) | |
251 AceLocale = self | |
252 | |
253 if oldLib then | |
254 self.registry = oldLib.registry | |
255 self.curTranslation = oldLib.curTranslation | |
256 self.baseTranslation = oldLib.baseTranslation | |
257 self.translations = oldLib.translations | |
258 self.baseLocale = oldLib.baseLocale | |
259 self.curLocale = oldLib.curLocale | |
260 self.strictTranslations = oldLib.strictTranslations | |
261 self.dynamic = oldLib.dynamic | |
262 self.reverseTranslation = oldLib.reverseTranslation | |
263 end | |
264 | |
265 if not self.registry then self.registry = {} end | |
266 if not self.curTranslation then self.curTranslation = {} end | |
267 if not self.baseTranslation then self.baseTranslation = {} end | |
268 if not self.translations then self.translations = {} end | |
269 if not self.baseLocale then self.baseLocale = {} end | |
270 if not self.curLocale then self.curLocale = {} end | |
271 if not self.strictTranslations then self.strictTranslations = {} end | |
272 if not self.dynamic then self.dynamic = {} end | |
273 if not self.reverseTranslation then self.reverseTranslation = {} end | |
274 | |
275 if oldDeactivate then | |
276 oldDeactivate(oldLib) | |
277 end | |
278 | |
279 curTranslation = self.curTranslation | |
280 baseTranslation = self.baseTranslation | |
281 translations = self.translations | |
282 baseLocale = self.baseLocale | |
283 curLocale = self.curLocale | |
284 strictTranslations = self.strictTranslations | |
285 dynamic = self.dynamic | |
286 reverseTranslation = self.reverseTranslation | |
287 end | |
288 | |
289 AceLibrary:Register(AceLocale, MAJOR_VERSION, MINOR_VERSION, activate) | |
290 AceLocale = AceLibrary(MAJOR_VERSION) |