Difference between revisions 18626567 and 18626568 on frwiktionary

local m_languages = mw.loadData("Module:languages")
local export = {}

-- transliterate the text, if possible
function export.translit(lang, text)
    -- TODO: the table's information should be moved to [[Module:languages]]
    local translit_modules = {
        ["ae"] = "Module:Avst-translit",
        ["ady"] = "Module:ady-translit",
        ["el"] = "Module:el-translit",
        ["ru"] = "Module:ru-translit",
        ["ug"] = "Module:ug-translit",
        ["tg"] = "Module:tg-translit",
        ["os"] = "Module:os-translit",
        ["ka"] = "Module:ka-translit",
        ["oge"] = "Module:oge-translit",
        ["xmf"] = "Module:xmf-translit",
        ["lzz"] = "Module:lzz-translit",
        ["sva"] = "Module:sva-translit",
        ["xcl"] = "Module:xcl-translit",
        ["axm"] = "Module:axm-translit",
        ["hy"] = "Module:hy-translit"
    }
 
    if translit_modules[lang] then
        return require(translit_modules[lang]).tr(text)
    end
end
 
-- Detect the script based on the first alphabetical characters of a string
function export.detect_script(text, lang)
    local m_scripts = mw.loadData("Module:scripts")
    
    -- first try to detect the script based on the native script(s) of the language
    local scripts = languages[lang].scripts or {}local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    
    -- Does this language have more than one script?
    -- If not, we can bypass the detection for a speed bonus.
    -- But always do the detection if the script is "None" or "Zyyy"
    if not (langinfo.scripts[2] or langinfo.scripts[1] == "None" or langinfo.scripts[1] == "Zyyy") then
        return langinfo.scripts[1], false
    end
    
    for i, script in ipairs(scripts) do
        local script2check = script:gsub(".-%-", "") -- removes the language code from script name, e.g. "nv-Latn" > "Latn"
        if script2check == "Latf" or script2check == "Latinx" or script2check == "unicode" then
            script2check = "Latn"
        end
        
        local scriptinfo = m_scripts[script2check] or error("The script code \"" .. script .. "\" is not valid.")
        if scriptinfo.characters and mw.ustring.match(text, scriptinfo.characters) then
            return script, false
        end
    end
    
    -- not written in native script(s); check for all scripts
    -- TODO: This is slow; we really shouldn't be doing this!
    for script, scriptinfo in pairs(m_scripts) do
        if scriptinfo.characters and mw.ustring.match(text, "[%[%d%p%s]-" .. scriptinfo.characters) then
            return script, (scripts[1] ~= "Zyyy" and scripts[1] ~= "None")
        end
    end
    
    return langinfo.scripts[1], false
end

-- Format the categories with the appropriate sort key
function export.format_categories(categories, lang, sort_key, sort_base)
    local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
    
    if NAMESPACE == "" or NAMESPACE == "Appendix" then
        PAGENAME = PAGENAME or mw.title.getCurrentTitle().text
        SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
        
        -- Generate a default language-independent sort key
(contracted; show full)        if NAMESPACE == "Template" then
            lang = "und"
        else
            error("Language code has not been specified. Please pass parameter 1 to the template.")
        end
    end
    
    local langinfo = 
m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    
    local prefix = ""
    if format == "pos" then
        prefix = langinfo.names[1] .. " "
    elseif format == "topic" then
        prefix = lang .. ":"
    end
    
    local i = 2
    local cat = args[i]
    
    while cat do
        if cat ~= "" then
            table.insert(categories, prefix .. cat)
        end
        
        i = i + 1
        cat = args[i]
    end
    
    return export.format_categories(categories, lang, sort_key)
end

return export