forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalization.lua
More file actions
187 lines (156 loc) · 3.85 KB
/
localization.lua
File metadata and controls
187 lines (156 loc) · 3.85 KB
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
local FILENAME = "localization.csv"
Localization = class(function(self)
self.data = {}
self.langs = {}
self.codes = {}
self.lang_index = 1
self.init = false
end)
localization = Localization()
function Localization.get_list_codes(self)
return self.codes
end
function Localization.get_language(self)
return self.codes[self.lang_index]
end
function Localization.refresh_global_strings(self)
join_community_msg = loc("join_community").."\ndiscord.panelattack.com"
end
function Localization.set_language(self, lang_code)
for i, v in ipairs(self.codes) do
if v == lang_code then
self.lang_index = i
break
end
end
config.language_code = self.codes[self.lang_index]
if config.language_code == "JP" then
set_global_font("jp.ttf", 14)
else
set_global_font(nil, 12)
end
self:refresh_global_strings()
end
function Localization.init(self)
local function csv_line(line, acc)
local function trim(a, b)
if line:sub(a, a) == '"' then
a = a+1
end
if line:sub(b, b) == '"' then
b = b-1
end
return line:sub(a, b)
end
local tokens = {}
local leftover = nil
local cur = 1
local stop_cur = 1
local escape = (acc ~= nil)
local ch
while cur <= line:len() do
ch = line:sub(cur, cur)
if ch == '"' then
if line:sub(cur+1, cur+1) == '"' then
cur = cur + 1
else
escape = not escape
end
elseif not escape and ch == ',' then
tokens[#tokens+1] = trim(stop_cur, cur-1)
if acc then
tokens[#tokens] = acc..tokens[#tokens]
acc = nil
end
tokens[#tokens] = tokens[#tokens]:gsub('""', '"')
stop_cur = cur + 1
end
cur = cur + 1
end
if escape then
if not acc then
leftover = line:sub(stop_cur+1, cur)
else
leftover = acc..line:sub(stop_cur, cur)
end
leftover = leftover.."\n"
else
tokens[#tokens+1] = trim(stop_cur, cur)
end
return tokens, leftover
end
self.init = true
local num_line = 1
local tokens, leftover
local i = 1
local key = nil
if love.filesystem.getInfo(FILENAME) then
for line in love.filesystem.lines(FILENAME) do
if num_line == 1 then
tokens = csv_line(line)
for i, v in ipairs(tokens) do
if i > 1 and v:gsub("%s", ""):len() > 0 then
self.codes[#self.codes+1] = v
self.data[v] = {}
end
end
else
tokens, leftover = csv_line(line, leftover)
for _, v in ipairs(tokens) do
if not key then
key = v
if key == "" or key:match("%s+") then
break
end
else
if v ~= "" and not v:match("^%s+$") then
if num_line == 2 then
self.langs[#self.langs+1] = v
end
self.data[self.codes[i]][key] = v
end
i = i+1
end
if i > #self.codes then
break
end
end
if not leftover then
i = 1
key = nil
end
end
num_line = num_line + 1
end
end
--[[ for k, v in pairs(self.data) do
print("LANG "..k)
for a, b in pairs(v) do
print(a..": "..b)
end
end--]]
self:set_language(config.language_code)
end
function loc(text_key, ...)
local self = localization
local code = self.codes[self.lang_index]
if not code or not self.data[code] then
code = self.codes[1]
end
local ret = nil
if self.init then
ret = self.data[code][text_key]
end
if ret then
for i = 1, select('#', ...) do
local tmp = select(i, ...)
ret = ret:gsub("%%"..i, tmp)
end
else
ret = "#"..text_key
for i = 1, select('#', ...) do
ret = ret.." "..select(i, ...)
end
end
return ret
end