Mòdul:llengua

De Viccionari
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Aquest mòdul proporciona definicions d'una llengua a partir del seu codi. Treu els valors proporcionats pel MediaWiki, o valors per defecte, complementats amb la taula auxiliar Module:llengua/taula.

Funcions:

nom

Retorna el nom de la llengua a partir del seu <codi>. Sintaxi:

{{#invoke:llengua|nom|<codi>}}
del_nom

Retorna "del nom" o "de l'nom" segons les regles d'apostrofació en català. Sintaxi:

{{#invoke:llengua|del_nom|<codi>}}
script

Retorna el codi ISO 15924 del sistema d'escriptura, per defecte Latn. Sintaxi:

{{#invoke:llengua|script|<codi>}}
dir

Retorna la direcció del sistema d'escriptura corresponent al codi de llengua, segons la propietat dir en CSS: ltr (esquerra a dreta, per defecte) o rtl (dreta a esquerra). Sintaxi:

{{#invoke:llengua|dir|<codi>}}
wikimedia

Retorna el codi de llengua corresponent al subdomini de Wikimedia o "" (buit) si no existeix. Sintaxi:

{{#invoke:llengua|wikimedia|<codi>}}

En general es correspon amb el mateix <codi> ISO de llengua, amb algunes excepcions.

wmproject

Retorna el codi de llengua corresponent al subdomini de Wiktionary o "" (buit) si no existeix el projecte. Sintaxi:

{{#invoke:llengua|wmproject|<codi>}}

Vegeu també

  • CLDR, taula de codis i noms de llengua en català importats de CLDR a Wikimedia.
  • Names.php, taula de codis i noms de llengua autònims usats a Wikimedia.

local p = {}

local taula = mw.loadData("Module:llengua/taula")
local general = require("Module:ca-general")

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 taula[code] == nil and mw.language.fetchLanguageName(code, 'ca') == '' 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 llengua definit a /taula o en la llibreria de MediaWiki
local function language_name_get(code)
	if not code then return end
	local name
	if taula[code] then
		name = taula[code].nom
	end
	if name == nil then
		name = mw.language.new('ca'):lcfirst(mw.language.fetchLanguageName(code, 'ca'))
	end
	if not isSet(name) then
		return
	end
	return name
end

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

-- Retorna la direcció d'escriptura, ltr o rtl
function p.dir(codi)
    if type(codi) == "table" then codi = codi.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if codi == nil then
        return 'Cap codi'
    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(codi)
    if scriptRtl[alfabet] then
        return 'rtl'
    end
    local dir = mw.getLanguage(codi):getDir()
    if (dir ~= nil and dir ~= '') then
        return dir
    end
    return 'ltr'
end

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

-- Retorna el subdomini Wikimedia o "" (buit) si no existeix
function p.wikimedia(codi)
    if type(codi) == "table" then codi = codi.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if codi == nil then
        return 'Cap codi'
    end
    local llenguaTaula = taula[codi]
    local codiWM = nil
    if llenguaTaula then
        codiWM = llenguaTaula.wikimedia
    end
    if codiWM ~= nil then
        return codiWM
    elseif mw.language.isSupportedLanguage(codi) then
        return codi
    end
    return ''
end

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

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

-- Retorna "del nom" o "de l'nom"
-- TODO: track and move to addParticle, move template:fals to Module:etimologia
function p.del_nom(frame)
	local langname = p.nom(frame)
	if langname == "preromà" then
		return "d'un preromà"
	end
	return general.addParticle("del", langname)
end

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

-- genera una taula amb les definicions d'una llengua
function p.getByCode(code, allowEtymLang)
	local data
	local parent = {}
	if allowEtymLang then
		data = mw.loadData("Module:etimologia/llengua")[code]
		if data then
			parent = taula[data.parent] or {}
		end
	end
	if not data then
		data = taula[code] or {}
		allowEtymLang = false
	end
	
	local lang = {}
	lang.code = data.parent or code
	lang.name = data.nom or p.nom(code)
	lang.sc = data.sc or parent.sc or p.script(lang.code)
	lang.type = data.type or parent.type or (allowEtymLang and "etymology-only")
	
	return lang
end

return p