comparison Libs/AceLocale-3.0/AceLocale-3.0.lua @ 0:98c6f55e6619

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