This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
186 lines (144 loc) · 4.22 KB
/
init.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
local zflib = 'zf-lib'
local export = exports[zflib]
if not GetResourceState(zflib):find('start') then
error('^1zf-lib should be started before this resource.^0', 2)
end
-----------------------------------------------
--------------- Module handling ---------------
-----------------------------------------------
local LoadResourceFile = LoadResourceFile
local context = IsDuplicityVersion() and 'server' or 'client'
function noop() end
local function loadModule(self, module)
local dir = ('modules/%s'):format(module)
local chunk = LoadResourceFile(zflib, ('%s/%s.lua'):format(dir, context))
local shared = LoadResourceFile(zflib, ('%s/shared.lua'):format(dir))
if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
end
if chunk then
local fn, err = load(chunk, ('@@' .. zflib .. '/%s/%s.lua'):format(module, context))
if not fn or err then
return error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
end
local result = fn()
self[module] = result or noop
return self[module]
end
end
local function call(self, index, ...)
local module = rawget(self, index)
if not module then
self[index] = noop
module = loadModule(self, index)
if not module then
local function method(...)
return export[index](nil, ...)
end
if not ... then
self[index] = method
end
return method
end
end
return module
end
zf = setmetatable({
name = zflib,
context = context
}, {
__index = call,
__call = call,
})
requireMod = zf.requireMod
-----------------------------------------------
------------- Locales from QBCore -------------
-----------------------------------------------
Locale = {}
Locale.__index = Locale
local function translateKey(phrase, subs)
if type(phrase) ~= 'string' then
error('TypeError: translateKey function expects arg #1 to be a string')
end
if not subs then
return phrase
end
local result = phrase
for k, v in pairs(subs) do
local templateToFind = '%%{' .. k .. '}'
result = result:gsub(templateToFind, tostring(v)) -- string to allow all types
end
return result
end
function Locale.new(_, opts)
local self = setmetatable({}, Locale)
self.fallback = opts.fallbackLang and Locale:new({
warnOnMissing = false,
phrases = opts.fallbackLang.phrases,
}) or false
self.warnOnMissing = type(opts.warnOnMissing) ~= 'boolean' and true or opts.warnOnMissing
self.phrases = {}
self:extend(opts.phrases or {})
return self
end
function Locale:extend(phrases, prefix)
for key, phrase in pairs(phrases) do
local prefixKey = prefix and ('%s.%s'):format(prefix, key) or key
-- If this is a nested table, we need to go reeeeeeeeeeeecursive
if type(phrase) == 'table' then
self:extend(phrase, prefixKey)
else
self.phrases[prefixKey] = phrase
end
end
end
function Locale:clear()
self.phrases = {}
end
function Locale:replace(phrases)
phrases = phrases or {}
self:clear()
self:extend(phrases)
end
function Locale:locale(newLocale)
if (newLocale) then
self.currentLocale = newLocale
end
return self.currentLocale
end
function Locale:t(key, subs)
local phrase, result
subs = subs or {}
if type(self.phrases[key]) == 'string' then
phrase = self.phrases[key]
else
if self.warnOnMissing then
print(('^3Warning: Missing phrase for key: "%s"'):format(key))
end
if self.fallback then
return self.fallback:t(key, subs)
end
result = key
end
if type(phrase) == 'string' then
result = translateKey(phrase, subs)
end
return result
end
function Locale:has(key)
return self.phrases[key] ~= nil
end
function Locale:delete(phraseTarget, prefix)
if type(phraseTarget) == 'string' then
self.phrases[phraseTarget] = nil
else
for key, phrase in pairs(phraseTarget) do
local prefixKey = prefix and prefix .. '.' .. key or key
if type(phrase) == 'table' then
self:delete(phrase, prefixKey)
else
self.phrases[prefixKey] = nil
end
end
end
end