forked from olhodedeus/Redlol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
languages.lua
82 lines (67 loc) · 2.47 KB
/
languages.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
local config = require 'config'
local strings = {} -- internal array with translated strings
-- Evaluates the Lua's expression
local function eval(str)
return load('return ' .. str)()
end
-- Parses the file with translation and returns a table with English strings as
-- keys and translated strings as values. The keys are stored without a leading
-- linebreak for crawling bugs of xgettext. It adds needless linebreak to
-- string literals with long brackets. Fuzzy translations are ignored if flag
-- config.allow_fuzzy_translations isn't set.
local function parse(filename)
local state = 'ign_msgstr' -- states of finite state machine
local msgid, msgstr
local result = {}
for line in io.lines(filename) do
line = line:trim()
local input, argument = line:match('^(%w*)%s*(".*")$')
if line:match('^#,.*fuzzy') then
input = 'fuzzy'
end
assert(state == 'msgid' or state == 'msgstr' or state == 'ign_msgid' or state == 'ign_msgstr')
assert(input == nil or input == '' or input == 'msgid' or input == 'msgstr' or input == 'fuzzy')
if state == 'msgid' and input == '' then
msgid = msgid .. eval(argument)
elseif state == 'msgid' and input == 'msgstr' then
msgstr = eval(argument)
state = 'msgstr'
elseif state == 'msgstr' and input == '' then
msgstr = msgstr .. eval(argument)
elseif state == 'msgstr' and input == 'msgid' then
if msgstr ~= '' then result[msgid:gsub('^\n', '')] = msgstr end
msgid = eval(argument)
state = 'msgid'
elseif state == 'msgstr' and input == 'fuzzy' then
if msgstr ~= '' then result[msgid:gsub('^\n', '')] = msgstr end
if not config.allow_fuzzy_translations then
state = 'ign_msgid'
end
elseif state == 'ign_msgid' and input == 'msgstr' then
state = 'ign_msgstr'
elseif state == 'ign_msgstr' and input == 'msgid' then
msgid = eval(argument)
state = 'msgid'
elseif state == 'ign_msgstr' and input == 'fuzzy' then
state = 'ign_msgid'
end
end
if state == 'msgstr' and msgstr ~= '' then
result[msgid:gsub('^\n', '')] = msgstr
end
return result
end
local locale = {} -- table with exported functions
locale.language = 'pt_BR' -- default language
function locale.init(directory)
directory = directory or "locales"
for lang_code in pairs(config.available_languages) do
strings[lang_code] = parse(string.format('%s/%s.po', directory, lang_code))
end
end
function locale.translate(msgid)
return strings[locale.language][msgid:gsub('^\n', '')] or msgid
end
_ = locale.translate
locale.init()
return locale