-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocaleManager.lua
192 lines (170 loc) · 5.16 KB
/
LocaleManager.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
--[[
* Copyright (c) 2011-2012 by Adam Hellberg.
*
* This file is part of Command.
*
* Command is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Command is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Command. If not, see <http://www.gnu.org/licenses/>.
--]]
-- NOTE: All error() calls use constant strings in english for debugging reasons.
-- NOTE: return after error() shouldn't be needed. Consider remove.
-- Upvalues
local type = type
local pairs = pairs
local tostring = tostring
local setmetatable = setmetatable
-- Custom GetLocale()
local BlizzGetLocale = GetLocale
local GetLocale = function()
local l = BlizzGetLocale()
if l == "enGB" then l = "enUS" end
return l
end
local C = Command
C.LocaleManager = {
Settings = {},
Master = "enUS", -- Fallback locale
Active = GetLocale(),
Locales = {}
}
local LM = C.LocaleManager
local function l_index(self, k)
k = tostring(k):upper()
master = LM:GetMaster()
if self == master then -- Prevent recursion
return ("%s"):format(k)
end
local val = master[k]
if val then return val end
end
local s_meta = {
__call = function(self, arg1, arg2, isPlr)
if not arg1 and not arg2 then return nil end
if arg2 then -- (locale, key)
return self:GetLocale(arg1, isPlr)[tostring(arg2):upper()]
end
arg1 = tostring(arg1):upper()
return self:GetActive()[arg1]
end
}
-- Initialize LocaleManager
function LM:Init()
if self.Active == "enGB" then self.Active = "enUS" end
setmetatable(self, s_meta)
self:LoadSavedVars()
end
-- Load the saved variables
function LM:LoadSavedVars()
if type(C.Settings.LOCALE_MANAGER) ~= "table" then
C.Settings.LOCALE_MANAGER = {}
end
self.Settings = C.Settings.LOCALE_MANAGER
if type(self.Settings.LOCALE) ~= "string" then
self.Settings.LOCALE = self.Active
end
if type(self.Settings.PLAYER_INDEPENDENT) ~= "boolean" then
self.Settings.PLAYER_INDEPENDENT = true
end
end
-- Register a new locale (might add a check against master to make sure no keys are missing)
function LM:Register(locale, localeTable)
if type(locale) ~= "string" or type(localeTable) ~= "table" then
error("Invalid arguments for Register. Expected [string, table], got ["..type(locale)..", "..type(localeTable).."]!")
return
end
locale = locale:lower()
if self:LocaleLoaded(locale) then return end
self.Locales[locale] = {}
for k,v in pairs(localeTable) do
if type(v) == "string" then
self.Locales[locale][k] = v
end
end
setmetatable(self.Locales[locale], {__index = l_index})
end
-- Check if a locale has been loaded/registered
function LM:LocaleLoaded(locale)
locale = locale:lower()
if self.Locales[locale] then
return true
end
return false
end
-- Get the locale <locale>
-- Will handle nonexistant locale
-- However, if for some reason the client default or fallback locale is NOT loaded...
-- ^NOTE^ Above should not happen, under any circumstance what-so-ever unless Locales table is modified.
function LM:GetLocale(locale, isPlr)
if isPlr and not self.Settings.PLAYER_INDEPENDENT then
locale = self.Settings.LOCALE
end
locale = locale or self.Settings.LOCALE
locale = locale:lower()
if locale == "engb" then locale = "enus" end
if not self:LocaleLoaded(locale) then
if locale ~= self.Settings.LOCALE:lower() then -- Prevent infinite loop...
return self:GetActive()
elseif locale ~= self.Master:lower() then
return self:GetMaster()
else
error("FATAL: GetLocale unable to resolve to working locale.")
return nil
end
end
return self.Locales[locale]
end
-- Returns client locale (or user setting if set)
function LM:GetActive()
return self:GetLocale(self.Settings.LOCALE)
end
-- Returns master (fallback) locale
function LM:GetMaster()
if not self:LocaleLoaded(self.Master) then
error("FATAL! Master locale not loaded, AddOn cannot function!")
return
end
return LM:GetLocale(self.Master)
end
-- Set the locale to use
function LM:SetLocale(locale)
if locale:lower() == "engb" then locale = "enUS" end
if not self:LocaleLoaded(locale) then
return false, "LOCALE_NOT_LOADED"
end
self.Settings.LOCALE = locale
return "LOCALE_UPDATE", {self.Settings.LOCALE}
end
-- Reset locale to client default
function LM:ResetLocale()
return self:SetLocale(self.Active)
end
-- Set locale to master (fallback) locale
function LM:UseMasterLocale()
return self:SetLocale(self.Master)
end
function LM:SetPlayerIndependent(active)
self.Settings.PLAYER_INDEPENDENT = active
if self.Settings.PLAYER_INDEPENDENT then
return "LOCALE_PI_ACTIVE"
end
return "LOCALE_PI_INACTIVE"
end
function LM:EnablePlayerIndependent()
return self:SetPlayerIndependent(true)
end
function LM:DisablePlayerIndependent()
return self:SetPlayerIndependent(false)
end
function LM:TogglePlayerIndependent()
return self:SetPlayerIndependent(not self.Settings.PLAYER_INDEPENDENT)
end