Module:Languages

From Dead by Daylight Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

Language Lookup Table

Language name Language code
Catalan ca
Czech cs
Danish da
Greek el
English en
Finnish fi
Croatian hr
Hungarian hu
Japanese ja
Korean ko
Mongolian mn
Norwegian no
Romanian ro
Russian ru
Slovak sk
Swedish sv
Swahili sw
Thai th
Turkish tr
Ukranian uk
Vietnamese vi
Zhuang za

_language = "en" --valid values are in the list "officialLanguages"

local p = {}
local frame = mw.getCurrentFrame()
local stringsM = require("Module:Strings")
p.langSpecified = false
p.pageName = nil
p.unofficialLang = cstr.empty --Used for redirecting modules


p.languages = {
	{iso = "ca", name = "Catalan"},
	{iso = "cs", name = "Czech"},
	{iso = "da", name = "Danish"},
	{iso = "el", name = "Greek"},
	{iso = "en", name = "English"},
	{iso = "fi", name = "Finnish"},
	{iso = "hr", name = "Croatian"},
	{iso = "hu", name = "Hungarian"},
	{iso = "ja", name = "Japanese", active = false},
	{iso = "ko", name = "Korean"},
	{iso = "mn", name = "Mongolian"},
	{iso = "no", name = "Norwegian"},
	{iso = "ro", name = "Romanian"},
	{iso = "ru", name = "Russian"},
	{iso = "sk", name = "Slovak"},
	{iso = "sv", name = "Swedish"},
	{iso = "sw", name = "Swahili"},
	{iso = "th", name = "Thai"},
	{iso = "tr", name = "Turkish"},
	{iso = "uk", name = "Ukrainian"},
	{iso = "vi", name = "Vietnamese"},
	{iso = "za", name = "Zhuang"}
}

p.officialLanguages = {
	{iso = "en", name = "English"},
	{iso = "de", name = "German"},
	{iso = "fr", name = "French"},
	{iso = "es", name = "Spanish"},
	{iso = "it", name = "Italian"},
	{iso = "pl", name = "Polish"},
	{iso = "pt", name = "Portuguese"},
	{iso = "zh", name = "Chinese"},
}
local officialLanguages = p.officialLanguages

function p.nonEn()
	return _language ~= "en"
end

function p.getLang()
	return _language
end

--if page name contains language suffix then the language code is used for module redirection to language alteration, ex: Module:Datatable => Module:Datatable/ja
function p.lang()
	specifyUnofficialLanguage()
	return ((p.unofficialLang ~= cstr.empty and "/") or cstr.empty) .. p.unofficialLang
end

translatedPages = {
	ja = { --these work as an example, the pages are already deleted
		"ドワイト・フェアフィールド", "メグ・トーマス", "ジェイク・パーク", "ネア・カールソン", "ローリー・ストロード", "エース・ヴィスコンティ", "フェン・ミン", "デイビッド・キング", "クエンティン・スミス", "デイビッド・タップ刑事", "ケイト・デンソン", "アダム・フランシス", "ジェーン・ロメロ", "ナンシー・ウィーラー", "スティーブ・ハリントン", "木村結衣", "ザリーナ・カッシル", "シェリル・メイソン", "フェリックス・リクター"
	}
}

