Module:Multilingual description
From Neodyland Wiki
More actions
This documentation is transcluded from Module:Multilingual description/doc.
local p = {}
local sortedKnownLanguageTags = require('Module:Multilingual description/sort')
local function addDescription(descriptions, lang, description)
if type(description) == 'string' then
description = mw.text.trim(description)
if description:len() > 0 then
table.insert(descriptions, mw.getCurrentFrame():expandTemplate{
title = 'Ls',
args = {
lang,
description,
classes = 'description'
}
})
end
end
end
local function addRemappedTracking(descriptions)
table.insert(descriptions, '[[Category:Multilingual descriptions using deprecated language codes]]')
end
local function _mld(args)
local descriptions = {}
-- Remap some "valid" language codes that are still unknown, but are known by another code
-- in order to get a visible language name (and if possible, BCP47 conformance)!
local remap = {
['als'] = 'gsw', -- legacy broken codes (but known) to changed new code (also known, but conforming to BCP47)
['bat-smg'] = 'sgs', -- same remark
['be-x-old'] = 'be-tarask', -- same remark
['bh'] = 'bho', -- same remark
['bu'] = 'my', -- same remark
['no'] = 'nb', -- same remark
-- 'nrm' should be remapped here, but there's no known target for now
['fiu-vro'] = 'vro', -- same remark
['simple'] = 'en', -- same remark
['zh-classical'] = 'lzh', -- same remark
['zh-min-nan'] = 'nan', -- same remark
['zh-wuu'] = 'wuu', -- same remark
['zh-yue'] = 'yue', -- same remark
['ko-kr'] = 'ko', -- both codes are conforming and supported, only the shortest one is known
['sr-cyrl'] = 'sr-ec', -- this alternate known code is non-standard and in fact not supported, but has a correct native name
['sr-latn'] = 'sr-el', -- same remark
}
local remapped = false
for cur,alt in pairs(remap) do
if args[cur] and not(mw.language.isSupportedLanguage(cur) and mw.language.isKnownLanguageTag(cur))
and (mw.language.isSupportedLanguage(alt) and mw.language.isKnownLanguageTag(alt)) then
remapped = true
if args[alt] == nil then -- only if this does not conflict
args[alt] = args[cur] -- set key for alternate known language
end
args[cur] = nil -- remove the standard unknown key
end
end
-- First all known languages in order if they have description
for _, lang in ipairs(sortedKnownLanguageTags) do
if args[lang] then
addDescription(descriptions, lang, args[lang])
args[lang] = nil
end
end
-- Append other unknown languages, but only if they are supported
for lang, description in pairs(args) do
if type(lang) == 'string' and mw.language.isSupportedLanguage(lang) then
addDescription(descriptions, lang, description)
end
end
if remapped then
addRemappedTracking(descriptions)
end
return table.concat(descriptions)
end
function p.mld(frame)
local args = (frame:getParent() or {}).args or {}
return _mld(args)
end
function p.runTests()
local input = {
[1] = 'One?', -- discarded
unsupported = 'What?', -- discarded
en = ' ', -- empty description after trimming (discarded)
als = 'GSW', -- will be remapped
['en-gb'] = 'EN-GB ', -- trimming at end
['en-ca'] = 'EN-CA <nowiki/>',
de = ' DE', -- trimming at start
fr = ' FR ', -- trimming both ends
rue = 'RUE',
ru = 'RU',
ko = 'KO',
ja = 'JA',
zh = 'ZH',
he = 'HE',
ur = 'UR',
ar = 'AR',
dv = 'DV',
}
local expect = {}
--[=[
This is the exact order to expect according to native language names,
and after discarding empty descriptions or unsupported language codes.
--]=]
addDescription(expect, 'gsw', 'GSW') --[[Alemannisch]] -- remapped
addDescription(expect, 'en-gb', 'EN-GB') --[[British English]]
addDescription(expect, 'en-ca', 'EN-CA <nowiki/>') --[[Canadian English]]
addDescription(expect, 'de', 'DE') --[[Deutsch]]
addDescription(expect, 'fr', 'FR') --[[français]]
addDescription(expect, 'rue', 'RUE') --[[русиньскый]]
addDescription(expect, 'ru', 'RU') --[[русский]]
addDescription(expect, 'ko', 'KO') --[[한국어]]
addDescription(expect, 'ja', 'JA') --[[日本語]]
addDescription(expect, 'zh', 'ZH') --[[中文]]
addDescription(expect, 'he', 'HE') --[[עברית]]
addDescription(expect, 'ur', 'UR') --[[اردو]]
addDescription(expect, 'ar', 'AR') --[[العربية]]
addDescription(expect, 'dv', 'DV') --[[ދިވެހިބަސް]]
addRemappedTracking(expect)
expect = table.concat(expect)
-- mw.log(expect)
return _mld(input) == expect
end
return p