Module:translations: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(28 intermediate revisions by 2 users not shown)
Line 2: Line 2:


local m_languages = require("Module:languages")
local m_languages = require("Module:languages")
local m_links = require("Module:links")


function export.show(frame)
function export.show(frame)
---PLEASE DELETE THIS SOON
if frame:getParent().args[2] == nil then return "<big><b>Please now add the entry language code as the first parameter!</b></big>[[Category:Translations to update]]" end
local function sort_function(a, b)
local function sort_function(a, b)
return a.name < b.name
return a.name < b.name
end
end
local tag_missing = {"rad", "lfv", "ryn"}
local translations = {}
local translations = {}
local title
local title
local args = frame:getParent().args
local args = frame:getParent().args
local language = m_languages.get_by_code(args[1])
local title = args[2] or args["title"] or args["t"]
local nocat = args["nocat"] ~= nil or false
if title then title = string.gsub(title, "%s+$", "") end
for k, v in pairs(args) do
for k, v in pairs(args) do
if k == 1 then
if type(k) == "string" and k ~= "nocat" and type(v) == "string" then
title = v
local tr_language = m_languages.get_by_code(k)
elseif type(k) == "string" then
local to_process = v
local language = m_languages.get_by_code(k)
local bytext = ""
table.insert(translations, {name = language.name, display = language.name .. ": " .. v})
-- add links between *asterisks* --
while #to_process > 0 do
if mw.ustring.match(to_process, "%*[^%*]+%*") then
local pre = mw.ustring.match(to_process, "^([^%*]+)%*") or ""
local translink = mw.ustring.match(to_process, "%*([^%*]+)%*")
local translink_alt
to_process = mw.ustring.sub(to_process, mw.ustring.len(pre) + mw.ustring.len(translink) + 3)
if mw.ustring.find(translink, "[^%:]+%:[^%:]+") then
translink_alt, translink = mw.ustring.match(translink, "([^%:]+)%:([^%:]+)")
else
translink_alt = nil
end
translink = m_links.full_link{term = translink, alt = translink_alt, language = tr_language, nobold = true}
bytext = bytext .. pre .. translink
else
bytext = bytext .. to_process
to_process = ""
end
end
table.insert(translations, {name = tr_language.name, display = tr_language.name .. ": " .. bytext})
end
end
end
end
if not title then error("the first parameter provided must be a title for the translations box") end
if not title then error("the first parameter provided must be a title for the translations box") end
table.sort(translations, sort_function)
table.sort(translations, sort_function)
local out = '<div class="mw-collapsible mw-collapsed" data-expandtext="{{int:show}}" data-collapsetext="{{int:hide}}" style="padding:0.2em 0.6em 0.2em 0.6em; border-right:1px solid #BEBEBE; border-bottom:1px dotted black; background:#EDEDED">'
local out = ""
out = out .. "''" .. title .. "''"
if #translations > 0 then
out = out .. '\n<div class="z-index:-1; toccolours mw-collapsible-content" style="border:0;">'
out = '<div class="mw-collapsible mw-collapsed" data-expandtext="{{int:show}}" data-collapsetext="{{int:hide}}" style="padding:0.2em 0.6em 0.2em 0.6em; border-right:1px solid #BEBEBE; border-bottom:1px dotted black; background:#EDEDED">'
out = out .. '\n{| border="0" width="100%" class="translations"'
out = out .. "'''" .. title .. "'''"
out = out .. '\n|-'
out = out .. '\n<div class="z-index:-1; toccolours mw-collapsible-content" style="border:0;">'
out = out .. '\n| bgcolor="beige" valign="top" width="48%" |'
out = out .. '\n{| border="0" width="100%" class="translations"'
for _, n in ipairs(translations) do out = out .. '\n* ' .. n.display end
out = out .. '\n|-'
out = out .. '\n|}'
out = out .. '\n| bgcolor="beige" valign="top" width="48%" |'
out = out .. '\n</div></div>'
for _, n in ipairs(translations) do out = out .. '\n* ' .. n.display .. "[[Category:" .. language.name .. " terms with " .. n.name .. " translations]]" end
out = out .. '\n|}'
out = out .. '\n</div></div>[[Category:' .. language.name .. ' terms with translations]]'
end
for _, lang in ipairs(tag_missing) do
if lang ~= language.code and args[lang] == nil then
out = out .. "[[Category:Terms needing " .. m_languages.get_by_code(lang).name .. " translations]]"
end
end
if nocat then out = string.gsub(out, "%[%[Category[^%]]+%]%]", "") end
return out
return out
end
end


