Module:Multilingual description: Difference between revisions
From Neodyland Wiki
More actions
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 |
m a couple of remaps for old codes; track all deprecated language codes used in multilingual descriptions (when they could be remapped) |
||
| Line 17: | Line 17: | ||
end | end | ||
end | end | ||
end | |||
local function addRemappedTracking(descriptions) | |||
table.insert(descriptions, '[[Category:Multilingual descriptions using deprecated language codes]]') | |||
end | end | ||
| Line 25: | Line 29: | ||
local remap = { | local remap = { | ||
['als'] = 'gsw', -- legacy broken codes (but known) to changed new code (also known, but conforming to BCP47) | ['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 | ['simple'] = 'en', -- same remark | ||
['zh-classical'] = 'lzh', -- same remark | ['zh-classical'] = 'lzh', -- same remark | ||
['zh-min-nan'] = 'nan', -- same remark | ['zh-min-nan'] = 'nan', -- same remark | ||
['zh-wuu'] = 'wuu', -- same remark | |||
['zh-yue'] = 'yue', -- same remark | ['zh-yue'] = 'yue', -- same remark | ||
['ko-kr'] = 'ko', -- both codes are conforming and supported, only the shortest one is known | ['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-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 | ['sr-latn'] = 'sr-el', -- same remark | ||
} | } | ||
local remapped = false | |||
for cur,alt in pairs(remap) do | for cur,alt in pairs(remap) do | ||
if args[cur] and not(mw.language.isSupportedLanguage(cur) and mw.language.isKnownLanguageTag(cur)) | 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 | and (mw.language.isSupportedLanguage(alt) and mw.language.isKnownLanguageTag(alt)) then | ||
remapped = true | |||
if args[alt] == nil then -- only if this does not conflict | if args[alt] == nil then -- only if this does not conflict | ||
args[alt] = args[cur] -- set key for alternate known language | args[alt] = args[cur] -- set key for alternate known language | ||
| Line 58: | Line 68: | ||
addDescription(descriptions, lang, description) | addDescription(descriptions, lang, description) | ||
end | end | ||
end | |||
if remapped then | |||
addRemappedTracking(descriptions) | |||
end | end | ||
return table.concat(descriptions) | return table.concat(descriptions) | ||
| Line 92: | Line 105: | ||
and after discarding empty descriptions or unsupported language codes. | and after discarding empty descriptions or unsupported language codes. | ||
--]=] | --]=] | ||
addDescription(expect, 'gsw', 'GSW') --[[Alemannisch]] | addDescription(expect, 'gsw', 'GSW') --[[Alemannisch]] -- remapped | ||
addDescription(expect, 'en-gb', 'EN-GB') --[[British English]] | addDescription(expect, 'en-gb', 'EN-GB') --[[British English]] | ||
addDescription(expect, 'en-ca', 'EN-CA <nowiki/>') --[[Canadian English]] | addDescription(expect, 'en-ca', 'EN-CA <nowiki/>') --[[Canadian English]] | ||
| Line 106: | Line 119: | ||
addDescription(expect, 'ar', 'AR') --[[العربية]] | addDescription(expect, 'ar', 'AR') --[[العربية]] | ||
addDescription(expect, 'dv', 'DV') --[[ދިވެހިބަސް]] | addDescription(expect, 'dv', 'DV') --[[ދިވެހިބަސް]] | ||
addRemappedTracking(expect) | |||
expect = table.concat(expect) | expect = table.concat(expect) | ||
-- mw.log(expect) | -- mw.log(expect) | ||
Revision as of 11:06, 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 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