Revision 18626557 of "Module:utilities" on frwiktionarylocal 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",
["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)
-- list of characters that may occur at the beginning of a word
-- TODO: move to [[Module:scripts]]
local chars_table = {
["Latn"] = "A-Za-z",
["Arab"] = "-ۿݐ-ݿ",
["Armn"] = "Ա-֊",
["Beng"] = "ঁ-৺",
["Cyrl"] = "Ѐ-ӿ",
["Deva"] = "ँ-ॽ",
["Geor"] = "Ⴀ-ჼ",
["Goth"] = "𐌰-𐍊",
["Grek"] = "ʹ-Ͽ",
["Hebr"] = "א-ת",
["Khmr"] = "ក-៹",
["Laoo"] = "ກ-ໝ",
["Mong"] = "᠀-ᢪ",
["Mymr"] = "က-ၙ",
["Thai"] = "ก-ฺ",
["Sinh"] = "ං-෴",
-- TODO
}
chars_table["fa-Arab"] = chars_table["Arab"]
chars_table["glk-Arab"] = chars_table["Arab"]
chars_table["kk-Arab"] = chars_table["Arab"]
chars_table["ks-Arab"] = chars_table["Arab"]
chars_table["ku-Arab"] = chars_table["Arab"]
chars_table["mzn-Arab"] = chars_table["Arab"]
chars_table["ota-Arab"] = chars_table["Arab"]
chars_table["pa-Arab"] = chars_table["Arab"]
chars_table["ps-Arab"] = chars_table["Arab"]
chars_table["sd-Arab"] = chars_table["Arab"]
chars_table["tt-Arab"] = chars_table["Arab"]
chars_table["ug-Arab"] = chars_table["Arab"]
chars_table["ur-Arab"] = chars_table["Arab"]
chars_table["Latf"] = chars_table["Latn"]
chars_table["Latinx"] = chars_table["Latn"] .. "ƀ-ɏ"
chars_table["nv-Latn"] = chars_table["Latn"] .. "ḻṉṟṯ"
chars_table["pjt-Latn"] = chars_table["Latn"]
-- first try to detect the script based on the native script(s) of the language
local scripts = languages[lang].scripts or {}
for i, script in ipairs(scripts) do
if chars_table[script] and mw.ustring.match(text, "[" .. chars_table[script] .. "]") then
return script
end
end
-- not written in native script(s); check for all scripts
for script, chars in ipairs(chars_table) do
if mw.ustring.match(text, "[%[%d%p%s]-[" .. chars .. "]") then
return script
end
end
end
-- Format the categories with the appropriate sort key
function export.format_categories(categories, lang, sort_key, sort_base)
local langinfo = 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
sort_base = mw.ustring.lower(sort_base or SUBPAGENAME)
sort_base = mw.ustring.gsub(sort_base, "^[-־ـ*]+(.)",
"%1") -- Remove initial hyphens and *
sort_base = mw.ustring.gsub(sort_base, "%([^()]+%)", "") -- Remove anything in parentheses
-- If there are language-specific rules to generate the key, use those
if langinfo.sort_key then
for i, from in ipairs(langinfo.sort_key.from) do
local to = langinfo.sort_key.to[i] or ""
sort_base = mw.ustring.gsub(sort_base, from, to)
end
end
if sort_key then
-- Gather some statistics regarding sort keys
if mw.ustring.lower(sort_key) == sort_base then
table.insert(categories, "Sort key tracking/redundant")
elseif lang ~= "cmn" and lang ~= "ja" and lang ~= "zu" and lang ~= "nan" and lang ~= "yue" then
if lang == "ga" or lang == "gv" or lang == "nv" or lang == "roa-jer" or lang == "fr" or lang == "rm" or lang == "prg" or lang == "gd" or lang == "twf" or lang == "en" or lang == "ro" or lang == "egl" or lang == "roa-tar" or lang == "gl" or lang == "ast" or lang == "br" then
table.insert(categories, "Sort key tracking/needed/" .. lang)
else
table.insert(categories, "Sort key tracking/needed")
end
end
else
sort_key = sort_base
end
-- If the resulting key is the same as the wiki software's default, remove it
if sort_key == PAGENAME then
sort_key = nil
end
for key, cat in ipairs(categories) do
categories[key] = "[[Category:" .. cat .. (sort_key and "|" .. sort_key or "") .. "]]"
end
return table.concat(categories, "")
else
return ""
end
end
-- Used by {{categorize}}
function export.template_categorize(frame)
NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
local format = frame.args["format"]
local args = frame:getParent().args
local lang = args[1]
local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
local categories = {}
if lang == "" or lang == nil then
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 = 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 exportAll content in the above text box is licensed under the Creative Commons Attribution-ShareAlike license Version 4 and was originally sourced from https://fr.wiktionary.org/w/index.php?oldid=18626557.
![]() ![]() This site is not affiliated with or endorsed in any way by the Wikimedia Foundation or any of its affiliates. In fact, we fucking despise them.
|