Модуль:Loc: различия между версиями
Pok (обсуждение | вклад) мНет описания правки Метка: ручная отмена |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 13 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
local JsonPaths = require('Module:JsonPaths') | |||
local data = mw.loadData(" | local getArgs = require('Module:Arguments').getArgs | ||
local data = mw.loadData(JsonPaths.get("loc.json")) | |||
local p = {} | local p = {} | ||
local function tolower(s) | |||
function | return s and mw.ustring.lower(s) or s | ||
return | |||
end | end | ||
local complexLowerIndex = nil | |||
local function | |||
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 | ||
end | |||
local function getValueFromEntry(entry, subkey) | |||
if entry == nil then return nil end | |||
if | local t = type(entry) | ||
return | if t == "string" then | ||
return entry | |||
elseif t == "table" then | |||
if subkey then | |||
return entry[subkey] | |||
if subkey | else | ||
return | 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 | ||
local | |||
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 | end | ||
return nil | return nil | ||
end | end | ||
function p.GetString(frame) | |||
local | local args = getArgs(frame, { removeBlanks = false }) | ||
local | local key = args[1] | ||
local subkey = args[2] or "_value" | |||
local noFormat = args.noFormat or "" | |||
local | |||
if not key then | |||
return "Ошибка: Не указаны все необходимые параметры." | |||
end | |||
local result = findTextByKeyOptimized(key, subkey) | |||
if not result then | |||
return key | |||
end | end | ||
if noFormat == "" then | |||
return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}") | |||
else | |||
return frame:preprocess("{{#invoke:Loc/Marking|main|noFormat=1|<nowiki>".. result .. "</nowiki>}}") | |||
end | |||
end | end | ||
function p.GetRawString(frame) | |||
function p. | local args = getArgs(frame, { removeBlanks = false }) | ||
local | local key = args[1] | ||
local | local subkey = args[2] or "_value" | ||
local | local noFormat = args.noFormat or "" | ||
if not | if not key then | ||
return "Ошибка: Не указаны все необходимые параметры." | return "Ошибка: Не указаны все необходимые параметры." | ||
end | end | ||
local result = findTextByKeyOptimized(key, subkey) | |||
if not result then | |||
return key | |||
return | |||
end | end | ||
if noFormat == "" then | |||
return result | |||
else | |||
return frame:preprocess("<nowiki>".. result .. "</nowiki>") | |||
end | |||
end | end | ||
return p | return p | ||