return export
return export

Latest revision as of 19:30, 26 July 2024

Documentation for this module may be created at Module:translations/documentation

local export = {}

local m_languages = require("Module:languages")
local m_links = require("Module:links")

function export.show(frame)
	---PLEASE DELETE THIS SOON
	if frame:getParent().args[2] == nil then return "<big><b>Please now add the entry language code as the first parameter!</b></big>[[Category:Translations to update]]" end
	
	local function sort_function(a, b)
		return a.name < b.name
	end
	local tag_missing = {"rad", "lfv", "ryn"}
	local translations = {}
	local title
	local args = frame:getParent().args
	local language = m_languages.get_by_code(args[1])
	local title = args[2] or args["title"] or args["t"]
	local nocat = args["nocat"] ~= nil or false
	if title then title = string.gsub(title, "%s+$", "") end
	for k, v in pairs(args) do
		if type(k) == "string" and k ~= "nocat" and type(v) == "string" then
			local tr_language = m_languages.get_by_code(k)
			local to_process = v
			local bytext = ""
			-- add links between *asterisks* --
			while #to_process > 0 do
				if mw.ustring.match(to_process, "%*[^%*]+%*") then
					local pre = mw.ustring.match(to_process, "^([^%*]+)%*") or ""
					local translink = mw.ustring.match(to_process, "%*([^%*]+)%*")
					local translink_alt
					
					to_process = mw.ustring.sub(to_process, mw.ustring.len(pre) + mw.ustring.len(translink) + 3)
					
					if mw.ustring.find(translink, "[^%:]+%:[^%:]+") then
						translink_alt, translink = mw.ustring.match(translink, "([^%:]+)%:([^%:]+)")
					else
						translink_alt = nil
					end
					
					translink = m_links.full_link{term = translink, alt = translink_alt, language = tr_language, nobold = true}
					bytext = bytext .. pre .. translink
				else
					bytext = bytext .. to_process
					to_process = ""
				end
			end
			table.insert(translations, {name = tr_language.name, display = tr_language.name .. ": " .. bytext})
		end
	end
	if not title then error("the first parameter provided must be a title for the translations box") end
	table.sort(translations, sort_function)
	local out = ""
	if #translations > 0 then
		out = '<div class="mw-collapsible mw-collapsed" data-expandtext="{{int:show}}" data-collapsetext="{{int:hide}}" style="padding:0.2em 0.6em 0.2em 0.6em; border-right:1px solid #BEBEBE; border-bottom:1px dotted black; background:#EDEDED">'
		out = out .. "'''" .. title .. "'''"
		out = out .. '\n<div class="z-index:-1; toccolours mw-collapsible-content" style="border:0;">'
		out = out .. '\n{| border="0" width="100%" class="translations"'
		out = out .. '\n|-'
		out = out .. '\n| bgcolor="beige" valign="top" width="48%" |'
		for _, n in ipairs(translations) do out = out .. '\n* ' .. n.display .. "[[Category:" .. language.name .. " terms with " .. n.name .. " translations]]" end
		out = out .. '\n|}'
		out = out .. '\n</div></div>[[Category:' .. language.name .. ' terms with translations]]'
	end
	for _, lang in ipairs(tag_missing) do
		if lang ~= language.code and args[lang] == nil then
			out = out .. "[[Category:Terms needing " .. m_languages.get_by_code(lang).name .. " translations]]"
		end
	end
	if nocat then out = string.gsub(out, "%[%[Category[^%]]+%]%]", "") end
	return out
end

return export