Salta al contegnùo

Modulo:łéngua

Da Wikisionario

La documentazione per questo modulo può essere creata in Modulo:łéngua/man

--DA CA.WIKT--
local p = {}

local tola = mw.loadData("Module:łéngua/toła")

local function isSet( var )
	return not (var == nil or var == '')
end

--[[extracts and returns IETF language tag parts:
	primary language subtag (required) - 2 or 3 character IANA language code
	script subtag - four character IANA script code
	region subtag - two-letter or three digit IANA region code
	variant subtag - four digit or 5-8 alnum variant code; only one variant subtag supported
	private subtag - x- followed by 1-8 alnum private code; only supported with the primary language tag

in any one of these forms
	lang					lang-variant
	lang-script				lang-script-variant
	lang-region				lang-region-variant
	lang-script-region		lang-script-region-variant
	lang-x-private	
	
each of lang, script, region, variant, and private, when used, must be valid

Languages with both two- and three-character code synonyms are promoted to the two-character synonym because
the IANA registry file omits the synonymous three-character code; we cannot depend on browsers understanding
the synonymous three-character codes in the lang= attribute.

returns six  values; all lower case.  Valid parts are returned as themselves; omitted parts are returned as empty strings, invalid
parts are returned as nil; the sixth returned item is an error message (if an error detected) or nil.

see http://www.rfc-editor.org/rfc/bcp/bcp47.txt section 2.1

]]

local function get_ietf_parts(source, args_script, args_region, args_variant)
	local code, script, region, variant, private								-- ietf tag parts

	if not isSet(source) then
		return nil, nil, nil, nil, nil, 'missing language tag'
	end

	local pattern = {															-- table of tables holding acceptibe ietf tag patterns and short names of the ietf part captured by the pattern
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%a%a)%-(%d%d%d%d)$', 's', 'r', 'v'}, 				-- 1 -  ll-Ssss-RR-variant (where variant is 4 digits)
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)%-(%d%d%d%d)$', 's', 'r', 'v'},				-- 2 -  ll-Ssss-DDD-variant (where region is 3 digits; variant is 4 digits)
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%a%a)%-(%w%w%w%w%w%w?%w?%w?)$', 's', 'r', 'v'},		-- 3 -  ll-Ssss-RR-variant (where variant is 5-8 alnum characters)
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)%-(%w%w%w%w%w%w?%w?%w?)$', 's', 'r', 'v'},	-- 4 -  ll-Ssss-DDD-variant (where region is 3 digits; variant is 5-8 alnum characters)
		
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d%d)$', 's', 'v'},						-- 5 -  ll-Ssss-variant (where variant is 4 digits)
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%w%w%w%w%w%w?%w?%w?)$', 's', 'v'},			-- 6 -  ll-Ssss-variant (where variant is 5-8 alnum characters)
		
		{'^(%a%a%a?)%-(%a%a)%-(%d%d%d%d)$', 'r', 'v'},							-- 7 -  ll-RR-variant (where variant is 4 digits)
		{'^(%a%a%a?)%-(%d%d%d)%-(%d%d%d%d)$', 'r', 'v'},						-- 8 -  ll-DDD-variant (where region is 3 digits; variant is 4 digits)
		{'^(%a%a%a?)%-(%a%a)%-(%w%w%w%w%w%w?%w?%w?)$', 'r', 'v'},				-- 9 -  ll-RR-variant (where variant is 5-8 alnum characters)
		{'^(%a%a%a?)%-(%d%d%d)%-(%w%w%w%w%w%w?%w?%w?)$', 'r', 'v'},				-- 10 - ll-DDD-variant (where region is 3 digits; variant is 5-8 alnum characters)
		
		{'^(%a%a%a?)%-(%d%d%d%d)$', 'v'},										-- 11 - ll-variant (where variant is 4 digits)
		{'^(%a%a%a?)%-(%w%w%w%w%w%w?%w?%w?)$', 'v'},							-- 12 - ll-variant (where variant is 5-8 alnum characters)
		
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%a%a)$', 's', 'r'},							-- 13 - ll-Ssss-RR
		{'^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)$', 's', 'r'},						-- 14 - ll-Ssss-DDD (region is 3 digits)
		
		{'^(%a%a%a?)%-(%a%a%a%a)$', 's'},										-- 15 - ll-Ssss
		
		{'^(%a%a%a?)[%-_](%a%a)$', 'r'},										-- 16 - ll-RR
		{'^(%a%a%a?)%-(%d%d%d)$', 'r'},											-- 17 - ll-DDD (region is 3 digits)
		
		{'^(%a%a%a?)$'},														-- 18 - ll
		
		{'^(%a%a%a?)%-x%-(%w%w?%w?%w?%w?%w?%w?%w?)$', 'p'},						-- 19 - ll-x-pppppppp (private is 1-8 alnum characters)
		}

	local t = {}										-- table of captures; serves as a translator between captured ietf tag parts and named variables

	for i, v in ipairs(pattern) do						-- spin through the pattern table looking for a match
		local c1, c2, c3, c4							-- captures in the 'pattern' from the pattern table go here
	
		c1, c2, c3, c4 = source:match(pattern[i][1])	-- one or more captures set if source matches pattern[i])
		if c1 then										-- c1 always set on match
			code = c1									-- first capture is always code
			t = {
				[pattern[i][2] or 'x'] = c2,			-- fill the table of captures with the rest of the captures
				[pattern[i][3] or 'x'] = c3,			-- take index names from pattern table and assign sequential captures
				[pattern[i][4] or 'x'] = c4,			-- index name may be nil in pattern[i] table so "or 'x'" spoofs a name for this index in this table
				}
			script = t.s or ''							-- translate table contents to named variables;
			region = t.r or ''							-- absent table entries are nil so set named ietf parts to empty string for concatenation
			variant= t.v or ''
			private = t.p or ''
			break										-- and done
		end
	end
	
	if not code then
		return nil, nil, nil, nil, nil, table.concat ({'unrecognized language tag: ', source}) -- don't know what we got but it is malformed
	end
	
	code = code:lower()				-- ensure that we use and return lower case version of this
	--if synonyms[code] then			-- if 639-2/639-2T code has a 639-1 synonym
	--	code = synonyms[code]		-- use the synonym
	--end
	if tola[code] == nil and mw.language.fetchLanguageName(code, 'vec') == '' then
		return nil, nil, nil, nil, nil, table.concat({'unrecognized language code: ', code}) -- invalid language code, don't know about the others (don't care?)
	end
	
	if not isSet(script) then
		script = args_script or ''						-- use args.script if provided
	end 
	if isSet(script) then
		script = string.sub(script, 1, 1):upper() .. string.sub(script, 2):lower()
		--if not scripts[script] then
		--	return code, nil, nil, nil, nil, table.concat({'unrecognized script: ', script, ' for code: ', code});	-- language code ok, invalid script, don't know about the others (don't care?)
		--end
	end
	
	if not isSet(region) then
		region = args_region or ''					-- use args.region if provided
	end
	region = region:upper()
	
	if not isSet(variant) then
		variant = args_variant or ''				-- use args.variant if provided
	end 
	variant = variant:lower()						-- ensure that we use and return lower case version of this
	
	if isSet(private) then
		private = private:lower()					-- ensure that we use and return lower case version of this
	end
	
	return code, script, region, variant, private, nil	-- return the good bits; make sure that msg is nil
