Open main menu
Lua
CodeDiscussionEditHistoryLinksLink countNo test API Subpages:DocumentationTestsResultsSandboxLive code All modules

Module:Name is the back end of the several templates related to names used by {{Creator}} template and directly by users:

Internationalization is done through Module:I18n/name.

Code

local p = {}
local i18n = require('Module:I18n/name')

local function langSwitch(list,lang)
	local langList = mw.language.getFallbacksFor(lang)
	table.insert(langList,1,lang)
	table.insert(langList, 'default')
	for i,language in ipairs(langList) do
		if list[language] then
			return list[language]
		end
	end
	return nil
end

function p._name(operation, base_name, lang)
  -- Error checking on "operation" parameter
	if operation == '' then -- no operation provided -> return the name
		return base_name
	end
	operation = mw.ustring.lower(operation) -- convert operation to lower case
	if not i18n[operation] then -- if operation is not recognized than maybe it is an alias
		operation = i18n.Aliases[operation]
	end
	if not i18n[operation] then -- operation is still not recognized
		return "name not supported"
	end
	local colon = tostring(mw.message.new('colon'):inLanguage(lang))
	
	-- translation through Wikidata q-code
	if type(i18n[operation])=='string' then 
		-- usually operation is a translation table, but if it is a string than that is
		-- a wikidata q-code, so look up the label stored in wikidata
		local getLabel = require('Module:Wikidata label')._getLabel
		return getLabel(i18n[operation], lang) .. colon .. base_name
	end
	
	-- translation through local LangSwitch which return language specific function or string
	local full_name = langSwitch(i18n[operation], lang)
	if type(full_name)=='function' then
		full_name = full_name(base_name)
	elseif type(full_name)=='string' then
		--full_name = mw.getLanguage(lang):ucfirst(full_name)
		full_name = mw.getCurrentFrame():callParserFunction( "ucfirst", { full_name } )
		if mw.ustring.find(full_name, '$of_name') then -- forms like Master of X
			base_name = require('Module:Linguistic').of(base_name, lang)
			full_name = mw.ustring.gsub(full_name, '$of_name', base_name)
		elseif mw.ustring.find(full_name, '$name') then -- forms like Master X
			-- replace parts of the string '$name' with base_name strings
			full_name = mw.ustring.gsub(full_name, '$name', base_name)
		else -- forms like Pseudonym: base_name, with a colon
			full_name = full_name .. colon .. base_name
		end
	end
	return full_name
end

function p.name(frame)
	local args = {}
	for name, value in pairs( frame.args ) do 
		if value ~= '' then -- nuke empty strings
			args[name] = value
		end
	end
	if not (args.lang and mw.language.isValidCode(args.lang)) then 
		args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language 
	end
	local base_name = args.name or ''
	-- handle case where there is no "name" parameter but chinese names
	if base_name=='' and (args.trad or args.simp or args.PY) then
		 local Func = langSwitch(i18n.Chinese_name, args.lang)
		 base_name = Func(args.trad or '', args.simp or '', args.PY or '')
	end
	return p._name(args.operation or '', base_name, args.lang)
end

return p