Модуль:Loc: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
Метка: отменено
мНет описания правки
 
(не показана 21 промежуточная версия этого же участника)
Строка 1: Строка 1:
-- Загрузка данных
local data = mw.loadData("Модуль:IanComradeBot/loc.json/data")
local data = mw.loadData("Модуль:IanComradeBot/ftl/ru-RU.json/data")


local p = {}
local p = {}


-- Функция для получения таблицы данных
local function tolower(s)
function p.getData()
     return s and mw.ustring.lower(s) or s
     return data
end
end


local function convertSpaces(text)
local complexLowerIndex = nil
     if not text then
 
         return 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
    return text:gsub("%s+", " ")
end
end


-- Получение значения по ключу с поддержкой подключачей
local function getValueFromEntry(entry, subkey)
local function getValueByKey(data, key, subkey)
     if entry == nil then return nil end
     if not data[key] then
     local t = type(entry)
        return nil
     if t == "string" then
    end
         return entry
   
     elseif t == "table" then
     local value = data[key]
         if subkey then
   
            return entry[subkey]
    -- Если значение - строка, возвращаем её
        else
     if type(value) == "string" then
             return entry["_value"]
         return value
    end
   
    -- Если значение - таблица (вложенная структура)
     if type(value) == "table" then
         if subkey and value[subkey] then
             return value[subkey]
         end
         end
     end
     end
   
     return nil
     return nil
end
end


-- Поиск текста по ключу (без учета регистра)
local function findTextByKeyOptimized(key, subkey)
local function findTextByKey(data, key, subkey)
    if not key then return nil end
     local searchKey = string.lower(key)
 
     for k, value in pairs(data) do
     local direct = data[key]
        if string.lower(k) == searchKey then
     local r = getValueFromEntry(direct, subkey)
            local result = getValueByKey(data, k, subkey)
    if r then return r end
            if result then
 
                return convertSpaces(result)
    buildComplexIndex()
            end
    local lk = tolower(key)
        end
    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 function findKeyByText(data, text, subkey)
    local key = frame.args[1]
     local searchText = string.lower(text)
     local subkey = frame.args[2] or "_value"
   
 
     for key, value in pairs(data) do
     if not key then
         local val = getValueByKey(data, key, subkey)
         return "Ошибка: Не указаны все необходимые параметры."
        if val and string.lower(val) == searchText then
    end
            return key
 
        end
    local result = findTextByKeyOptimized(key, subkey)
 
    if not result then
        return key
     end
     end
     return nil
 
     return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}")
end
end


-- Основная функция модуля
function p.GetRawString(frame)
function p.main(frame)
     local key = frame.args[1]
     local mode = frame.args[1]
     local subkey = frame.args[2] or "_value"
     local param = frame.args[2]
    local subkey = frame.args[3] or "_value"


     if not mode or not param then
     if not key then
         return "Ошибка: Не указаны все необходимые параметры."
         return "Ошибка: Не указаны все необходимые параметры."
     end
     end


     if mode == "translation" then
     local result = findTextByKeyOptimized(key, subkey)
        local result = findTextByKey(data, param, subkey)
 
        return result or "Ошибка: Ключ не найден."
     if not result then
     elseif mode == "key" then
         return key
        local result = findKeyByText(data, param, subkey)
         return result or "Ошибка: Текст не найден."
    else
        return "Ошибка: Неизвестный режим работы."
     end
     end
    return frame:preprocess("<nowiki>".. result .. "</nowiki>")
end
end


return p
return p

Текущая версия от 17:57, 24 февраля 2026

Для документации этого модуля может быть создана страница Модуль:Loc/doc

local data = mw.loadData("Модуль:IanComradeBot/loc.json/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