ماڈیول:Language

Documentation icon دستاویز
require('Module:No globals')local m_data = mw.loadData("Module:Language/data")local p = {}local sub = mw.ustring.sublocal gsub = mw.ustring.gsublocal find = mw.ustring.findlocal match = mw.ustring.matchlocal lower = mw.ustring.lowerlocal upper = mw.ustring.upperlocal function checkForString(variable)return variable ~= "" and variable ~= nilendlocal function makeLinkedName(languageCode)local data = m_data[languageCode]local article = data["article"]local name = data["Wikipedia_name"] or data["name"]return "[[" .. article .. "|" .. name .. "]]:&nbsp;"endlocal function makeEntryName(word, languageCode)local data = m_data[languageCode]word = tostring(word)if word == nil thenerror("The function makeEntryName requires a string argument")elseif word == "" thenreturn ""else-- Remove bold and italics, so that words that contain bolding or emphasis can be linked without piping.word = gsub(word, "\'\'\'", "")word = gsub(word, "\'\'", "")if data == nil thenreturn wordelselocal replacements = data and data["replacements"]if replacements == nil thenreturn wordelsefor regex, replacement in pairs(replacements) doword = gsub(word, regex, replacement)endreturn wordendendendendlocal function getCodes(codes, text)local languageCode, scriptCode, invalidCodelocal errorTextif codes == nil or codes == "" thenerrorText = 'no language or script code provided'elseif find(codes, "^%s*%a%a%a?%s*$") or find(codes, "^%s*%a%a%a?%-%a%a%a%a%s*$") then-- A three- or two-letter lowercase sequence at beginning of first parameterlanguageCode =find(codes, "^%s*%a%a%a?") and (match(codes, "^%s*(%l%l%l?)")or gsub(match(codes, "^%s*(%a%a%a?)"),"(%a%a%a?)",function(a)return lower(a)end,1))-- One uppercase and three lowercase letters at the end of the first parameterscriptCode =find(codes, "%a%a%a%a%s*$") and (match(codes, "(%u%l%l%l)%s*$")or gsub(match(codes, "(%a%a%a%a)%s*$"),"(%a)(%a%a%a)",function(a, b)return upper(a) .. lower(b)end,1))elseif find(codes, "^%s*%a%a%a%-%a%a%a$") thenlanguageCode = match(codes, "^%s*%l%l%l%-%l%l%l$") and match (codes, "^%s*%l%l%l%-%l%l%l$") or gsub(match(codes, "^%s*%a%a%a%-%a%a%a$"), "(%a%a%a?)", function(a) return lower(a) end, 1)elseif find(codes, "^%s*%a%a%a?") thenlanguageCode, invalidCode = match(codes, "^%s*(%a%a%a?)%-?(.*)")languageCode = lower(languageCode)errorText = '<code>'..invalidCode..'</code> is not a valid script code.'elseif find(codes, "%-?%a%a%a%a%s*$") theninvalidCode, scriptCode = match(codes, "(.*)%-?(%a%a%a%a)%s*$")scriptCode = gsub(scriptCode,"(%a)(%a%a%a)",function(a, b)return upper(a) .. lower(b)end)errorText = '<code>'..invalidCode..'</code> is not a valid language code.'elseerrorText = '<code>'..codes..'</code> is not a valid language or script code.'endif not scriptCode thenscriptCode = require("Module:Language/scripts").isLatn(text) and "Latn" or "unknown"endif errorText thenerrorText = ' <span style="font-size: smaller">[' .. errorText .. ']</span>'elseerrorText = ""endreturn languageCode, scriptCode, errorTextendlocal function tag(text, languageCode, script, italics)local data = m_data[languageCode]local italicize = script == "Latn" and italicsif not text then text = "[text?]" endlocal textDirectionMarkers = { "", "", "" }if data and data["direction"] == "rtl" thentextDirectionMarkers = { ' dir="rtl"', '&rlm;', '&lrm;' }endlocal out = { textDirectionMarkers[2] }if italicize thentable.insert(out, "<i lang=\"" .. languageCode .. "\" xml:lang=\"" .. languageCode  .. "\"" .. textDirectionMarkers[1] .. ">" .. text .. "</i>")elsetable.insert(out, "<span lang=\"" .. languageCode .. "\" xml:lang=\"" .. languageCode .. "\"" .. textDirectionMarkers[1] .. ">" .. text .. "</span>")endtable.insert(out, textDirectionMarkers[3])return table.concat(out)endfunction p.lang(frame)local parent = frame:getParent()local args = parent.args[1] and parent.args or frame.argslocal codes = args[1]local text = args[2] or error("Provide text in the second parameter")local languageCode, scriptCode, errorText = getCodes(codes, text)local italics = args.italics or args.iitalics = not (italics == "n" or italics == "-")return tag(text, languageCode, scriptCode, italics) .. errorTextendlocal function linkToWiktionary(entry, linkText, languageCode)local data = m_data[languageCode]local nameif languageCode thenif data thenname = data.nameelseif mw.language.fetchLanguageName(languageCode, 'ur') ~= "" then-- On other languages' wikis, use mw.getContentLanguage():getCode(), or replace with that wiki's language code.name = mw.language.fetchLanguageName(languageCode, 'ur')elseerror("No name for the language " .. (languageCode or "nil") .. " could be found")endif sub(entry, 1, 1) == "*"  thenif name ~= "" thenentry = "Reconstruction:" .. name .. "/" .. sub(entry, 2)elseerror("Language name is empty")endendif entry and linkText thenreturn "[[wikt:" .. entry .. "#" .. name .. "|" .. linkText .. "]]"elseerror("linkToWiktionary needs a Wiktionary entry or link text, or both")endelsereturn "[[wikt:" .. entry .. "|" .. linkText .. "]]"endendfunction p.wiktlang(frame)local parent = frame:getParent()local args = parent.args[1] and parent.args or frame.argslocal codes = args[1] or nillocal word1 = args[2] or nillocal word2 = args[3] or nillocal languageCode, scriptCode, errorText = getCodes(codes, word1)local italics = args.italics or args.iitalics = not (italics == "n" or italics == "-")local entry, linkTextif checkForString(word2) and checkForString(word1) thenentry = makeEntryName(word1, languageCode)linkText = word2elseif checkForString(word1) thenentry = makeEntryName(word1, languageCode)linkText = word1endlocal outif languageCode and entry and linkText thenout = tag(linkToWiktionary(entry, linkText, languageCode), languageCode, scriptCode, italics)elseif entry and linkText thenout = linkToWiktionary(entry, linkText)elseout = '<span style="font-size: smaller;">[text?]</span>'endif out and errorText thenreturn out .. errorTextelsereturn errorText or error("The function wiktlang generated nothing")endendfunction p.wikt(frame)local parent = frame:getParent()local args = parent.args[1] and parent.args or frame.argslocal codes = args[1] or nillocal word1 = args[2] or nillocal word2 = args[3] or nillocal languageCode, scriptCode, errorText = getCodes(codes, word1)local entry, linkTextif checkForString(word2) and checkForString(word1) thenentry = makeEntryName(word1, languageCode)linkText = word2elseif checkForString(word1) thenentry = makeEntryName(word1, languageCode)linkText = word1endlocal outif languageCode and entry and linkText thenout = linkToWiktionary(entry, linkText, languageCode) elseif entry and linkText thenout = linkToWiktionary(entry, linkText)elseout = '<span style="font-size: smaller;">[text?]</span>'endif out and errorText thenreturn out and out .. errorTextelsereturn errorText or error("The function wikt generated nothing")endendreturn p