comparison Libs/AceLocale-3.0/AceLocale-3.0.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children
comparison
equal deleted inserted replaced
56:75431c084aa0 57:01b63b8ed811
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 1035 2011-07-09 03:20:13Z kaelten $
5 local MAJOR,MINOR = "AceLocale-3.0", 6
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 getmetatable, setmetatable, rawset, rawget = getmetatable, 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. Must be set on the first locale registered. If set to "raw", nils will be returned for unknown keys (no metatable used).
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 -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed
96 local gameLocale = GAME_LOCALE or gameLocale
97
98 local app = AceLocale.apps[application]
99
100 if silent and app and getmetatable(app) ~= readmetasilent then
101 geterrorhandler()("Usage: NewLocale(application, locale[, isDefault[, silent]]): 'silent' must be specified for the first locale registered")
102 end
103
104 if not app then
105 if silent=="raw" then
106 app = {}
107 else
108 app = setmetatable({}, silent and readmetasilent or readmeta)
109 end
110 AceLocale.apps[application] = app
111 AceLocale.appnames[app] = application
112 end
113
114 if locale ~= gameLocale and not isDefault then
115 return -- nop, we don't need these translations
116 end
117
118 registering = app -- remember globally for writeproxy and writedefaultproxy
119
120 if isDefault then
121 return writedefaultproxy
122 end
123
124 return writeproxy
125 end
126
127 --- Returns localizations for the current locale (or default locale if translations are missing).
128 -- Errors if nothing is registered (spank developer, not just a missing translation)
129 -- @param application Unique name of addon / module
130 -- @param silent If true, the locale is optional, silently return nil if it's not found (defaults to false, optional)
131 -- @return The locale table for the current language.
132 function AceLocale:GetLocale(application, silent)
133 if not silent and not AceLocale.apps[application] then
134 error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2)
135 end
136 return AceLocale.apps[application]
137 end