Module:Languages/List

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Previously this module generated a static list like this:

p.list = {'zgh-latn', 'nan-hant', 'ace', 'sma', 'ang', 'af', 'agq', 'ak', 'gsw', 'als', 'en-us', 'ase', 'smn', 'an', 'aae', 'rup', 'roa-rup', 'frp', 'ast', 'atj', 'gn', 'ay', 'az', 'ksf', 'bfd', 'abs', 'gor', 'id', 'ms', 'bdr', 'bkc', 'bkh', 'bm', 'bax', 'nan', 'zh-min-nan', 'bjn', 'ban', 'map-bms', 'kge', 'bug', 'bas', 'btm', 'bbc', 'bbc-latn', 'bew', 'sje', 'bcl', 'bar', 'bi', 'bs', 'brh', 'br', 'en-gb', 'en-ca', 'cps', 'ca', 'ceb', 'cs', 'cho', 'ch', 'cbk-zam', 'chn', 'ny', 'sn', 'tum', 'sei', 'co', 'cy', 'dga', 'dag', 'da', 'se', 'se-no', 'se-se', 'se-fi', 'pdc', 'de', 'de-formal', 'nv', 'dsb', 'na', 'dua', 'dtp', 'mh', 'et', 'efi', 'etu', 'vmw', 'egl', 'eml', 'en', 'es', 'es-formal', 'es-419', 'eo', 'eu', 'ext', 'eto', 'ee', 'ewo', 'wls', 'gur', 'fmp', 'hif', 'hif-latn', 'fon', 'fo', 'fr', 'frc', 'fy', 'ff', 'fur', 'gaa', 'ga', 'gv', 'sm', 'gag', 'gd', 'gl', 'aln', 'gya', 'gpe', 'bbj', 'ki', 'gom-latn', 'guw', 'cnh', 'ha', 'haw', 'ho', 'hsb', 'hr', 'hrx', 'ibb', 'io', 'igl', 'ig', 'rw', 'rn', 'ilo', 'hil', 'ia', 'ie', 'ike-latn', 'ik', 'bto', 'xh', 'zu', 'is', 'isu', 'it', 'jv', 'smj', 'jut', 'rmf', 'kbp', 'kea', 'kl', 'pam', 'kr', 'cak', 'kai', 'krl', 'csb', 'ker', 'kw', 'krj', 'sw', 'kiu', 'bkm', 'kg', 'avk', 'ht', 'ses', 'kri', 'gcf', 'gcr', 'ku', 'ku-latn', 'kus', 'fkv', 'kj', 'nmg', 'jbo', 'lld', 'lad', 'lns', 'ltg', 'la', 'lv', 'lzz', 'to', 'lb', 'nia', 'li', 'lt', 'lij', 'ln', 'lfn', 'liv', 'olo', 'lmo', 'lg', 'mad', 'hu', 'hu-formal', 'vmf', 'mcp', 'mg', 'mt', 'mnc-latn', 'mi', 'mrh', 'arn', 'srq', 'fit', 'byv', 'fat', 'min', 'cdo', 'mwl', 'lus', 'bqz', 'mos', 'mua', 'mus', 'fj', 'nah', 'pcm', 'nmz', 'nap', 'nnz', 'nl', 'nl-informal', 'nds-nl', 'nnh', 'cr', 'nla', 'nge', 'yrl', 'niu', 'lem', 'frr', 'pih', 'no', 'nb', 'nn', 'nrm', 'nov', 'yas', 'sms', 'nys', 'uz-latn', 'uz', 'ann', 'oc', 'ojb', 'om', 'nyo', 'ttj', 'ng', 'de-at', 'hz', 'pfl', 'pag', 'ami', 'pap', 'pap-aw', 'jam', 'pcd', 'wes', 'pwn', 'pms', 'nds', 'pdt', 'cpx-latn', 'pl', 'pt', 'pt-br', 'prg', 'aa', 'kaa', 'quc', 'kk-latn', 'kk-tr', 'crh', 'crh-latn', 'ty', 'ksh', 'ro', 'rmc', 'rmy', 'rgn', 'rm', 'qug', 'qu', 'nyn', 'xsy', 'szy', 'sg', 'sc', 'sro', 'sdc', 'sli', 'de-ch', 'sco', 'trv', 'stq', 'st', 'nso', 'tn', 'scn', 'sq', 'loz', 'simple', 'ss', 'sk', 'sl', 'szl', 'so', 'srn', 'sr-el', 'sh', 'sh-latn', 'su', 'fi', 'sv', 'shy', 'shy-latn', 'kab', 'tl', 'roa-tara', 'rif', 'shi', 'shi-latn', 'tt-latn', 'crh-ro', 'tay', 'tet', 'din', 'vi', 'tg-latn', 'tpi', 'tok', 'tly', 'chy', 've', 'bag', 'tvu', 'aeb-latn', 'tr', 'tk', 'tru', 'tw', 'kcg', 'sju', 'ug-latn', 'vot', 'za', 'vec', 'vep', 'ruq', 'ruq-latn', 'vo', 'vro', 'fiu-vro', 'mcn', 'vut', 'wa', 'bci', 'guc', 'osa-latn', 'vls', 'wal', 'war', 'wo', 'wya', 'ts', 'yat', 'ybb', 'yav', 'yo', 'diq', 'zea', 'sgs', 'bat-smg', 'grc', 'el', 'pnt', 'av', 'ady', 'ady-cyrl', 'kbd', 'kbd-cyrl', 'ab', 'be-tarask', 'alt', 'ba', 'be', 'be-x-old', 'bxr', 'bg', 'ruq-cyrl', 'inh', 'os', 'sjd', 'kv', 'krc', 'kum', 'crh-cyrl', 'ky', 'mrj', 'kk', 'kk-cyrl', 'kk-kz', 'lbe', 'lez', 'mdf', 'mk', 'mo', 'mn', 'rut', 'gld', 'nog', 'ce', 'mhr', 'koi', 'rue', 'rsk', 'sah', 'ru', 'sty', 'cu', 'sr-ec', 'sr', 'sh-cyrl', 'tt-cyrl', 'tt', 'tly-cyrl', 'tg-cyrl', 'tg', 'tyv', 'udm', 'uz-cyrl', 'uk', 'kjh', 'myv', 'xal', 'cv', 'xmf', 'ka', 'hyw', 'hy', 'anp', 'awa', 'ks-deva', 'ks', 'gom-deva', 'gom', 'dty', 'new', 'ne', 'pi', 'bho', 'bh', 'rwr', 'mag', 'mr', 'mai', 'sa', 'hi', 'as', 'bn', 'bpy', 'pa', 'syl', 'gu', 'or', 'ta', 'kn', 'nit', 'te', 'tcy', 'ml', 'si', 'dz', 'bo', 'mni', 'ksw', 'blk', 'kjp', 'shn', 'mnw', 'my', 'rki', 'km', 'lo', 'tdd', 'nod', 'th', 'ban-bali', 'sat', 'chr', 'ike-cans', 'iu', 'got', 'tzm', 'zgh', 'shi-tfng', 'ti', 'am', 'ii', 'ko-kp', 'ko', 'ja', 'ryu', 'zh', 'zh-cn', 'zh-tw', 'zh-sg', 'zh-mo', 'zh-hans', 'zh-hant', 'zh-hk', 'zh-my', 'wuu-hant', 'wuu', 'wuu-hans', 'hak', 'lzh', 'zh-classical', 'hsn', 'yue', 'zh-yue', 'yue-hant', 'yue-hans', 'cpx', 'cpx-hant', 'cpx-hans', 'gan-hans', 'gan', 'gan-hant', 'nan-hani', 'mnc-mong', 'mnc', 'he', 'yi', 'ur', 'ary', 'ar', 'acm', 'bqi', 'ms-arab', 'ps', 'pnb', 'aeb-arab', 'aeb', 'azb', 'arq', 'bcc', 'bgn', 'skr', 'skr-arab', 'sd', 'fa', 'kk-arab', 'kk-cn', 'ku-arab', 'ks-arab', 'khw', 'ckb', 'sdh', 'glk', 'ota', 'luz', 'lrc', 'lki', 'mzn', 'arz', 'ug-arab', 'hno', 'ug', 'arc', 'dv', 'nqo'}

