Difference between revisions 18626583 and 18626584 on frwiktionary

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

-- 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/tempdata")
    
    local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
    local scFix = false

    for i, script in ipairs(langinfo.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"
        elseif script2check == "Hans" or script2check == "Hant" then
            script2check = "Hani"
        end
        
        local scriptinfo = m_scripts[script2check] or error("The script code \"" .. script .. "\" is not valid.")
        if scriptinfo.characters and mw.ustring.match(text, "[%[%d%p%s]-[" .. scriptinfo.characters .. "]") then
            return script, scFix
        end
    end
    
    scFix = (langinfo.scripts[1] ~= "Zyyy" and langinfo.scripts[1] ~= "None")
    
    -- 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, scFix
        end
    end
    
    return langinfo.scripts[1], scFix
end

(contracted; show full)        i = i + 1
        cat = args[i]
    end
    
    return export.format_categories(categories, lang, sort_key)
end

return export