ਮੌਡਿਊਲ:Language

This module is used by {{wikt-lang}} and {{wt}}. It is inspired by the templates {{m}} and {{l}} and their associated modules on Wiktionary. It has a Wiktionary link function that links to the correct section of the Wiktionary entry, and applies correct language formatting and italics. The language-tagging function does most of what {{lang}} does, except that italics can't be customized and categories aren't added.

The module uses Module:Language/data to retrieve the language name for a language code, and to perform the necessary entry-name replacements (for instance, removing macrons from Latin entry names). These are unfortunately not automatically retrieved from Wiktionary's language data modules. For language codes that do not have a name value in this module, the language name is fetched with mw.language.fetchLanguage. When mw.language.fetchLanguage does not fetch the correct language name (or any language name at all), please add it to Module:language/data, and similarly when the correct entry name is not generated, please add the entry name replacements to the module.

Examples

* {{#invoke:Language|wikt|en|language}}* {{#invoke:Language|wikt|en|language|languages}}* {{#invoke:Language|wikt|fr|bourguignon}}* {{#invoke:Language|wikt|la|homō}}* {{#invoke:Language|wikt|grc|δημοκρατίᾱ}}* {{#invoke:Language|wikt|ru|язы́к}}* {{#invoke:Language|wikt|ar|العَرَبِيَّة}}* {{#invoke:Language|wikt|fa|فارسی}}* {{#invoke:Language|wikt|ine-x-proto|*h₂enǵʰ-}}

Invalid codes

* {{#invoke:Language|wikt|EN|language}}* {{#invoke:Language|wikt|En|language|languages}}* {{#invoke:Language|wikt|La|homō}}* {{#invoke:Language|wikt|Grc|δημοκρατίᾱ}}* {{#invoke:Language|wikt|Ru|язы́к}}* {{#invoke:Language|wikt|Ar|العَرَبِيَّة}}* {{#invoke:Language|wikt|Fa|فارسی}}

Errors

* {{#invoke:Language|wikt|en-Latin|language}}
  • language [Latin is not a valid script code.]

Tracking categories

  • Category:Language module errors (0)
  • Category:Language module reconstructed with no asterisk (0)
  • Category:Language module using redirect code (0)

require('strict')local m_data = mw.loadData("Module:Language/data")local langData = m_data.languages or m_datalocal p = {}local function ifNotEmpty(value)if value == "" thenreturn nilelsereturn valueendendlocal function makeLinkedName(languageCode)local data = langData[languageCode]local article = data["article"]local name = data["Wikipedia_name"] or data["name"]return "[[" .. article .. "|" .. name .. "]]:&nbsp;"endlocal function makeEntryName(word, languageCode)local data = langData[languageCode]local ugsub = mw.ustring.gsubword = 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 = word:gsub("\'\'\'", "")word = word:gsub("\'\'", "")if data == nil thenreturn wordelselocal replacements = data and data["replacements"]if replacements == nil thenreturn wordelse-- Decompose so that the diacritics of characters such-- as á can be removed in one go.-- No need to compose at the end, because the MediaWiki software-- will handle that.if replacements.decompose thenword = mw.ustring.toNFD(word)for i, from in ipairs(replacements.from) doword = ugsub(word,from,replacements.to and replacements.to[i] or "")endelsefor regex, replacement in pairs(replacements) doword = ugsub(word, regex, replacement)endendreturn wordendendendendp.makeEntryName = makeEntryNamelocal function fixScriptCode(firstLetter, threeLetters)return string.upper(firstLetter) .. string.lower(threeLetters)endlocal function getCodes(codes, text)local languageCode, scriptCode, invalidCodelocal errorTextif codes == nil or codes == "" thenerrorText = 'no language or script code provided'elseif codes:find("^%a%a%a?$") or codes:find("^%a%a%a?%-%a%a%a%a$") then-- A three- or two-letter lowercase sequence at beginning of first parameterlanguageCode =codes:find("^%a%a%a?") and (codes:match("^(%l%l%l?)")or codes:match("^(%a%a%a?)"):gsub("(%a%a%a?)", string.lower, 1))-- One uppercase and three lowercase letters at the end of the first parameterscriptCode =codes:find("%a%a%a%a$") and (codes:match("(%u%l%l%l)$")or gsub(codes:match("(%a%a%a%a)$"),"(%a)(%a%a%a)",fixScriptCode,1))elseif codes:find("^%a%a%a?%-%a%a%a?$")or codes:find("^%a%a%a%-%a%a%a%-%a%a%a$") thenlanguageCode = codes-- Private-use subtag: x followed by one or more sequences of 1-8 lowercase-- letters separated by hyphens. This only allows for one sequence, as it is-- needed for proto-languages such as ine-x-proto (Proto-Indo-European).elseif codes:find("^%a%a%a?%-x%-%a%a?%a?%a?%a?%a?%a?%a?$") thenlanguageCode, scriptCode =codes:match("^(%a%a%a%-x%-%a%a?%a?%a?%a?%a?%a?%a?)%-?(.*)$")if not languageCode thenerrorText = '<code>'..codes..'</code> is not a valid language or script code.'elseif scriptCode ~= "" and not scriptCode:find("%a%a%a%a") thenerrorText = '<code>'..scriptCode..'</code> is not a valid script code.'elsescriptCode = scriptCode:gsub("(%a)(%a%a%a)",fixScriptCode,1)endelseif codes:find("^%a%a%a?") thenlanguageCode, invalidCode = codes:match("^(%a%a%a?)%-?(.*)")languageCode = string.lower(languageCode)errorText = '<code>'..invalidCode..'</code> is not a valid script code.'elseif codes:find("%-?%a%a%a%a$") theninvalidCode, scriptCode = codes:match("(.*)%-?(%a%a%a%a)$")scriptCode = gsub(scriptCode,"(%a)(%a%a%a)",fixScriptCode)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 or scriptCode == "" thenscriptCode = require("Module:Unicode data").is_Latin(text) and "Latn" or "unknown"endif errorText thenerrorText = ' <span style="font-size: smaller">[' .. errorText .. ']</span>'elseerrorText = ""endlanguageCode = m_data.redirects[languageCode] or languageCodereturn languageCode, scriptCode, errorTextendlocal function tag(text, languageCode, script, italics)local data = langData[languageCode]-- Use Wikipedia code if it has been given: for instance,-- Proto-Indo-European has the Wiktionary code "ine-pro" but the Wikipedia-- code "ine-x-proto".languageCode = data and data.Wikipedia_code or languageCodelocal 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 .. "\"" .. textDirectionMarkers[1] .. ">" .. text .. "</i>")elsetable.insert(out, "<span 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] and mw.text.trim(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.i or args.italicitalics = not (italics == "n" or italics == "-" or italics == "no")return tag(text, languageCode, scriptCode, italics) .. errorTextendlocal function linkToWiktionary(entry, linkText, languageCode)local data = langData[languageCode]local nameif languageCode thenif data and data.name thenname = data.nameelse-- On other languages' wikis, use mw.getContentLanguage():getCode(),-- or replace 'en' with that wiki's language code.name = mw.language.fetchLanguageName(languageCode, 'en')if name == "" thenerror("Name for the language code " .. ("%q"):format(languageCode or nil).. " could not be retrieved with mw.language.fetchLanguageName, ".. "so it should be added to [[Module:Language/data]]")endendif entry:sub(1, 1) == "*" thenif name ~= "" thenentry = "Reconstruction:" .. name .. "/" .. entry:sub(2)elseerror("Language name is empty")endelseif data and data.type == "reconstructed" thenmw.log("Reconstructed language without asterisk:", languageCode, name, entry)local frame = mw.getCurrentFrame()-- Track reconstructed entries with no asterisk by transcluding-- a nonexistent template. This technique is used in Wiktionary:-- see [[wikt:Module:debug]].-- [[Special:WhatLinksHere/tracking/wikt-lang/reconstructed with no asterisk]]pcall(frame.expandTemplate, frame,{ title = 'tracking/wikt-lang/reconstructed with no asterisk' })if name ~= "" thenentry = "Reconstruction:" .. name .. "/" .. entryelseerror("Language name is empty")endelseif data and data.type == "appendix" thenif name ~= "" thenentry = "Appendix:" .. name .. "/" .. entryelseerror("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] and mw.text.trim(args[1])local word1 = ifNotEmpty(args[2])local word2 = ifNotEmpty(args[3])if not args[2] thenerror("Parameter 2 is required")endlocal languageCode, scriptCode, errorText = getCodes(codes, word2 or word1)local italics = args.italics or args.i or args.italicitalics = not (italics == "n" or italics == "-" or italics == "no")local entry, linkTextif word2 and word1 thenentry = makeEntryName(word1, languageCode)linkText = word2elseif 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] and mw.text.trim(args[1])local word1 = ifNotEmpty(args[2])local word2 = ifNotEmpty(args[3])if not word1 thenerror("Provide a word in parameter 2.")endlocal languageCode, scriptCode, errorText = getCodes(codes, word1)local entry, linkTextif word2 and word1 thenentry = makeEntryName(word1, languageCode)linkText = word2elseif 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
🔥 Top keywords: ਮੁੱਖ ਸਫ਼ਾਪੰਜਾਬ ਦੇ ਮੇਲੇ ਅਤੇ ਤਿਓੁਹਾਰਪੰਜਾਬੀ ਲੋਕ ਖੇਡਾਂਪੰਜਾਬੀ ਸੱਭਿਆਚਾਰਪੰਜਾਬ ਦੇ ਲੋਕ-ਨਾਚਭਾਈ ਵੀਰ ਸਿੰਘਪੰਜਾਬੀ ਕੱਪੜੇਗੁਰੂ ਨਾਨਕਸੁਰਜੀਤ ਪਾਤਰਖ਼ਾਸ:ਖੋਜੋਅੰਮ੍ਰਿਤਾ ਪ੍ਰੀਤਮਪੰਜਾਬ ਦੀਆਂ ਵਿਰਾਸਤੀ ਖੇਡਾਂਵਿਆਹ ਦੀਆਂ ਰਸਮਾਂਪੰਜਾਬੀ ਤਿਓਹਾਰਵਿਸਾਖੀਪੰਜਾਬੀ ਭਾਸ਼ਾਗੁਰੂ ਹਰਿਗੋਬਿੰਦਗੁਰੂ ਅਰਜਨਹਰਿਮੰਦਰ ਸਾਹਿਬਭਗਤ ਸਿੰਘਪੰਜਾਬੀ ਭੋਜਨ ਸੱਭਿਆਚਾਰਪੰਜਾਬ, ਭਾਰਤਛਪਾਰ ਦਾ ਮੇਲਾਪੰਜਾਬੀ ਰੀਤੀ ਰਿਵਾਜਗੁਰੂ ਅਮਰਦਾਸਹੇਮਕੁੰਟ ਸਾਹਿਬਵਹਿਮ ਭਰਮਗੁਰੂ ਗੋਬਿੰਦ ਸਿੰਘਗੁਰੂ ਤੇਗ ਬਹਾਦਰਪੰਜਾਬੀ ਲੋਕ ਬੋਲੀਆਂਜਪੁਜੀ ਸਾਹਿਬਗੁਰੂ ਅੰਗਦਗੁਰੂ ਗ੍ਰੰਥ ਸਾਹਿਬਸ਼ਿਵ ਕੁਮਾਰ ਬਟਾਲਵੀਪੰਜਾਬੀ ਮੁਹਾਵਰੇ ਅਤੇ ਅਖਾਣਭੰਗੜਾ (ਨਾਚ)ਪੰਜਾਬੀ ਲਈ ਸਾਹਿਤ ਅਕਾਦਮੀ ਇਨਾਮ ਜੇਤੂਆਂ ਦੀ ਸੂਚੀਰਣਜੀਤ ਸਿੰਘਦਿਵਾਲੀ