Now this list is generated from the internal list of supported languages in the local instance of MediaWiki, which is then sorted by native name of each language in its primary script (LTR scripts are sorted before RTL scripts, language names in the same script are sorted together.

The order is also consistant with Module:Multilingual description which has its own comprehensive tests for the sort order of the list, its completeness (all language codes supported by the local instance of Mediawiki, plus some known aliases needed for BCP 47 conformance or for compatibility with past versions of this wiki even if they are not supported now and not shown in the list above), as well as for the directionality of each language.

Note that as of 2020-10-18, there's a new bug in Scribunto, which causes the standard "table.sort()" function to no longer accept a standard comparison function. Now when "table.sort()" fails, the error is correctly catched (instead of producing a viabile error), but the list may not be sorted correctly (temporarily) in some cases. The bug is tracked and corrected in Mediawiki but waiting to be deployed with the fix. Nothing will need to be changed (using "perror()" will then no longer have any effect, it is just needed for now and the bug was not a bug of this module which was properly tested and sudddently failed unexpectedly). Thanks.

Code

local p = {};

local nativeList = require('Module:Multilingual description/sort')
local nativeListIndex = {}
for i, lang in ipairs(nativeList) do
    nativeListIndex[lang] = i
end

--
--[[ Check this list by running this in the console of the Lua Module editor in MediaWiki:
="p.list = {'" .. table.concat(p.getSortedList(mw.language.fetchLanguageNames()), "', '") .. "'}"
]]
function p.getSortedList(mwLangList)
    local sortedList = {}
    for lang, _ in pairs(mwLangList) do
        table.insert(sortedList, lang)
    end
    pcall(function()
        table.sort(sortedList, function(lang1, lang2)
            return (nativeListIndex[lang1] or -1) <= (nativeListIndex[lang2] or -1)
        end)
    end)
    return sortedList
end

-- note that this fetch will be costly if all these languages are loaded individually with their data by Mediawiki
p.list = p.getSortedList(mw.language.fetchLanguageNames())

setmetatable(p, {
    quickTests = function()
        local i = 0
        for k, v in pairs(p.list) do
            if type(k) ~= 'number' or k < 1 or k ~= math.floor(k)
            or type(v) ~= 'string' or #v < 2 or #v > 16
            or (v):find('^[a-z][%-0-9a-z]*[0-9a-z]$') ~= 1 then
                return false, ': invalid sequence of language codes in p.list["' .. tostring(k) .. '"] = "' .. tostring(v) .. '"'
            end
            i = i + 1
        end
        if #(p.list) ~= i then return false, ': invalid sequence: length = '.. #(p.list) ' for ' .. i .. ' distinct keys' end
        return true
    end
})
--[[ To test this module in the Lua console:
=getmetatable(p).quickTests() -- must return true
--]]
return p;