-
Notifications
You must be signed in to change notification settings - Fork 11
/
lang.lua
193 lines (148 loc) · 4.36 KB
/
lang.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
193
-- from gmod.one with <3
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/lang.lua
--[[ usage:
lang:Add("example", "Привет мир!")
lang:Add("hi", "Hello {?}.")
lang:Add("ban", "{?} banned {?}")
lang:Add("time", "Your total time is {?}")
if CLIENT then
lang:ChatPrint("example")
lang:Notify("hi", {LocalPlayer():Name()}, 5)
utime.ui.text = lang:Format("time", {LocalPlayer():GetUTime()})
else
lang:ChatPrint(ply, "hi", {ply:Name()})
lang:Notify(player.GetHumans(), "ban", {admin:Name(), target:Name()}, 3, NOTIFY_GENERIC)
end
lang:Remove("time")
]]--
--[[ advanced usage:
https://github.com/Be1zebub/csv2lang
]]--
local bitCount, placeholderCount
do
function bitCount(num)
return math.floor(
math.log(
tonumber(string.format("%u", num))
, 2)
) + 1
end
function placeholderCount(str)
local count = 0
for s in string.gmatch(str, "{%?}") do
count = count + 1
end
return count
end
end
local list = {}
local map = {}
local bitcount = 0
local function find(uid)
local phrase
if isnumber(uid) then
phrase = list[uid]
else
phrase = map[uid]
end
return assert(phrase, "Phrase " .. tostring(uid) .. " not found!")
end
local lang = {}
function lang:Add(name, text)
local phrase = map[name]
if phrase == nil then
phrase = {
text = text,
placeholders = placeholderCount(text)
}
map[name] = phrase
phrase.index = table.insert(list, phrase)
bitcount = bitCount(#list)
else
phrase.text = text
phrase.placeholders = placeholderCount(text)
end
return phrase
end
function lang:Remove(name)
local phrase = map[name]
if phrase == nil then return end
map[name] = nil
table.remove(list, phrase.index)
for i, v in ipairs(list) do
v.index = i
end
bitcount = bitCount(#list)
end
function lang:Get(uid)
return find(uid).text
end
function lang:Format(uid, data)
local phrase = find(uid)
if data == nil or phrase.placeholders == 0 then return phrase.text end
local i = 0
return phrase.text:gsub("{%?}", function()
i = i + 1
return data[i]
end)
end
if gmod then
if CLIENT then
function lang:Notify(uid, data, len, type)
notification.AddLegacy(lang:Format(uid, data), type or NOTIFY_GENERIC, len or 3)
end
net.Receive("gmod.one/lang:Notify", function()
local index = net.ReadUInt(bitcount)
local phrase = find(index)
local data = {}
for i = 1, phrase.placeholders do
data[i] = net.ReadString()
end
lang:Notify(index, data, net.ReadUInt(4), net.ReadUInt(3))
end)
function lang:ChatPrint(uid, data)
chat.AddText(lang:Format(uid, data))
end
net.Receive("gmod.one/lang:ChatPrint", function()
local index = net.ReadUInt(bitcount)
local phrase = find(index)
local data = {}
for i = 1, phrase.placeholders do
data[i] = net.ReadString()
end
lang:ChatPrint(index, data)
end)
else
util.AddNetworkString("gmod.one/lang:Notify")
function lang:Notify(targets, uid, data, len, type)
local phrase = find(uid)
net.Start("gmod.one/lang:Notify")
net.WriteUInt(phrase.index, bitcount)
for i = 1, phrase.placeholders do
net.WriteString(data[i])
end
net.WriteUInt(math.min(len, 15), 4)
net.WriteUInt(type, 3)
if targets then
net.Send(targets)
else
net.Broadcast()
end
end
util.AddNetworkString("gmod.one/lang:ChatPrint")
function lang:ChatPrint(targets, uid, data)
local phrase = find(uid)
net.Start("gmod.one/lang:ChatPrint")
net.WriteUInt(phrase.index, bitcount)
for i = 1, phrase.placeholders do
net.WriteString(data[i])
end
if targets then
net.Send(targets)
else
net.Broadcast()
end
end
end
end
return lang