end

-- Cerca el nom de lengua definit a /tola o en la llibreria de MediaWiki
local function language_name_get(code)
	if not code then return end
	local name
	if tola[code] then
		name = tola[code].nom
	end
	if name == nil then
		name = mw.language.new('vec'):lcfirst(mw.language.fetchLanguageName(code, 'vec'))
	end
	if not isSet(name) then
		return
	end
	return name
end

-- Cerca el nom de lengua definit a /tola o en la llibreria de MediaWiki
function p.nom(codaze)
    if type(codaze) == "table" then codaze = codaze.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if codaze == nil then
        return 'Nisun còdaze'
    end
    local nom
    local lang, script = get_ietf_parts(codaze)
    if lang and isSet(script) then
    	nom = language_name_get(lang)
    end
    if not nom then
    	nom = language_name_get(codaze)
    end
    return nom or codaze
end

-- Retorna la direcció d'escriptura, ltr o rtl
function p.dir(codaze)
    if type(codaze) == "table" then codaze = codaze.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if codaze == nil then
        return 'Nisun còdaze'
    end
	local scriptRtl = {["Arab"]=true, ["fa-Arab"]=true, ["ks-Arab"]=true, ["ota-Arab"]=true, 
		["ps-Arab"]=true, ["ug-Arab"]=true, ["ur-Arab"]=true, ["Avst"]=true, ["Hebr"]=true, 
		["Nkoo"]=true, ["Phli"]=true, ["Phnx"]=true, ["Syrc"]=true, ["Thaa"]=true}
    local alfabet = p.script(codaze)
    if scriptRtl[alfabet] then
        return 'rtl'
    end
    local dir = mw.getLanguage(codaze):getDir()
    if (dir ~= nil and dir ~= '') then
        return dir
    end
    return 'ltr'
end

-- Retorna el sistema d'escriptura, Latn per defecte
function p.script(codaze)
	if type(codaze) == "table" then codaze = codaze.args[1] end -- des de plantilles via invoke o des de mòduls via require
	if codaze == nil then
		return 'Nisun còdaze'
	end
	local script
	if tola[codaze] then
		script = tola[codaze].script
	end
	if not script then
		_, script = get_ietf_parts(codaze)
	end
	return isSet(script) and script or 'Latn'
end

