Module:Wikt-lang

This is an old revision of this page, as edited by Erutuon (talk | contribs) at 18:59, 1 October 2016 (probably adding the local language code is slightly faster than using a function to determine it). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[ Name is the "canonical name" used on Wiktionary. Article is the Wikipedia article. Script is the ISO 15924 code. ]]

data = {
	["ar"] = {
		["name"] = "Arabic",
		["article"] = "Arabic language",
		["script"] = "Arab",
	},
	["en"] = {
		["name"] = "English",
		["article"] = "English language",
		["script"] = "Latn",
	},
	["grc"] = {
		["name"] = "Ancient Greek",
		["article"] = "Ancient Greek",
		["script"] = "Grek",
	},
	["ru"] = {
		["name"] = "Russian",
		["article"] = "Russian language",
		["script"] = "Cyrl",
	},
}

--[[

	[""] = {
		["name"] = "",
		["article"] = "",
		["script"] = "",
	},

]]

f = {}

local function strip(word)
	local replacements = {
		["́"] = ""
		}
	return string.gsub(word, "́", "")
end

local function languageSpan(languageCode, text)
	languageData = data[languageCode]
	languageScript = languageData["script"]
	if languageScript == "Latn" then
		return "<i lang=\"" .. languageCode .. "\" xml:lang=\"" .. languageCode .. "\">" .. text .. "</i>"
	else
		return "<span lang=\"" .. languageCode .. "\" xml:lang=\"" .. languageCode .. "\">" .. text .. "</span>"
	end
end

function f.lang(frame)
	return languageSpan(frame.args[1], frame.args[2])
end

function wiktionaryLink(languageCode, entry, linkText)
	local languageData, languageName = {}, ""
	if languageCode then
		languageData = data[languageCode]
		if languageData == nil then
			error("Language code is not recognized")
		end
		languageName = mw.language.fetchLanguageName(languageCode, 'en') -- On other languages' wikis, use mw.getContentLanguage():getCode(), or replace with that wiki's language code.
		if entry and linkText then
			return languageSpan(languageCode, "[[wikt:" .. entry .. "#" .. languageName .. "|" .. linkText .. "]]")
		else
			error("wiktionaryLink needs a Wiktionary entry or link text, or both")
		end
	else
		error("wiktionaryLink needs a language code")
	end
end

function f.wikt(frame)
	local languageCode = frame.args[1]
	local word1 = frame.args[2]
	local word2 = frame.args[3]
	if languageCode then
		if word2 and word1 then
			entry = strip(word1)
			linkText = word2
		elseif word1 then
			entry = strip(word1)
			linkText = word1
		else
			error("Please provide a word in the second parameter")
		end
	else
		error("Please provide a language code in the first parameter")
	end
	return wiktionaryLink(languageCode, entry, linkText)
end

return f