Module:Dir/RTL overrides: Difference between revisions
From Neodyland Wiki
More actions
m this is now a fully tested complete mapping that will avoid using the costly mw.language:isRTL() that throws errors |
mNo edit summary |
||
| Line 18: | Line 18: | ||
for _, lang in ipairs(ltrLangs) do p[lang] = false end | for _, lang in ipairs(ltrLangs) do p[lang] = false end | ||
p[true] = function() | |||
-- utility: reverse order iterator on sequences | -- utility: reverse order iterator on sequences | ||
local function revipairs(t) | local function revipairs(t) | ||
| Line 81: | Line 81: | ||
mw.log("local ltrLangs = { '" .. table.concat(ltrLangs, "', '") .. "' }") | mw.log("local ltrLangs = { '" .. table.concat(ltrLangs, "', '") .. "' }") | ||
return true | return true | ||
end | end | ||
--[==[ Type this in the Lua console to run tests: | --[==[ Type this in the Lua console to run tests: | ||
= | =p[true]() | ||
--]==] | --]==] | ||
return p | return p | ||
Revision as of 19:50, 20 April 2015
This documentation is transcluded from Module:Dir/RTL overrides/doc.
local p = {}
--[==[
Basic input sequences: relative order or duplicates does not matter here (but the end of the self test displays them sorted and deduplicated),
but codes must be valid and belong to only one of the two sequences).
Any missing language code will be checked with mw.language.new(code):isRTL(), which is costly
in loaded resources (it can only check 20 languages at most including the content language)
So it is best to map all languages that are the most likely to be used.
--]==]
local rtlLangs = { 'aeb', 'ar', 'arc', 'arz', 'azb', 'bcc', 'bgn', 'bqi', 'ckb', 'dv', 'fa', 'fa-af', 'glk', 'ha', 'he', 'khw', 'kk-arab', 'kk-cn', 'ks', 'ks-arab', 'ku-arab', 'mzn', 'pnb', 'prd', 'ps', 'sd', 'ug', 'ug-arab', 'ur', 'uz-arab', 'ydd', 'yi' }
local ltrLangs = { 'aa', 'ab', 'ace', 'af', 'ak', 'aln', 'als', 'am', 'an', 'ang', 'anp', 'arn', 'arq', 'ary', 'as', 'ast', 'av', 'avk', 'ay', 'az', 'ba', 'bar', 'bat-smg', 'bbc', 'bbc-latn', 'bcl', 'be', 'be-tarask', 'be-x-old', 'bg', 'bh', 'bho', 'bi', 'bjn', 'bm', 'bn', 'bo', 'bpy', 'br', 'brh', 'bs', 'bto', 'bu', 'bug', 'bxr', 'ca', 'cbk-zam', 'cdo', 'ce', 'ceb', 'ch', 'cho', 'chr', 'chy', 'co', 'cps', 'cr', 'crh', 'crh-cyrl', 'crh-latn', 'cs', 'csb', 'cu', 'cv', 'cy', 'da', 'de', 'de-at', 'de-ch', 'de-formal', 'diq', 'dsb', 'dtp', 'dz', 'ee', 'egl', 'el', 'eml', 'en', 'en-ca', 'en-gb', 'en-ie', 'en-us', 'eo', 'es', 'et', 'eu', 'ext', 'ff', 'fi', 'fil', 'fit', 'fiu-vro', 'fj', 'fo', 'fr', 'frc', 'fro', 'frp', 'frr', 'fur', 'fy', 'ga', 'gag', 'gan', 'gan-hans', 'gan-hant', 'gcf', 'gd', 'gl', 'gn', 'gom-latn', 'got', 'grc', 'gsw', 'gu', 'gv', 'hak', 'haw', 'hi', 'hif', 'hif-deva', 'hif-latn', 'hil', 'ho', 'hr', 'hrx', 'hsb', 'ht', 'hu', 'hy', 'hz', 'ia', 'id', 'ie', 'ig', 'ii', 'ik', 'ike-cans', 'ike-latn', 'ilo', 'inh', 'io', 'is', 'it', 'iu', 'ja', 'jam', 'jbo', 'jut', 'jv', 'ka', 'kaa', 'kab', 'kbd', 'kbd-cyrl', 'kbd-latn', 'kg', 'ki', 'kiu', 'kj', 'kk', 'kk-cyrl', 'kk-kz', 'kk-latn', 'kk-tr', 'kl', 'km', 'kn', 'ko', 'koi', 'ko-kp', 'ko-kr', 'kr', 'krc', 'kri', 'krj', 'ks-deva', 'ksh', 'ku', 'ku-latn', 'kv', 'kw', 'ky', 'la', 'lad', 'lb', 'lbe', 'lez', 'lfn', 'lg', 'li', 'lij', 'liv', 'lmo', 'ln', 'lo', 'loz', 'lrc', 'lt', 'ltg', 'lus', 'lv', 'lzh', 'lzz', 'mai', 'map-bms', 'mdf', 'mg', 'mh', 'mhr', 'mi', 'min', 'mk', 'ml', 'mn', 'mo', 'mr', 'mrj', 'ms', 'mt', 'mus', 'mwl', 'my', 'myv', 'na', 'nah', 'nan', 'nap', 'nb', 'nds', 'nds-nl', 'ne', 'new', 'ng', 'niu', 'nl', 'nl-informal', 'nn', 'no', 'nov', 'nrm', 'nso', 'nv', 'ny', 'oc', 'om', 'or', 'os', 'pa', 'pag', 'pam', 'pap', 'pcd', 'pdc', 'pdt', 'pfl', 'pi', 'pih', 'pl', 'pms', 'pnt', 'prg', 'pt', 'pt-br', 'pt-cv', 'pt-pt', 'qu', 'qug', 'rgn', 'rif', 'rm', 'rmy', 'rn', 'ro', 'roa-rup', 'roa-tara', 'ru', 'rue', 'rup', 'ruq', 'ruq-cyrl', 'ruq-latn', 'rw', 'sa', 'sah', 'sat', 'sc', 'scd', 'scn', 'sco', 'sdc', 'se', 'sei', 'ses', 'sg', 'sgs', 'sh', 'sh-cyrl', 'shi', 'shi-latn', 'shi-tfng', 'sh-latn', 'si', 'simple', 'sk', 'sl', 'sli', 'sm', 'sma', 'sn', 'so', 'sq', 'sr', 'sr-cyrl', 'sr-ec', 'sr-el', 'sr-latn', 'srn', 'ss', 'st', 'stq', 'su', 'sv', 'sw', 'szl', 'ta', 'tcy', 'te', 'tet', 'tg', 'tg-cyrl', 'tg-latn', 'th', 'ti', 'tk', 'tl', 'tly', 'tn', 'to', 'tokipona', 'tpi', 'tr', 'tru', 'ts', 'tt', 'tt-cyrl', 'tt-latn', 'tum', 'tw', 'ty', 'tyv', 'tzm', 'udm', 'ug-latn', 'uk', 'uz', 'uz-cyrl', 'uz-latn', 've', 'vec', 'vep', 'vi', 'vls', 'vmf', 'vo', 'vot', 'vro', 'wa', 'war', 'wo', 'wuu', 'xal', 'xh', 'xmf', 'yo', 'yue', 'za', 'zea', 'zh', 'zh-classical', 'zh-cn', 'zh-hans', 'zh-hant', 'zh-hk', 'zh-min-nan', 'zh-mo', 'zh-my', 'zh-sg', 'zh-tw', 'zh-wuu', 'zh-yue', 'zu' }
-- Build the inverted map of languages to direction returned by this data module (fast but unchecked)
for _, lang in ipairs(rtlLangs) do p[lang] = true end
for _, lang in ipairs(ltrLangs) do p[lang] = false end
p[true] = function()
-- utility: reverse order iterator on sequences
local function revipairs(t)
return function(t, i)
i = i - 1
local v = t[i]
if v == nil then return nil end
return i, v
end, t, #t + 1
end
-- Basic check of data format
local function checkLangs(name, langs)
for k, lang in pairs(langs) do
assert(type(k) == 'number' and k == math.floor(k)
and type(lang) == 'string' and #lang >= 2 and #lang <= 16
and lang:find('^[a-z][%-0-9a-z]*[0-9a-z]$') == 1,
": Invalid sequence of language codes, " .. name .. "['" .. k .. "'] = '" .. lang .. "'")
end
return true
end
local ok, msg
ok, msg = pcall(checkLangs, 'rtlLangs', rtlLangs)
if not ok then return false, msg end
ok, msg = pcall(checkLangs, 'ltrLangs', ltrLangs)
if not ok then return false, msg end
-- Build inverse maps of languages having each direction
local isrtl, isltr = {}, {}
for _, lang in ipairs(rtlLangs) do isrtl[lang] = true end
for _, lang in ipairs(ltrLangs) do isltr[lang] = true end
-- Check conflicts using the two inverse maps
for _, lang in ipairs(rtlLangs) do
if isltr[lang] then return false, ": Direction conflict for '" .. lang .. "'" end
end
for _, lang in ipairs(ltrLangs) do
if isrtl[lang] then return false, ": Direction conflict for '" .. lang .. "'" end
end
-- Log missing languages (allows filling the tables above) according to MediaWiki internal data
langs = mw.language.fetchLanguageNames()
for lang, _ in pairs(langs) do
if p[lang] == nil then -- only if we still don't have data for this language
-- Note: we cannot check more than 20 languages at once, then MediaWiki raises an error.
-- So this test only runs on the Lua console, where you can update the tables at top.
ok, value = pcall(function() return tostring(mw.language.new(lang):isRTL()) end)
mw.log("Missing direction for language '" .. lang .. "', MediaWiki returns '" .. value .. "'")
end
end
-- Sort and deduplicate language code values (by scanning backward) for data cleanup
table.sort(rtlLangs)
table.sort(ltrLangs)
for i, lang in revipairs(rtlLangs) do
if rtlLangs[i - 1] == rtlLangs[i] then table.remove(rtlLangs, i) end
end
for i, lang in revipairs(ltrLangs) do
if ltrLangs[i - 1] == ltrLangs[i] then table.remove(ltrLangs, i) end
end
-- Final presentation of current lists, sorted and deduplicated
mw.log("local rtlLangs = { '" .. table.concat(rtlLangs, "', '") .. "' }")
mw.log("local ltrLangs = { '" .. table.concat(ltrLangs, "', '") .. "' }")
return true
end
--[==[ Type this in the Lua console to run tests:
=p[true]()
--]==]
return p