Module:Multilingual description: Difference between revisions
From Neodyland Wiki
More actions
m Fix: Failed on pages passing pipes chars (|) as arguments |
m made quick self-test for sort order; and fix empty descriptions with trimming (descriptions with just a language name are still possible by setting description=  or with a nowiki tag |
||
| Line 1: | Line 1: | ||
local p = {} | 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 _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) | |||
['simple'] = 'en', -- same remark | |||
['fiu-vro'] = 'vro', -- same remark | |||
['be-x-old'] = 'be-tarask', -- same remark | |||
['zh-classical'] = 'lzh', -- same remark | |||
['zh-min-nan'] = 'nan', -- same remark | |||
['zh-yue'] = 'yue', -- same remark | |||
['no'] = 'nb', -- same remark | |||
['bat-smg'] = 'sgs', -- 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 | |||
} | |||
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 | |||
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 | |||
return table.concat(descriptions) | |||
end | end | ||
function p.mld(frame) | 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]] | |||
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') --[[ދިވެހިބަސް]] | |||
expect = table.concat(expect) | |||
-- mw.log(expect) | |||
return _mld(input) == expect | |||
end | end | ||
return p | return p | ||
Revision as of 10:37, 17 April 2015
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 _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)
['simple'] = 'en', -- same remark
['fiu-vro'] = 'vro', -- same remark
['be-x-old'] = 'be-tarask', -- same remark
['zh-classical'] = 'lzh', -- same remark
['zh-min-nan'] = 'nan', -- same remark
['zh-yue'] = 'yue', -- same remark
['no'] = 'nb', -- same remark
['bat-smg'] = 'sgs', -- 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
}
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
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
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]]
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') --[[ދިވެހިބަސް]]
expect = table.concat(expect)
-- mw.log(expect)
return _mld(input) == expect
end
return p