Module:Occupation

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Translation for occupation, based on translation submodules.

Use

[edit]

- function p.occupation(frame)

  • param 1: profession (should be present in either Module:Occcuation/en or Module:Occupation/redirects
  • param 2: gender ("m" or "f"
  • param lang: code of language in which the translation should be displayed, by default it is {{int:lang}}<nowiki> ({{int:lang}}). If there is no translation module exists for the language requested, it will look for the most relevant language using the standard "fallback" list. *<nowiki>{{#invoke:Occupation|occupation|painter}} ->
  • {{#invoke:Occupation|occupation|king}} -> {
  • {{#invoke:Occupation|occupation|queen}} ->
  • {{#invoke:Occupation|occupation|painter|f}} ->
  • {{#invoke:Occupation|occupation|king|f}} ->
  • {{#invoke:Occupation|occupation|queen|f}} ->
  • {{#invoke:Occupation|occupation|painter|f|lang=cs}} ->
  • {{#invoke:Occupation|occupation|king|f|lang=cs}} ->
  • {{#invoke:Occupation|occupation|queen|f|lang=cs}} ->

Supported terms

[edit]

Words in red are supported in the English version of the module but not in the translated version, you can add them. {{#invoke:Occupation|list}}

Words in the translated module, missing in the English version

[edit]

{{#invoke: Occupation|notinenglish}}

Broken redirects

[edit]

{{#invoke: Occupation|brokenredirects}}

Keys used in Module:Occupation/redirects and Module:Occupation/en at the same time

[edit]

keys appearing both in Module:Occupation/redirects and Module:Occupation/en:armourer

Code

local p = {}
local fallback = require('Module:Fallback')
local languages = {'ca', 'cs', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'he', 'hu', 'it', 'ja', 'ko', 'mk', 'ml', 'nds', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'sv', 'sw', 'uk', 'vi', 'yo', 'zh-hans', 'zh-hant'}

local function badoccupation(occupation) -- error handling when the occupation is not a key in the translation table
	-- check if it exists in English, if not add a maintenance category, else, maintenance category is useless
	local en = require('Module:Occupation/en')[occupation]
	local maintenancestring = ''
	if (not en) and (occupation ~= '') then
		maintenancestring = '[[Category:Pages with incorrect template usage/Occupation|' .. occupation .. ']]'
	end
	return tostring(mw.html.create('span'):css({['color'] = 'red'}):wikitext(occupation):done()) .. maintenancestring
end

local function findlang(lang) -- return the most approriate translation page
	if not lang then return
		error('no language provided in Module:Occupation')
	end
	return fallback.fallbackpage('Module:Occupation', lang)
end

local function findsex(gender)
	if gender == 'f' or gender == 'female' or gender == 'woman' then
		return 'f'
	end
	return 'm'
end

local function findword(val, gender) -- retrieves the correct translation for a given gender, once the table value is given
	if type(val) == 'string' then
		return val
	elseif type(val) == 'table' then
		local gender = findsex(gender)
		if gender == 'f' then -- second value if exists, else first value 
			return val[2] or val[1] 
		else
			return val[1]
		end
	end
	return error('bad data structure in Module:Occupation')
end

function p._occupation(occupation, lang, gender) -- main function to return the translation of a particular occupation
	local localmodule = require(findlang(lang))
	local val = localmodule[occupation] or localmodule[require('Module:Occupation/redirects')[occupation]]
	if not val then
		return badoccupation(occupation)
	end
	return findword(val, gender)
end

function p.occupation(frame)
	return p._occupation(frame.args[1], frame.args.lang or frame:preprocess('{{int:lang}}'), frame.args[2])
end

---------------------------------------------------------------------------
--[[ 
Maintenance functions
]]--


function p.list(frame)
	local enmodule = require('Module:Occupation/en')
	local sortedenglish = {} -- so that values are sorted alphabetically
	for i in pairs(enmodule) do
		table.insert(sortedenglish, i)
	end
	table.sort(sortedenglish)
	
	local lang = frame.args.lang or frame:preprocess('{{int:lang}}')
	local localmodulename = findlang(lang)
	local localmodule = require(localmodulename)
	
	local header = ''
	local wikidata = require('Module:Occupation/Wikidata')
	if localmodulename ~= 'Module:Occupation/en' then 
		header = require('Module:LangSwitch')._langSwitch({
		en = 'Values in red are those that do not have any translation in ([[' .. localmodulename .. ']]). ',
		fr = 'Les valeurs en rouge correspondent à des manques dans ([[' .. localmodulename .. ']]). ',
		}, lang)
	end
	local obj = mw.html.create('table')
		:tag('tr')
			:tag('td')
				:wikitext('<b>key</b>')
				:done()
			:tag('td')
				:wikitext('<b>male</b>')
				:done()
			:tag('td')
				:wikitext('<b>female</b>')
				:done()
			:tag('td')
				:wikitext('<b>Wikidata</b>')
				:done()
			:done()
	for i, j in pairs(sortedenglish) do
		local datalink = ''
		if wikidata[j] and wikidata[j] ~= '' then
			datalink = '[[:d:' .. wikidata[j] .. '|' .. wikidata[j] .. ']]'	
		end
		local line = mw.html.create('tr')
			:tag('td')
				:wikitext(j)
				:done()
			:tag('td')
				:wikitext(p._occupation(j, lang, 'm'))
				:done()
			:tag('td')
				:wikitext(p._occupation(j, lang, 'f'))
				:done()
			:tag('td')
				:wikitext(datalink)
				:done()
			:done()
		obj:node(line)
	end
	return header .. tostring(obj)
end

function p.brokenredirects(frame)
	local redirectpage = require('Module:Occupation/redirects')
	local text = 'broken redirects: '
	local list = {}
	for i, j in pairs(redirectpage) do
		if not require('Module:Occupation/en')[j] then
			table.insert(list, j)
		end
	end
	local strlist = ''
	if #list > 0 then 
		strlist = ': ' .. mw.text.listToText(list)
	end
	return tostring(#list) .. ' broken redirects from [[Module:Occupation/redirects]] to [[Module:Occupation/en]]' .. strlist .. '.'
end

function p.notinenglish(frame)
	local lang = frame.args.lang or frame:preprocess('{{int:lang}}')
	local localmodulename = findlang(lang)
	local langmodule = require(localmodulename)
	local engmodule = require('Module:Occupation/en')

	local text = require('Module:Fallback')._langSwitch({
		en = 'Pages found in local language modules but not in [[Module:Occupation/en]]: ',
		fr = 'Pages trouvées dans des modules en langue local, mais pas dans [[Module:Occupation/en]]: ',
		}, lang) .. '<br />'
	
	for i, j in pairs(languages) do
		local currentmodule = require('Module:Occupation/' .. j)
		local header = '[[' .. 'Module:Occupation/' .. j .. ']]: '
		local list = ''
		for k, l in pairs(currentmodule) do
			if not engmodule[k] then
				list = list .. ', ' .. k
			end
		end
		if list == '' then 
			text = text .. header .. '(none)' .. '<br />'
		else
			text = text .. header .. list .. '<br />'
		end
	end
	return text
end

function p.duplicatedredirects(frame)
	local redirects = require('Module:Occupation/redirects')
	local english = require('Module:Occupation/en')
	local header = 'keys appearing both in [[Module:Occupation/redirects]] and [[Module:Occupation/en]]:'
	local list = ''
	for i in pairs(redirects) do
		if english[i] then 
			list = list .. i
		end
	end
	return header .. list
end
return p