function specifyUnofficialLanguage()
	require("Module:Extensions")
	if not p.langSpecified then 
		p.pageName = p.pageName or mw.title.getCurrentTitle().text
		local pageNameParts = string.split(p.pageName, '/') -- remove page suffixes such as language codes
		if #pageNameParts > 1 then
			for _, lang in ipairs(p.languages) do
				if pageNameParts[#pageNameParts] == lang.iso then
					p.langSpecified = true
					p.unofficialLang = pageNameParts[#pageNameParts]
				end
			end
		else
			
			local activeUnofficialLanguages = {}
			for _, language in ipairs(p.languages) do
				if language.active then
					table.add(activeUnofficialLanguages, language)
				end
			end
			
			--[[
			for _, lang in ipairs(activeUnofficialLanguages) do
				for _, page in ipairs(mw.ext.cargo.query('_pageData', '_pageName',{
					    where='_categories HOLDS "' .. lang.name .. ' Translation"',
					    limit=5000
					})) do
				    
				    if (page._pageName == p.pageName) then
				    	p.langSpecified = true
						p.unofficialLang = lang.iso
			    	end
				end
			end
			]]
		
			--temporary logic, until the issue with lua use time is resolved
			for _, lang in ipairs(activeUnofficialLanguages) do
				if translatedPages[lang.iso] and #translatedPages[lang.iso] > 0 then
					for _, page in ipairs(translatedPages[lang.iso]) do
						if (page == p.pageName) then
					    	p.langSpecified = true
							p.unofficialLang = lang.iso
				    	end
					end
				end
			end
		end
	end
end

local articleFunctions = {
	{}
}

function p.resolveLanguageTable()
	local result = cstr.empty
	local pageTitle = mw.title.getCurrentTitle().fullText
	--mw.log(pageTitle)
	
	result = result .. '<div class="langBox">'
	result = result .. "In other (unofficial) languages: "
	
	--TODO: detect prefix namespace (such as "Module:")
	--mw.log(mw.dumpObject(mw.site.namespaces))
	
	local i = 1
	local langTitle
	local foundLanguages = 0
	for _, lang in ipairs(p.languages) do
		langTitle = pageTitle .. "/" .. lang.iso
		
		if mw.title.new(langTitle).exists then
			mw.log(langTitle)
			result = result .. file("Language" .. lang.name .. dot .. cstr.png, "link=" .. langTitle, "24px")
			foundLanguages = foundLanguages + 1
		end
	end
	
	result = result .. "</div><br>"
	
	if foundLanguages > 0 then
		mw.log(result)
		return result
	end
	return nil
end

--spaceless => whether it's supposed to be space after the article - for now it's only applicable for english, can be changed over time
function p.evaluateArticle(alter, indefinite, spaceless)
	--if _language and _language ~= "en" and not indefinite then
	for _, lang in ipairs(p.officialLanguages) do
		if _language == lang.iso then
			return (p["article_" .. lang.iso] and p["article_" .. lang.iso](alter, indefinite or false, spaceless or false)) or p.article_notImplemented(alter, true) --calling function based on the langauge code
		end
	end
end

--params.fullName - true/false
--params.firstname - string
--params.link - true/false
function p.evaluatePossessive(alter, params)
	for _, lang in ipairs(p.officialLanguages) do
		if _language == lang.iso then
			return (p["possessive_" .. lang.iso] and p["possessive_" .. lang.iso](alter, params)) or p.possessive_notImplemented(alter) --calling function based on the langauge code
		end
	end
end

function p.evaluatePronoun(obj, params)
	for _, lang in ipairs(p.officialLanguages) do
		if _language == lang.iso then
			return (p["pronoun_" .. lang.iso] and p["pronoun_" .. lang.iso](obj, params)) or p.pronoun_notImplemented(obj) --calling function based on the langauge code
		end
	end
end

function p.evaluateDative(alter, params)
	if _language and _language ~= "en" then
		for _, lang in ipairs(p.officialLanguages) do
			if _language == lang.iso then
				return	(p["dative_" .. lang.iso] and p["dative_" .. lang.iso](alter, params)) or p.dative_notImplemented(alter, params) --calling function based on the langauge code
			end
		end
	end
	return nil
end

function p.article_notImplemented(alter)
	if type(alter) == types.table and alter.article then --parameter is table that should be containing article field
		return alter.article .. space
	elseif type(alter) == types.string then --forced form as a string parameter
		return alter .. space
	else --if no parameter passed at all, pass the default, English article
		return p.article_en()
	end
end

function p.possessive_notImplemented(alter, params)
	return "Possessive not implemented"
end

function p.dative_notImplemented(alter, params)
	return cstr.empty
end

function p.pronoun_notImplemented(obj)
	return cstr.empty
end

-----------------------------
--          /* EN */       --
-----------------------------
function p.article_en(alter, indefinite, spaceless)
	if indefinite then
		if string.find(string.lower(alter.name or alter), "^[aeio]") --hideous lua patterns
		or string.find(string.lower(alter.name or alter), "^un")
		or string.find(string.lower(alter.name or alter), "^ul")
		then
			return "an "
		else
			return "a "
		end
	else
		return (alter.article == false and cstr.empty) or cstr.the .. ((not p.nonEn() and not spaceless and space) or cstr.empty)
	end
	return cstr.empty
end

function p.possessive_en(alter, params)
	local name = (not params.fullName and params.firstName) or alter.name
	if alter.possessive == true then
		name = name .. "'" --if it's true then just add the apostrof to the end
	elseif alter.possessive then --it's not true nor false nor empty, hence it should be string (not checked)
		name = alter.possessive
	else
		name = name .. "'s" --if nothing then simply add 's after first name
	end
	
	return name	
end

p.pronouns_en = {
	["Woman"] = "her",
	["Man"] = "his",
	["Not applicable (not human)"] = "its",
	["multiChars"] = "their", --flag multiChars present, default if none other is applicable
	["B"] = "their", --Both
	["F/M"] = "their", --Female/Male
	["M/F"] = "their", --Male/Female
}
function p.pronoun_en(obj)
	local result = cstr.empty
	if type(obj) == types.table then
		result = (obj.multiChars and p["pronouns_" .. _language]["multiChars"]) or p["pronouns_" .. _language][obj.gender] or p["pronouns_" .. _language]["multiChars"]
	end
	return result
end

function p.dative_en(obj)
	return the(obj)
end

-----------------------------
--          /* ES */       --
-----------------------------

--"El" = used for masculine pronouns
--"La" = used for feminine pronouns
--"Los" = used for plural masculine nouns
--"Las" = used for plural feminine nouns
--"Lo" = used for neutral pronouns.
--function p.article_es(alter, indefinite)
--	if indefinite then
--		local result = (type(alter) == types.table and alter.articleIndef or alter.articleIndefinite or alter.a) or type(alter) == types.string and p.article_fr_map_indefinite[alter] or cstr.empty
--		return result
--	end
--	result = (type(alter) == types.table and alter.article or (alter.name and p.article_fr_map_definite[alter.name])) or type(alter) == types.string and p.article_fr_map_definite[alter] or cstr.empty
--	return result .. ((not result:match("L'") and space) or cstr.empty)
--end


-----------------------------
--          /* DE */       --
-----------------------------

p.article_de_map = {
	["Überlebenden"] = "Die" .. space,
	["Killer"] = "Die" .. space
}

function p.article_de(alter, indefinite)
	if type(alter) == types.string then
		for word, mapping in pairs(p.article_de_map) do
			if word == alter then
				return mapping
			end
		end
	end
	return cstr.empty
end


-----------------------------
--          /* FR */       --
-----------------------------

--"Le" = used for masculine nouns
--"La" = used for feminine nouns
--"Les" = used for plural nouns
--"L' " = used for nouns starting with vowels
p.article_fr_map_definite = {
	["Tueurs"] = "Les" .. space,
	["Survivants"] = "Les" .. space,
	["Lampes de Poche"] = "Les" .. space,
	["Clés"] = "Les" .. space,
	["Cartes"] = "Les" .. space,
	["Trousses de Soins"] = "Les" .. space,
	["Boîtes à Outils"] = "Les" .. space,
}
p.article_fr_map_indefinite = {
	["Compétence"] = "une" .. space,
	["Accessoire"] = "un" .. space,
	["Objet"]  = "un" .. space,
	["Offrande"] = "une" .. space,
	["Don"] = "un" .. space,
}
function p.article_fr(alter, indefinite)
	if indefinite then
		local result = (type(alter) == types.table and alter.articleIndef or alter.articleIndefinite or alter.a) or type(alter) == types.string and p.article_fr_map_indefinite[alter] or cstr.empty
		return result
	end
	local result = (type(alter) == types.table and alter.article or (alter.name and p.article_fr_map_definite[alter.name])) or type(alter) == types.string and p.article_fr_map_definite[alter] or cstr.empty
	return result .. ((not result:match("L'") and space) or cstr.empty)
end

--"de + le" = "du"
--"de + la" = "de la" (no merging here)
--"de + les" = "des"
--"de + l' " = (no merging here)
function p.possessive_fr(alter, params)
	local article = cstr.empty
	if type(alter) == types.table then --parameter is table that should be containing article field
		article = alter.article
	elseif type(alter) == types.string then --forced form as a string parameter
		return alter
	end
	
	local result =
		(article == "Le" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "du " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "La" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "de " .. (params and params.link == false and "'''" or cstr.empty) .. "La ") or
		(article == "Les"and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "des " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "L'" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "de " .. (params and params.link == false and "'''" or cstr.empty) .. "L\'") or
		cstr.empty
	return result .. params.firstName
end

--"à + le" = "au"                    // Example: "qui appartient au Cénobite" (belonging to The Cenobite)
--"à + la" = "à la" (no contraction) // Example: "qui appartient à la Marchande de Crâne" (belonging to The Skull Merchant)
--"à + les" = "aux"                  // Example: "qui appartient aux Jumeaux" (belonging to The Twins)
--"à + l'" = "à l'" (no contraction) // Example: "qui appartient à l'[no space]Infirmière" (belonging to The Nurse)
p.dative_fr_map = {
	["Tueurs"] = "aux ",
	["Survivants"] = "aux ",
}
function p.dative_fr(alter, params)
	local article = cstr.empty
	if type(alter) == types.table then --parameter is table that should be containing article field
		article = alter.article
	elseif type(alter) == types.string then --forced form as a string parameter
		return p.dative_fr_map[alter]
	end

	local result = 
		(alter and (not alter.power and not alter.killer) and "à ") or --for survivors
		(article == "Le" and "au ") or
		(article == "La" and "à La ") or
		(article == "Les"and "aux ") or
		(article == "L'" and "à L'") or
		cstr.empty
	return result
end

p.gender_fr_map = {
	["Offrande"] = "F",
	["Objet"] = "M",
	["Compétence"] = "F",
	["Accessoire"] = "M",
	["Don"] = "M",
}
function p.genderEvaluation_fr(mappingWord, obj, objFName)
	result = cstr.empty
	if type(obj) == types.table and obj.name then 
		result = obj.name
	elseif type(obj) == types.string then
		result = obj
	end
	if p.gender_fr_map[mappingWord] and p.gender_fr_map[mappingWord] == 'F' then
		result = objFName or obj.fName
	end
	return result
end
-----------------------------
--          /* IT */       --
-----------------------------

--"Il" = used for masculine nouns
--"Lo" = used for masculine nouns starting with Z, S+consonant, GN, PS, PN, X and Y
--"La" = used for feminine nouns
--"I" = used for plural nouns
--"L' " = used for nouns starting with vowels
function p.article_it(alter, indefinite)
	local result = (type(alter) == types.table and alter.article) --[[or (type(alter) == types.string and alter)]] or cstr.empty
	return result .. ((not result:match("L'") and space) or cstr.empty)
end

-- De + Il = "del"
-- Del + Lo = "dello"
-- Del + La = "della"
-- De + i = "dei"
-- Del + L' = "dell'
-- no article = 'di'
function p.possessive_it(alter, params)
	local article = cstr.empty
	if type(alter) == types.table then --parameter is table that should be containing article field
		article = alter.article
	elseif type(alter) == types.string then --forced form as a string parameter
		return alter
	end
	
	local result =
		(not article	 and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "di " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "Il" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "del " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "Lo" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "dello " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "La" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "della " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "I"  and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "dei " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "L'" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "dell'" .. (params and params.link == false and "'''" or cstr.empty)) or
		cstr.empty
		
	return result .. params.firstName
end

-----------------------------
--          /* PL */       --
-----------------------------
function p.article_pl(alter, indefinite)
	return cstr.empty --PL doesn't have articles so we return empty string
end

-- Possessive endings
-- si, zi, ci <= if word ends in "ś, ź, ć" respectively
-- ąt <= if word ends in "ęta" or "ęt"
-- ki/gi <= if word ends in "ka" or "ga"
------------
-- general rule: If word ends in a vowel:”a, e, o, u, y” delete the vowel
-- if it ends with "i" don't add anything (before or after deleting the vowel)
-- if it ends with consonant or you deleted the vowel add:
-- ”a” for living, masculine nouns
-- “u” for non-living, masculine nouns
-- “y” for living, feminine nouns
-- "i" for non-living, feminine nouns
----------
-- if bool englishSpelling = true then
-- don't delete vowel but add "'" and then add correct ending
-- if it ends in consonant don't add anything
function p.possessive_pl(alter, params)
		require("Module:Extensions")
		local result = {}
		local resultWords = (params and not params.fullName and {params.firstName}) or (string.split((type(alter) == types.table and alter.name) or (type(alter) == types.string and alter) or cstr.empty, space))
		local gramGender = alter.gramGender or alter.gender or "M"
		local gramLiving = alter.gramLiving or false

		for i, nameWord in ipairs(resultWords) do
			local modified = nameWord
			
			--if alter.possessive contains list of numbers then only words with respective number in their order are processed
			if alter and (type(alter.possessive) ~= types.table or (type(alter.possessive) == types.table and table.contains(alter.possessive, i))) then
				local quotesRegex = '^"(.+)"$'
				local hasQuotes = string.find(modified, quotesRegex)
				if hasQuotes then
					modified = modified:match(quotesRegex)
				end
				
				local set1_pos1 = "^(.+)ś$"
				local set1_pos2 = "^(.+)ź$"
				local set1_pos3 = "^(.+)ć$"
				
				local set2_pos1 = "^(.+)ęta$"
				local set2_pos2 = "^(.+)ęt$"
				
				local set3_pos1 = "^(.+)ka$"
				local set3_pos2 = "^(.+)ga$"
				
				local set4_pos1 = "^(.+)[aeyou]$"
				local set4_pos1_sub1 = "^(.+)[^i]$"
				
				local set5_pos1 = "^(.+)[^aeiyouóęą.]$" --except all wovels = all consontants
				
				if string.find(string.lower(modified), set1_pos1) then
					modified = modified:match(set1_pos1) .. "si"
				elseif string.find(string.lower(modified), set1_pos2) then
					modified = modified:match(set1_pos2) .. "zi"
				elseif string.find(string.lower(modified), set1_pos3) then
					modified = modified:match(set1_pos3) .. "ci"
					
				elseif string.find(string.lower(modified), set2_pos1) then
					modified = modified:match(set2_pos1) .. "ąt"
				elseif string.find(string.lower(modified), set2_pos2) then
					modified = modified:match(set2_pos2) .. "ąt"
					
				elseif string.find(string.lower(modified), set3_pos1) then
					modified = modified:match(set3_pos1) .. "ki"
				elseif string.find(string.lower(modified), set3_pos2) then
					modified = modified:match(set3_pos2) .. "gi"
					
				elseif string.find(string.lower(modified), set4_pos1) then
					if alter.englishSpelling then --Ace Visconti --> Ace'a Visconti
						modified = modified .. "'"
					else
						modified = modified:match(set4_pos1)
					end
					if string.find(string.lower(modified), set4_pos1_sub1) then
						modified = possessive_pl_addVowel(gramGender, gramLiving, modified)
					end
				
				elseif string.find(string.lower(modified), set5_pos1) then
					modified = possessive_pl_addVowel(gramGender, gramLiving, modified)
				end
				
				if hasQuotes then
					modified = quotes(modified)
				end
			end
			table.insert(result, modified)
		end
		
	return table.join(result, " ")
end

--helper function to avoid code duplication
function possessive_pl_addVowel(gramGender, gramLiving, modified)
	if		gramGender == 'M' and gramLiving		then modified = modified .. "a"
	elseif	gramGender == 'M' and not gramLiving	then modified = modified .. "u"
	elseif	gramGender == 'F' and gramLiving		then modified = modified .. "y"
	elseif	gramGender == 'F' and not gramLiving	then modified = modified .. "i"
	end
	return modified
end

-----------------------------
--          /* PT */       --
-----------------------------

--"O" = used for masculine nouns
--"A" = used for feminine nouns
--"Os" = used for plural masculine nouns
--"As" = used for plural feminine nouns
function p.article_pt(alter, indefinite)
	local result = (type(alter) == types.table and alter.article) --[[or (type(alter) == types.string and alter)]] or cstr.empty
	return result .. space
end

-- De + O = "Do"
-- De + A = "Da"
-- De + Os = "Dos"
-- Del + As = "Das"
-- no article = "De"
function p.possessive_pt(alter, params)
	local article = cstr.empty
	if type(alter) == types.table then --parameter is table that should be containing article field
		article = alter.article
	elseif type(alter) == types.string then --forced form as a string parameter
		return alter
	end
	
	local result =
		(not article	 and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "de " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "O" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "do " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "A" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "da " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "Os"  and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "dos " .. (params and params.link == false and "'''" or cstr.empty)) or
		(article == "As" and (params and params.link == false and hiddenChar .. "'''" or cstr.empty) .. "das " .. (params and params.link == false and "'''" or cstr.empty)) or
		cstr.empty
		
	return result .. params.firstName
end

return p