Модуль:Loc: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 2: | Строка 2: | ||
local p = {} | local p = {} | ||
local function tolower(s) | local function tolower(s) | ||
| Строка 10: | Строка 7: | ||
end | end | ||
local function | local complexLowerIndex = nil | ||
if | |||
local function buildComplexIndex() | |||
if complexLowerIndex then return end | |||
complexLowerIndex = {} | |||
for k, v in pairs(data) do | for k, v in pairs(data) do | ||
if type(k) == "string" then | if type(v) == "table" and type(k) == "string" then | ||
local lk = tolower(k) | local lk = tolower(k) | ||
if not | if not complexLowerIndex[lk] then | ||
complexLowerIndex[lk] = v | |||
end | end | ||
end | end | ||
| Строка 23: | Строка 22: | ||
end | end | ||
local function getValueFromEntry(entry, subkey) | |||
local function | if entry == nil then return nil end | ||
if | local t = type(entry) | ||
local t = type( | |||
if t == "string" then | if t == "string" then | ||
return | return entry | ||
elseif t == "table" then | elseif t == "table" then | ||
if subkey | if subkey then | ||
return | return entry[subkey] | ||
else | |||
return entry["_value"] | |||
end | end | ||
end | end | ||
return nil | return nil | ||
end | end | ||
local function findTextByKeyOptimized(key, subkey) | |||
local function | |||
if not key then return nil end | if not key then return nil end | ||
local direct = data[key] | local direct = data[key] | ||
local | local r = getValueFromEntry(direct, subkey) | ||
if | if r then return r end | ||
buildComplexIndex() | |||
local lk = tolower(key) | local lk = tolower(key) | ||
local | local complexEntry = complexLowerIndex[lk] | ||
if complexEntry then | |||
local rr = getValueFromEntry(complexEntry, subkey) | |||
if rr then return rr end | |||
end | |||
return nil | return nil | ||
| Строка 63: | Строка 63: | ||
end | end | ||
local result = | local result = findTextByKeyOptimized(key, subkey) | ||
if not result then | if not result then | ||
| Строка 80: | Строка 80: | ||
end | end | ||
local result = | local result = findTextByKeyOptimized(key, subkey) | ||
if not result then | if not result then | ||
return key | return key | ||
Версия от 13:44, 23 февраля 2026
Для документации этого модуля может быть создана страница Модуль:Loc/doc
local data = mw.loadData("Модуль:IanComradeBot/loc/data")
local p = {}
local function tolower(s)
return s and mw.ustring.lower(s) or s
end
local complexLowerIndex = nil
local function buildComplexIndex()
if complexLowerIndex then return end
complexLowerIndex = {}
for k, v in pairs(data) do
if type(v) == "table" and type(k) == "string" then
local lk = tolower(k)
if not complexLowerIndex[lk] then
complexLowerIndex[lk] = v
end
end
end
end
local function getValueFromEntry(entry, subkey)
if entry == nil then return nil end
local t = type(entry)
if t == "string" then
return entry
elseif t == "table" then
if subkey then
return entry[subkey]
else
return entry["_value"]
end
end
return nil
end
local function findTextByKeyOptimized(key, subkey)
if not key then return nil end
local direct = data[key]
local r = getValueFromEntry(direct, subkey)
if r then return r end
buildComplexIndex()
local lk = tolower(key)
local complexEntry = complexLowerIndex[lk]
if complexEntry then
local rr = getValueFromEntry(complexEntry, subkey)
if rr then return rr end
end
return nil
end
function p.GetString(frame)
local key = frame.args[1]
local subkey = frame.args[2] or "_value"
if not key then
return "Ошибка: Не указаны все необходимые параметры."
end
local result = findTextByKeyOptimized(key, subkey)
if not result then
return key
end
return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}")
end
function p.GetRawString(frame)
local key = frame.args[1]
local subkey = frame.args[2] or "_value"
if not key then
return "Ошибка: Не указаны все необходимые параметры."
end
local result = findTextByKeyOptimized(key, subkey)
if not result then
return key
end
return frame:preprocess("<nowiki>".. result .. "</nowiki>")
end
return p