-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot_localization.lua
59 lines (52 loc) · 1.5 KB
/
bot_localization.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
-- Copyright (C) 2018 Jérôme Leclercq
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
local fs = require("fs")
local langtables = {}
Bot.LangTables = langtables
local langFiles = fs.scandirSync("localization")
for filename, filetype in assert(langFiles) do
if filetype == "file" then
local lang = filename:match("(%w+)%.lua")
if lang then
local data, err = dofile("localization/" .. filename)
if data then
langtables[lang] = data
else
print("failed to load language data from " .. filename .. ": " .. err)
end
end
end
end
function Bot:Format(guild, str, ...)
local serverconfig = self:GetModuleForGuild(guild, "serverconfig")
if serverconfig then
local config = serverconfig:GetConfig(guild)
if config then
local langtable = langtables[config.Language or "en"]
if langtable then
local translation = langtable.Locs[str]
if translation then
return string.format(translation, ...)
end
end
end
end
return string.format(str, ...)
end
function Bot:FormatDuration(guild, seconds, depth)
local serverconfig = self:GetModuleForGuild(guild, "serverconfig")
if serverconfig then
local config = serverconfig:GetConfig(guild)
if config then
local langtable = langtables[config.Language or "en"]
if langtable then
local formatTime = langtable.FormatTime
if formatTime then
return formatTime(seconds, depth)
end
end
end
end
return util.FormatTime(seconds, depth)
end