-- Retorna el subdomini Wikimedia o "" (buit) si no existeix
function p.wikimedia(codaze)
    if type(codaze) == "table" then codaze = codaze.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if codaze == nil then
        return 'Nisun còdaze'
    end
    local lenguatola = tola[codaze]
    local codazeWM = nil
    if lenguatola then
        codazeWM = lenguatola.wikimedia
    end
    if codazeWM ~= nil then
        return codazeWM
    elseif mw.language.isSupportedLanguage(codaze) then
        return codaze
    end
    return ''
end

-- A Wikimedia project exists if its Main page is linked at d:Q5296
function p.wmproject(codaze)
    if type(codaze) == "table" then codaze = codaze.args[1] end -- via invoke o via require
    if codaze == nil then return end
    local lengua_tola = tola[codaze]
    local codaze_wikimedia = codaze
    if lengua_tola then
        codaze_wikimedia = lengua_tola.wikimedia or codaze
    end
    local codaze_wikidata = string.gsub(codaze_wikimedia, '-', '_')
    local main_page = mw.wikibase.getEntity('Q5296')
    if main_page.sitelinks[codaze_wikidata .. 'wiktionary'] then
    	return codaze_wikimedia
    end
    return
end

-- Existeix el codaze?
function p.existeix(codaze)
    --if tola[codaze] or mw.language.isSupportedLanguage(codaze) then
    --    return true
    --end
    
    -- Comprovació alternativa, les llengües suportades pel Mediawiki són limitades
    if type(codaze) == "table" then codaze = codaze.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if p.nom(codaze) == codaze then
    	return false
    end
    return true
end

--[=[ 
Funció auxiliar per l’apostrofació de l’article masculí, retorna true o false
Limitacions, en general no necessàries per noms de lengua:
    els casos previstos de h consonant són limitats
    no analitza el cas l’s+consonant
    no preveu si hi ha caràcters de puntuació inicial (l’«apòstrof», l’[[enllaç]])
    no apostrofa davant números (l’1, l’11) ni sigles (l’FMI)
--]=]

local apostrofa = {
	["hakk"]=false, -- h consonant (hakka)
    ["hawa"]=false, -- h consonant (hawaià)
    ["hia"]=false, ["hie"]=false, ["hio"]=false, ["hui"]=false, -- vocal consonant
    ["uix"]=true, -- excepció per u vocal
    ["ha"]=true, ["he"]=true, ["hi"]=true, ["hí"]=true, ["ho"]=true, ["hu"]=true, ["hy"]=true, -- excepte anteriors
    ["ia"]=false, ["ià"]=false, ["ie"]=false, ["io"]=false, ["iu"]=false, -- i consonant
    ["ua"]=false, ["ue"]=false, ["ui"]=false, ["uí"]=false, ["uï"]=false, ["uo"]=false, -- u consonant
    ["ya"]=false, ["ye"]=false, ["yi"]=false, ["yo"]=false, ["yu"]=false, -- y consonant
    ["a"]=true, ["à"]=true, ["e"]=true, ["è"]=true, ["é"]=true,
    ["i"]=true, ["í"]=true, ["ï"]=true, ["y"]=true,
    ["o"]=true, ["ò"]=true, ["ó"]=true, ["u"]=true, ["ú"]=true, ["ü"]=true -- excepte anteriors
    }

local function sapostrofa(text)
    local elText = mw.ustring.lower(text)
    for i=4,1,-1 do
        lletres = mw.ustring.sub(elText, 1, i)
        apo = apostrofa[lletres]
        if apo ~= nil then
            return apo
        end
    end
    return false
end

-- Retorna "del nom" o "de l'nom"
function p.del_nom(frame)
	local nom = p.nom(frame)
	if nom == "preromà" then
		return "d'un preromà"
	end
	if sapostrofa(nom) then
		return "de l'" .. nom
	end
	return "del " .. nom
end

-- Retorna "al nom" o "a l'nom"
function p.al_nom(frame)
    local nom = p.nom(frame)
    if sapostrofa(nom) then
        return "a l'" .. nom
    end
    return "al " .. nom
end

-- Retorna "el nom" o "l'nom"
-- TODO: unificar funcions d'apostrofació en base a paràmetre de partícula
function p.el_nom(codaze)
    local nom = p.nom(codaze)
    if sapostrofa(nom) then
        return "l'" .. nom
    end
    return "el " .. nom
end

-- transcripció, si existeix el mòdul per la lengua amb la funció tr
function p.trans(lang, text)
	local trans
	if lang and text then
		local m_trans = tola[lang]
		if m_trans and m_trans.trans_module then
			trans = require("Module:" .. m_trans.trans_module).tr(text)
		end
	end
	return trans
end

return p
Traesto fora da Wikipèdia - L'ençiclopedia łìbara e cołaboradiva in łéngua Vèneta "https://vec.wiktionary.org/w/index.php?title=Modulo:łéngua&oldid=33832"