Módulo:LangInfo
Saltar para a navegação
Saltar para a pesquisa
Provides the functionality of {{langInfo}}.
The main and only function is ; the kind of information returned is determined by the second unnamed parameter. The table go contains, for every language where it is necessary, the localized page title of the respective language's page about that language.
wplinks
Example:
["en"] = "English language"
This is only necessary if that page title is not equal to the language's field in the data module.
lname
--------------------------------------------------------------------------------
--
-- =============================================================================
--
-- Module:LangInfo
--
-- Miscellaneous information about languages
--
-- =============================================================================
--
-- Code annotations:
-- This module is documented according to LuaCATS (Lua Comment and Type System).
-- LuaCATS comments are prefixed with three dashes (---) and use Markdown syntax.
-- For a full list of annotations, see the following link:
-- https://luals.github.io/wiki/annotations/
--
--------------------------------------------------------------------------------
---The Wikipedia link to a language in that language is the same as the language's
---endonym by default. Adjust it manually here, if necessary.
local wplinks = {
-- format: ["language code"] = "local Wikipedia article",
-- example for English: ["en"] = "American English",
["bg"] = "Български език",
["da"] = "Dansk (sprog)",
["de"] = "Deutsche Sprache",
["es"] = "Idioma español",
["fi"] = "Suomen kieli",
["hi"] = "हिन्दी",
["hu"] = "Magyar nyelv",
["it"] = "Lingua italiana",
["lt"] = "Lietuvių kalba",
["lv"] = "Latviešu valoda",
["pl"] = "Język polski",
["pt"] = "Língua portuguesa",
["ru"] = "Русский язык",
["sk"] = "Slovenčina",
["th"] = "ภาษาไทย",
["uk"] = "Українська мова",
["zh"] = "中文",
}
---The "countrycode" data of a language is the ISO 3166-1 code of the country
---that the language is commonly associated with.
---By default that is the same as the language's ISO 639-1/639-3 language code.
---Adjust it manually here, if necessary.
local countrycodes = {
-- format: ["language code"] = "country code",
-- example for English: ["en"] = "us",
["ar"] = "eg",
["be"] = "by",
["cs"] = "cz",
["da"] = "dk",
["el"] = "gr",
["en"] = "us",
["hi"] = "in",
["ja"] = "jp",
["ko"] = "kr",
["sv"] = "se",
["uk"] = "ua",
["vi"] = "vn",
["yue"] = "cn",
["zh"] = "cn",
}
local preciselanguagecodes = {
-- format: ["language code"] = "precise language codes",
-- example for Chinese Simplified: ["zh"] = "zh-hans",
["zh"] = "zh-hans",
}
local trim = mw.text.trim
---Holds the `fullname`, `shortname`, and `lname` data for a lot of languages.
---@type table<string, table<string, string>>
local data = mw.loadData('Module:LangInfo/data')
---Holds the arguments from the template call.
---@type table<string, string>
local args_table
---Return a trimmed version of the value of the template parameter with the specified `key`.
---Return `nil` if the parameter is empty or unset.
---@param key string|integer
---@return string?
local getArg = function(key)
local value = args_table[key]
if not value then
return nil
end
value = trim(value)
if value == '' then
return nil
end
return value
end
--------------------------------------------------------------------------------
---Main return object
local p = {}
---Return the queried piece of information about a language.
---@param frame table Interface to the parser (`mw.frame`)
---@return string
p.go = function(frame, args)
args_table = args or frame.args -- global input args cache
local _arg1 = getArg(1) or ''
local _arg2 = getArg(2) or ''
if not data[_arg1] then
return ''
end
if _arg2 == 'name' then
if data[_arg1]['shortname'] ~= '' then
return data[_arg1]['shortname']
else
return data[_arg1]['fullname']
end
elseif _arg2 == 'countrycode' then
if countrycodes[_arg1] ~= nil then
return countrycodes[_arg1]
else
-- default country code: same as language code
return _arg1
end
elseif _arg2 == 'languagecode' then
if preciselanguagecodes[_arg1] ~= nil then
return preciselanguagecodes[_arg1]
else
-- default true language code: same as language code
return _arg1
end
elseif _arg2 == 'lname' or _arg2 == 'wplink' then
if _arg2 == 'wplink' and wplinks[_arg1] ~= nil then
return wplinks[_arg1]
else
-- only get the first element of a potential comma-separated list
-- (the parentheses are there to drop the second return value of `string.gsub`)
return ( string.gsub(data[_arg1]['lname'], "(.-),.+", "%1") )
end
elseif data[_arg1][_arg2] ~= '' then
return data[_arg1][_arg2]
end
return ''
end
return p