yellowfive@57
|
1 --- **AceLocale-3.0** manages localization in addons, allowing for multiple locale to be registered with fallback to the base locale for untranslated strings.
|
yellowfive@57
|
2 -- @class file
|
yellowfive@57
|
3 -- @name AceLocale-3.0
|
yellowfive@57
|
4 -- @release $Id: AceLocale-3.0.lua 1035 2011-07-09 03:20:13Z kaelten $
|
yellowfive@57
|
5 local MAJOR,MINOR = "AceLocale-3.0", 6
|
yellowfive@57
|
6
|
yellowfive@57
|
7 local AceLocale, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
|
yellowfive@57
|
8
|
yellowfive@57
|
9 if not AceLocale then return end -- no upgrade needed
|
yellowfive@57
|
10
|
yellowfive@57
|
11 -- Lua APIs
|
yellowfive@57
|
12 local assert, tostring, error = assert, tostring, error
|
yellowfive@57
|
13 local getmetatable, setmetatable, rawset, rawget = getmetatable, setmetatable, rawset, rawget
|
yellowfive@57
|
14
|
yellowfive@57
|
15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
|
yellowfive@57
|
16 -- List them here for Mikk's FindGlobals script
|
yellowfive@57
|
17 -- GLOBALS: GAME_LOCALE, geterrorhandler
|
yellowfive@57
|
18
|
yellowfive@57
|
19 local gameLocale = GetLocale()
|
yellowfive@57
|
20 if gameLocale == "enGB" then
|
yellowfive@57
|
21 gameLocale = "enUS"
|
yellowfive@57
|
22 end
|
yellowfive@57
|
23
|
yellowfive@57
|
24 AceLocale.apps = AceLocale.apps or {} -- array of ["AppName"]=localetableref
|
yellowfive@57
|
25 AceLocale.appnames = AceLocale.appnames or {} -- array of [localetableref]="AppName"
|
yellowfive@57
|
26
|
yellowfive@57
|
27 -- This metatable is used on all tables returned from GetLocale
|
yellowfive@57
|
28 local readmeta = {
|
yellowfive@57
|
29 __index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key
|
yellowfive@57
|
30 rawset(self, key, key) -- only need to see the warning once, really
|
yellowfive@57
|
31 geterrorhandler()(MAJOR..": "..tostring(AceLocale.appnames[self])..": Missing entry for '"..tostring(key).."'")
|
yellowfive@57
|
32 return key
|
yellowfive@57
|
33 end
|
yellowfive@57
|
34 }
|
yellowfive@57
|
35
|
yellowfive@57
|
36 -- This metatable is used on all tables returned from GetLocale if the silent flag is true, it does not issue a warning on unknown keys
|
yellowfive@57
|
37 local readmetasilent = {
|
yellowfive@57
|
38 __index = function(self, key) -- requesting totally unknown entries: return key
|
yellowfive@57
|
39 rawset(self, key, key) -- only need to invoke this function once
|
yellowfive@57
|
40 return key
|
yellowfive@57
|
41 end
|
yellowfive@57
|
42 }
|
yellowfive@57
|
43
|
yellowfive@57
|
44 -- Remember the locale table being registered right now (it gets set by :NewLocale())
|
yellowfive@57
|
45 -- NOTE: Do never try to register 2 locale tables at once and mix their definition.
|
yellowfive@57
|
46 local registering
|
yellowfive@57
|
47
|
yellowfive@57
|
48 -- local assert false function
|
yellowfive@57
|
49 local assertfalse = function() assert(false) end
|
yellowfive@57
|
50
|
yellowfive@57
|
51 -- This metatable proxy is used when registering nondefault locales
|
yellowfive@57
|
52 local writeproxy = setmetatable({}, {
|
yellowfive@57
|
53 __newindex = function(self, key, value)
|
yellowfive@57
|
54 rawset(registering, key, value == true and key or value) -- assigning values: replace 'true' with key string
|
yellowfive@57
|
55 end,
|
yellowfive@57
|
56 __index = assertfalse
|
yellowfive@57
|
57 })
|
yellowfive@57
|
58
|
yellowfive@57
|
59 -- This metatable proxy is used when registering the default locale.
|
yellowfive@57
|
60 -- It refuses to overwrite existing values
|
yellowfive@57
|
61 -- Reason 1: Allows loading locales in any order
|
yellowfive@57
|
62 -- Reason 2: If 2 modules have the same string, but only the first one to be
|
yellowfive@57
|
63 -- loaded has a translation for the current locale, the translation
|
yellowfive@57
|
64 -- doesn't get overwritten.
|
yellowfive@57
|
65 --
|
yellowfive@57
|
66 local writedefaultproxy = setmetatable({}, {
|
yellowfive@57
|
67 __newindex = function(self, key, value)
|
yellowfive@57
|
68 if not rawget(registering, key) then
|
yellowfive@57
|
69 rawset(registering, key, value == true and key or value)
|
yellowfive@57
|
70 end
|
yellowfive@57
|
71 end,
|
yellowfive@57
|
72 __index = assertfalse
|
yellowfive@57
|
73 })
|
yellowfive@57
|
74
|
yellowfive@57
|
75 --- Register a new locale (or extend an existing one) for the specified application.
|
yellowfive@57
|
76 -- :NewLocale will return a table you can fill your locale into, or nil if the locale isn't needed for the players
|
yellowfive@57
|
77 -- game locale.
|
yellowfive@57
|
78 -- @paramsig application, locale[, isDefault[, silent]]
|
yellowfive@57
|
79 -- @param application Unique name of addon / module
|
yellowfive@57
|
80 -- @param locale Name of the locale to register, e.g. "enUS", "deDE", etc.
|
yellowfive@57
|
81 -- @param isDefault If this is the default locale being registered (your addon is written in this language, generally enUS)
|
yellowfive@57
|
82 -- @param silent If true, the locale will not issue warnings for missing keys. Must be set on the first locale registered. If set to "raw", nils will be returned for unknown keys (no metatable used).
|
yellowfive@57
|
83 -- @usage
|
yellowfive@57
|
84 -- -- enUS.lua
|
yellowfive@57
|
85 -- local L = LibStub("AceLocale-3.0"):NewLocale("TestLocale", "enUS", true)
|
yellowfive@57
|
86 -- L["string1"] = true
|
yellowfive@57
|
87 --
|
yellowfive@57
|
88 -- -- deDE.lua
|
yellowfive@57
|
89 -- local L = LibStub("AceLocale-3.0"):NewLocale("TestLocale", "deDE")
|
yellowfive@57
|
90 -- if not L then return end
|
yellowfive@57
|
91 -- L["string1"] = "Zeichenkette1"
|
yellowfive@57
|
92 -- @return Locale Table to add localizations to, or nil if the current locale is not required.
|
yellowfive@57
|
93 function AceLocale:NewLocale(application, locale, isDefault, silent)
|
yellowfive@57
|
94
|
yellowfive@57
|
95 -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed
|
yellowfive@57
|
96 local gameLocale = GAME_LOCALE or gameLocale
|
yellowfive@57
|
97
|
yellowfive@57
|
98 local app = AceLocale.apps[application]
|
yellowfive@57
|
99
|
yellowfive@57
|
100 if silent and app and getmetatable(app) ~= readmetasilent then
|
yellowfive@57
|
101 geterrorhandler()("Usage: NewLocale(application, locale[, isDefault[, silent]]): 'silent' must be specified for the first locale registered")
|
yellowfive@57
|
102 end
|
yellowfive@57
|
103
|
yellowfive@57
|
104 if not app then
|
yellowfive@57
|
105 if silent=="raw" then
|
yellowfive@57
|
106 app = {}
|
yellowfive@57
|
107 else
|
yellowfive@57
|
108 app = setmetatable({}, silent and readmetasilent or readmeta)
|
yellowfive@57
|
109 end
|
yellowfive@57
|
110 AceLocale.apps[application] = app
|
yellowfive@57
|
111 AceLocale.appnames[app] = application
|
yellowfive@57
|
112 end
|
yellowfive@57
|
113
|
yellowfive@57
|
114 if locale ~= gameLocale and not isDefault then
|
yellowfive@57
|
115 return -- nop, we don't need these translations
|
yellowfive@57
|
116 end
|
yellowfive@57
|
117
|
yellowfive@57
|
118 registering = app -- remember globally for writeproxy and writedefaultproxy
|
yellowfive@57
|
119
|
yellowfive@57
|
120 if isDefault then
|
yellowfive@57
|
121 return writedefaultproxy
|
yellowfive@57
|
122 end
|
yellowfive@57
|
123
|
yellowfive@57
|
124 return writeproxy
|
yellowfive@57
|
125 end
|
yellowfive@57
|
126
|
yellowfive@57
|
127 --- Returns localizations for the current locale (or default locale if translations are missing).
|
yellowfive@57
|
128 -- Errors if nothing is registered (spank developer, not just a missing translation)
|
yellowfive@57
|
129 -- @param application Unique name of addon / module
|
yellowfive@57
|
130 -- @param silent If true, the locale is optional, silently return nil if it's not found (defaults to false, optional)
|
yellowfive@57
|
131 -- @return The locale table for the current language.
|
yellowfive@57
|
132 function AceLocale:GetLocale(application, silent)
|
yellowfive@57
|
133 if not silent and not AceLocale.apps[application] then
|
yellowfive@57
|
134 error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2)
|
yellowfive@57
|
135 end
|
yellowfive@57
|
136 return AceLocale.apps[application]
|
yellowfive@57
|
137 end
|