Module:Language: Difference between revisions

2,279 bytes added ,  2 years ago
m
1 revision imported
m (1 revision imported)
m (1 revision imported)
 
(One intermediate revision by one other user not shown)
Line 1:
require('Module:No globals')
local m_data = mw.loadData("Module:Language/data")
local langData = m_data.languages or m_data
 
local p = {}
 
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local find = mw.ustring.find
local match = mw.ustring.match
local lower = mw.ustring.lower
local upper = mw.ustring.upper
 
local function checkForString(variable)
Line 16 ⟶ 10:
 
local function makeLinkedName(languageCode)
local data = m_datalangData[languageCode]
local article = data["article"]
local name = data["Wikipedia_name"] or data["name"]
Line 23 ⟶ 17:
 
local function makeEntryName(word, languageCode)
local data = m_datalangData[languageCode]
local ugsub = mw.ustring.gsub
word = tostring(word)
if word == nil then
Line 31 ⟶ 26:
else
-- Remove bold and italics, so that words that contain bolding or emphasis can be linked without piping.
word = word:gsub(word, "\'\'\'", "")
word = word:gsub(word, "\'\'", "")
if data == nil then
return word
Line 40 ⟶ 35:
return word
else
-- Decompose so that the diacritics of characters such
for regex, replacement in pairs(replacements) do
-- as á can be removed in one go.
word = gsub(word, regex, replacement)
-- No need to compose at the end, because the MediaWiki software
-- will handle that.
if replacements.decompose then
word = mw.ustring.toNFD(word)
for i, from in ipairs(replacements.from) do
word = ugsub(
word,
from,
replacements.to and replacements.to[i] or "")
end
else
for regex, replacement in pairs(replacements) do
word = ugsub(word, regex, replacement)
end
end
return word
Line 47 ⟶ 56:
end
end
end
 
p.makeEntryName = makeEntryName
 
local function fixScriptCode(firstLetter, threeLetters)
return string.upper(firstLetter) .. string.lower(threeLetters)
end
 
Line 54 ⟶ 69:
if codes == nil or codes == "" then
errorText = 'no language or script code provided'
elseif codes:find(codes, "^%s*%a%a%a?%s*$") or codes:find(codes, "^%s*%a%a%a?%-%a%a%a%a%s*$") then
-- A three- or two-letter lowercase sequence at beginning of first parameter
languageCode =
codes:find(codes, "^%s*%a%a%a?") and (
codes:match(codes, "^%s*(%l%l%l?)")
or gsubcodes:match("^(%a%a%a?)")
match:gsub(codes, "^%s*(%a%a%a?)"), string.lower, 1)
"(%a%a%a?)",
function(a)
return lower(a)
end,
1
)
)
-- One uppercase and three lowercase letters at the end of the first parameter
scriptCode =
codes:find(codes, "%a%a%a%a%s*$") and (
codes:match(codes, "(%u%l%l%l)%s*$")
or gsub(
codes:match(codes, "(%a%a%a%a)%s*$"),
"(%a)(%a%a%a)",
function(afixScriptCode, b)
return upper(a) .. lower(b)
end,
1
)
)
elseif codes:find(codes, "^%s*%a%a%a?%-%a%a%a?$") then
or codes:find("^%a%a%a%-%a%a%a%-%a%a%a$") then
languageCode = 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)
languageCode = codes
elseif find(codes, "^%s*%a%a%a?") then
languageCode, invalidCode = match(codes, "^%s*(%a%a%a?)%-?(.*)")
-- Private-use subtag: x followed by one or more sequences of 1-8 lowercase
languageCode = lower(languageCode)
-- 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?$") then
languageCode, scriptCode =
codes:match("^(%a%a%a%-x%-%a%a?%a?%a?%a?%a?%a?%a?)%-?(.*)$")
if not languageCode then
errorText = '<code>'..codes..'</code> is not a valid language or script code.'
elseif scriptCode ~= "" and not scriptCode:find("%a%a%a%a") then
errorText = '<code>'..scriptCode..'</code> is not a valid script code.'
else
scriptCode = scriptCode:gsub(
"(%a)(%a%a%a)",
fixScriptCode,
1
)
end
elseif codes:find("^%a%a%a?") then
languageCode, invalidCode = codes:match("^(%a%a%a?)%-?(.*)")
languageCode = string.lower(languageCode)
errorText = '<code>'..invalidCode..'</code> is not a valid script code.'
elseif codes:find(codes, "%-?%a%a%a%a%s*$") then
invalidCode, scriptCode = codes:match(codes, "(.*)%-?(%a%a%a%a)%s*$")
scriptCode = gsub(
scriptCode,
"(%a)(%a%a%a)",
fixScriptCode
function(a, b)
return upper(a) .. lower(b)
end
)
errorText = '<code>'..invalidCode..'</code> is not a valid language code.'
Line 100 ⟶ 124:
errorText = '<code>'..codes..'</code> is not a valid language or script code.'
end
if not scriptCode or scriptCode == "" then
scriptCode = require("Module:Language/scriptsUnicode data").isLatnis_Latin(text) and "Latn" or "unknown"
end
if errorText then
Line 108 ⟶ 132:
errorText = ""
end
languageCode = m_data.redirects[languageCode] or languageCode
return languageCode, scriptCode, errorText
end
 
local function tag(text, languageCode, script, italics)
local data = m_datalangData[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 languageCode
local italicize = script == "Latn" and italics
Line 133 ⟶ 162:
return table.concat(out)
end
 
 
 
function p.lang(frame)
Line 138 ⟶ 169:
local args = parent.args[1] and parent.args or frame.args
local 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.italic
italics = not (italics == "n" or italics == "-" or italics == "no")
return tag(text, languageCode, scriptCode, italics) .. errorText
Line 150 ⟶ 181:
 
local function linkToWiktionary(entry, linkText, languageCode)
local data = m_datalangData[languageCode]
local name
if languageCode then
if data and data.name then
name = data.name
elseif mw.language.fetchLanguageName(languageCode, 'en') ~= "" then
Line 159 ⟶ 190:
name = mw.language.fetchLanguageName(languageCode, 'en')
else
error("No name for the language " .. ("%q"):format(languageCode or "nil") .. " could be found")
end
if entry:sub(entry, 1, 1) == "*" then
if name ~= "" then
entry = "Reconstruction:" .. name .. "/" .. entry:sub(entry, 2)
else
error("Language name is empty")
end
elseif data and data.type == "reconstructed" then
mw.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 ~= "" then
entry = "Reconstruction:" .. name .. "/" .. entry
else
error("Language name is empty")
end
elseif data and data.type == "appendix" then
if name ~= "" then
entry = "Appendix:" .. name .. "/" .. entry
else
error("Language name is empty")
Line 182 ⟶ 233:
local args = parent.args[1] and parent.args or frame.args
local codes = args[1] orand nilmw.text.trim(args[1])
local word1 = args[2] or nil
local word2 = args[3] or nil
if not args[2] then
error("Parameter 2 is required")
end
local languageCode, scriptCode, errorText = getCodes(codes, word1)
Line 220 ⟶ 275:
local args = parent.args[1] and parent.args or frame.args
local codes = args[1] orand nilmw.text.trim(args[1])
local word1 = args[2] or nil
local word2 = args[3] or nil
if not word1 then
error("Provide a word in parameter 2.")
end
local languageCode, scriptCode, errorText = getCodes(codes, word1)