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

Материал из Space Station 14 Вики
мНет описания правки
Нет описания правки
Строка 7: Строка 7:
function p.getData()
function p.getData()
     return data
     return data
end
-- Получение значения по ключу с поддержкой подключачей
local function getValueByKey(data, key, subkey)
    if not data[key] then
        return nil
    end
   
    local value = data[key]
   
    -- Если значение - строка, возвращаем её
    if type(value) == "string" then
        return value
    end
   
    -- Если значение - таблица (вложенная структура)
    if type(value) == "table" then
        if subkey and value[subkey] then
            return value[subkey]
        end
    end
   
    return nil
end
end


-- Поиск текста по ключу (без учета регистра)
-- Поиск текста по ключу (без учета регистра)
local function findTextByKey(data, key)
local function findTextByKey(data, key, subkey)
     local searchKey = string.lower(key)
     local searchKey = string.lower(key)
     for categoryName, category in pairs(data) do
     for k, value in pairs(data) do
         for k, value in pairs(category) do
         if string.lower(k) == searchKey then
            if string.lower(k) == searchKey then
            local result = getValueByKey(data, k, subkey)
                 return value
            if result then
                 return result
             end
             end
         end
         end
Строка 23: Строка 47:


-- Поиск ключа по тексту (без учета регистра для текста)
-- Поиск ключа по тексту (без учета регистра для текста)
local function findKeyByText(data, text)
local function findKeyByText(data, text, subkey)
     local searchText = string.lower(text)
     local searchText = string.lower(text)
     for categoryName, category in pairs(data) do
   
         for key, value in pairs(category) do
     for key, value in pairs(data) do
            if string.lower(value) == searchText then
         local val = getValueByKey(data, key, subkey)
                return key
        if val and string.lower(val) == searchText then
            end
            return key
         end
         end
     end
     end
     return nil
     return nil
end
-- Получение всех строк из категории (без учета регистра)
local function getCategoryStrings(data, categoryName)
    local searchCategory = string.lower(categoryName)
    local matchedCategory = nil
    for catName, catData in pairs(data) do
        if string.lower(catName) == searchCategory then
            matchedCategory = catData
            break
        end
    end
    if not matchedCategory then
        return nil
    end
    local result = {}
    for _, value in pairs(matchedCategory) do
        if type(value) == "string" then
            table.insert(result, value)
        end
    end
    return result
end
end


Строка 64: Строка 63:
     local mode = frame.args[1]
     local mode = frame.args[1]
     local param = frame.args[2]
     local param = frame.args[2]
    local subkey = frame.args[3] or "_value"


     if not mode or not param then
     if not mode or not param then
Строка 70: Строка 70:


     if mode == "translation" then
     if mode == "translation" then
         local result = findTextByKey(data, param)
         local result = findTextByKey(data, param, subkey)
         return result or "Ошибка: Ключ не найден."
         return result or "Ошибка: Ключ не найден."
     elseif mode == "key" then
     elseif mode == "key" then
         local result = findKeyByText(data, param)
         local result = findKeyByText(data, param, subkey)
         return result or "Ошибка: Текст не найден."
         return result or "Ошибка: Текст не найден."
    elseif mode == "categories" then
        local strings = getCategoryStrings(data, param)
        if not strings or #strings == 0 then
            return "Ошибка: Категория не найдена или пуста."
        end
        local output = {}
        for _, value in ipairs(strings) do
            table.insert(output, "<li>" .. mw.text.encode(value) .. "</li>")
        end
        return table.concat(output)
     else
     else
         return "Ошибка: Неизвестный режим работы."
         return "Ошибка: Неизвестный режим работы."

Версия от 10:04, 12 января 2026

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

-- Загрузка данных
local data = mw.loadData("Модуль:IanComradeBot/ftl/ru-RU.json/data")

local p = {}

-- Функция для получения таблицы данных
function p.getData()
    return data
end

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

-- Поиск текста по ключу (без учета регистра)
local function findTextByKey(data, key, subkey)
    local searchKey = string.lower(key)
    for k, value in pairs(data) do
        if string.lower(k) == searchKey then
            local result = getValueByKey(data, k, subkey)
            if result then
                return result
            end
        end
    end
    return nil
end

-- Поиск ключа по тексту (без учета регистра для текста)
local function findKeyByText(data, text, subkey)
    local searchText = string.lower(text)
    
    for key, value in pairs(data) do
        local val = getValueByKey(data, key, subkey)
        if val and string.lower(val) == searchText then
            return key
        end
    end
    return nil
end

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

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

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

return p