Модуль:Loc: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки Метка: ручная отмена |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local data = mw.loadData("Модуль:IanComradeBot/loc/data") | local data = mw.loadData("Модуль:IanComradeBot/loc/data") | ||
local p = {} | local p = {} | ||
-- | -- индекс для регистронезависимого поиска | ||
function | local lowerIndex = nil | ||
return | |||
local function tolower(s) | |||
return s and mw.ustring.lower(s) or s | |||
end | end | ||
local function buildLowerIndex() | |||
local function | if lowerIndex then return end | ||
lowerIndex = {} | |||
for k, v in pairs(data) do | |||
if type(k) == "string" then | |||
local lk = tolower(k) | |||
if not lowerIndex[lk] then | |||
lowerIndex[lk] = v | |||
end | |||
end | |||
end | end | ||
end | |||
-- Возвращает значение по ключу | |||
local function getValueByKeyRaw(value, subkey) | |||
if type(value) == "string" then | if value == nil then return nil end | ||
local t = type(value) | |||
if t == "string" then | |||
return value | return value | ||
elseif t == "table" then | |||
if subkey and value[subkey] then | if subkey and value[subkey] then | ||
return value[subkey] | return value[subkey] | ||
end | end | ||
return nil | |||
end | end | ||
return nil | return nil | ||
end | end | ||
-- | -- Основной поиск | ||
local function findTextByKey( | local function findTextByKey(key, subkey) | ||
local | if not key then return nil end | ||
local direct = data[key] | |||
local result = getValueByKeyRaw(direct, subkey) | |||
if result then return result end | |||
buildLowerIndex() | |||
local lk = tolower(key) | |||
local v = lowerIndex[lk] | |||
result = getValueByKeyRaw(v, subkey) | |||
if result then return result end | |||
return nil | return nil | ||
end | end | ||
| Строка 54: | Строка 63: | ||
end | end | ||
local result = findTextByKey( | local result = findTextByKey(key, subkey) | ||
if not result then | if not result then | ||
return key | |||
end | end | ||
return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}") | return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}") | ||
end | end | ||
| Строка 71: | Строка 80: | ||
end | end | ||
local result = findTextByKey( | local result = findTextByKey(key, subkey) | ||
if not result then | if not result then | ||
return key | |||
end | end | ||
Версия от 13:12, 23 февраля 2026
Для документации этого модуля может быть создана страница Модуль:Loc/doc
local data = mw.loadData("Модуль:IanComradeBot/loc/data")
local p = {}
-- индекс для регистронезависимого поиска
local lowerIndex = nil
local function tolower(s)
return s and mw.ustring.lower(s) or s
end
local function buildLowerIndex()
if lowerIndex then return end
lowerIndex = {}
for k, v in pairs(data) do
if type(k) == "string" then
local lk = tolower(k)
if not lowerIndex[lk] then
lowerIndex[lk] = v
end
end
end
end
-- Возвращает значение по ключу
local function getValueByKeyRaw(value, subkey)
if value == nil then return nil end
local t = type(value)
if t == "string" then
return value
elseif t == "table" then
if subkey and value[subkey] then
return value[subkey]
end
return nil
end
return nil
end
-- Основной поиск
local function findTextByKey(key, subkey)
if not key then return nil end
local direct = data[key]
local result = getValueByKeyRaw(direct, subkey)
if result then return result end
buildLowerIndex()
local lk = tolower(key)
local v = lowerIndex[lk]
result = getValueByKeyRaw(v, subkey)
if result then return result 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 = findTextByKey(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 = findTextByKey(key, subkey)
if not result then
return key
end
return frame:preprocess("<nowiki>".. result .. "</nowiki>")
end
return p