comparison lib/AceLocale-3.0/AceLocale-3.0.lua @ 28:21bcaf8215ff

- converted to Ace3 - rearranged file layout - configGUI menus not working right now
author Flick <flickerstreak@gmail.com>
date Mon, 17 Mar 2008 18:24:53 +0000
parents
children
comparison
equal deleted inserted replaced
27:f1e838841ce1 28:21bcaf8215ff
1 --[[ $Id: AceLocale-3.0.lua 60131 2008-02-03 13:03:56Z nevcairiel $ ]]
2 local MAJOR,MINOR = "AceLocale-3.0", 1
3
4 local AceLocale, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
5
6 if not AceLocale then return end -- no upgrade needed
7
8 local gameLocale = GetLocale()
9 if gameLocale == "enGB" then
10 gameLocale = "enUS"
11 end
12
13 AceLocale.apps = AceLocale.apps or {} -- array of ["AppName"]=localetableref
14 AceLocale.appnames = AceLocale.appnames or {} -- array of [localetableref]="AppName"
15
16 -- This metatable is used on all tables returned from GetLocale
17 local readmeta = {
18 __index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key
19 geterrorhandler()(MAJOR..": "..tostring(AceLocale.appnames[self])..": Missing entry for '"..tostring(key).."'")
20 rawset(self, key, key) -- only need to see the warning once, really
21 return key
22 end
23 }
24
25 -- Remember the locale table being registered right now (it gets set by :NewLocale())
26 local registering
27
28 -- local assert false function
29 local assertfalse = function() assert(false) end
30
31 -- This metatable proxy is used when registering nondefault locales
32 local writeproxy = setmetatable({}, {
33 __newindex = function(self, key, value)
34 rawset(registering, key, value == true and key or value) -- assigning values: replace 'true' with key string
35 end,
36 __index = assertfalse
37 })
38
39 -- This metatable proxy is used when registering the default locale.
40 -- It refuses to overwrite existing values
41 -- Reason 1: Allows loading locales in any order
42 -- Reason 2: If 2 modules have the same string, but only the first one to be
43 -- loaded has a translation for the current locale, the translation
44 -- doesn't get overwritten.
45 --
46 local writedefaultproxy = setmetatable({}, {
47 __newindex = function(self, key, value)
48 if not rawget(registering, key) then
49 rawset(registering, key, value == true and key or value)
50 end
51 end,
52 __index = assertfalse
53 })
54
55 -- AceLocale:NewLocale(application, locale, isDefault)
56 --
57 -- application (string) - unique name of addon / module
58 -- locale (string) - name of locale to register, e.g. "enUS", "deDE", etc...
59 -- isDefault (string) - if this is the default locale being registered
60 --
61 -- Returns a table where localizations can be filled out, or nil if the locale is not needed
62 function AceLocale:NewLocale(application, locale, isDefault)
63
64 -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed
65 -- Ammo: I still think this is a bad idea, for instance an addon that checks for some ingame string will fail, just because some other addon
66 -- gives the user the illusion that they can run in a different locale? Ditch this whole thing or allow a setting per 'application'. I'm of the
67 -- opinion to remove this.
68 local gameLocale = GAME_LOCALE or gameLocale
69
70 if locale ~= gameLocale and not isDefault then
71 return -- nop, we don't need these translations
72 end
73
74 local app = AceLocale.apps[application]
75
76 if not app then
77 app = setmetatable({}, readmeta)
78 AceLocale.apps[application] = app
79 AceLocale.appnames[app] = application
80 end
81
82 registering = app -- remember globally for writeproxy and writedefaultproxy
83
84 if isDefault then
85 return writedefaultproxy
86 end
87
88 return writeproxy
89 end
90
91 -- AceLocale:GetLocale(application [, silent])
92 --
93 -- application (string) - unique name of addon
94 -- silent (boolean) - if true, the locale is optional, silently return nil if it's not found
95 --
96 -- Returns localizations for the current locale (or default locale if translations are missing)
97 -- Errors if nothing is registered (spank developer, not just a missing translation)
98 function AceLocale:GetLocale(application, silent)
99 if not silent and not AceLocale.apps[application] then
100 error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2)
101 end
102 return AceLocale.apps[application]
103 end