local Multilingual = { suite = "Multilingual",
serial = "2020-12-10",
item = 47541920,
globals = { ISO15924 = 71584769,
WLink = 19363224 }
}
--[=[
Utilities for multilingual texts and ISO 639 (BCP47) issues etc.
- fair
- fallback
- findCode
- fix
- format
- getBase
- getLang
- getName
- i18n
- int
- isLang
- isLangWiki
- isMinusculable
- isRTL
- message
- sitelink
- tabData
- userLang
- userLangCode
- wikibase
- failsafe
loadData: Multilingual/config Multilingual/names
]=]
local Failsafe = Multilingual
local GlobalMod = Multilingual
local GlobalData = Multilingual
local User = { sniffer = "showpreview" }
Multilingual.globals.Multilingual = Multilingual.item  
Multilingual.exotic = { simple = true,
no = true }
Multilingual.prefer = { cs = true,
de = true,
en = true,
es = true,
fr = true,
it = true,
nl = true,
pt = true,
ru = true,
sv = true }  
local foreignModule = function ( access, advanced, append, alt, alert )
-- Fetch global module
-- Precondition:
-- access -- string, with name of base module
-- advanced -- true, for require; else mw.loadData
-- append -- string, with subpage part, if any; or false
-- alt -- number, of wikidata item of root; or false
-- alert -- true, for throwing error on data problem
-- Postcondition:
-- Returns whatever, probably table
-- 2020-01-01
local storage = access
local finer = function
if append then
storage = string.format( "%s/%s",
storage,
append )
end
end
local fun, lucky, r, suited
if advanced then
fun = require
else
fun = mw.loadData
end
GlobalMod.globalModules = GlobalMod.globalModules or { }
suited = GlobalMod.globalModules[ access ]
if not suited then
finer
lucky, r = pcall( fun, "Module:" .. storage )
end
if not lucky then
if not suited and
type( alt ) == "number" and
alt > 0 then
suited = string.format( "Q%d", alt )
suited = mw.wikibase.getSitelink( suited )
GlobalMod.globalModules[ access ] = suited or true
end
if type( suited ) == "string" then
storage = suited
finer
lucky, r = pcall( fun, storage )
end
if not lucky and alert then
error( "Missing or invalid page: " .. storage )
end
end
return r
end -- foreignModule  
local fetchData = function ( access )
-- Retrieve translated keyword from commons:Data:****.tab
-- Precondition:
-- access -- string, with page identification on Commons
-- Returns table, with data, or string, with error message
-- 2019-12-05
local storage = access
local r
if type( storage ) == "string" then
local s
storage = mw.text.trim( storage )
s = storage:lower
if s:sub( 1, 2 ) == "c:" then
storage = mw.text.trim( storage:sub( 3 ) )
s = storage:lower
elseif s:sub( 1, 8 ) == "commons:" then
storage = mw.text.trim( storage:sub( 9 ) )
s = storage:lower
end
if s:sub( 1, 5 ) == "data:" then
storage = mw.text.trim( storage:sub( 6 ) )
s = storage:lower
end
if s == "" or s == ".tab" then
storage = false
elseif s:sub( -4 ) == ".tab" then
storage = storage:sub( 1, -5 ) .. ".tab"
else
storage = storage .. ".tab"
end
end
if type( storage ) == "string" then
local data
if type( GlobalData.TabDATA ) ~= "table" then
GlobalData.TabDATA = { }
end
data = GlobalData.TabDATA[ storage ]
if data then
r = data
else
local lucky
lucky, data = pcall( mw.ext.data.get, storage, "_" )
if type( data ) == "table" then
data = data.data
if type( data ) == "table" then
GlobalData.TabDATA[ storage ] = data
else
r = string.format( "%s %s%s",
"INVALID Data:*.tab",
"commons:Data:",
storage )
end
else
r = "BAD PAGE Data:*.tab &#8211; commons:" .. storage
end
if r then
GlobalData.TabDATA[ storage ] = r
data = false
else
r = data
end
end
else
r = "BAD PAGE commons:Data:*.tab"
end
return r
end -- fetchData  
local favorites = function
-- Provide fallback codes
-- Postcondition:
-- Returns table with sequence of preferred languages
-- * ahead elements
-- * user (not yet accessible)
-- * page content language (not yet accessible)
-- * page name subpage
-- * project
-- * en
local r = Multilingual.polyglott
if not r then
local self = mw.language.getContentLanguage:getCode:lower
local sub = mw.title.getCurrentTitle.subpageText
local f = function ( add )
local s = add
for i = 1, #r do
if r[ i ] == s then
s = false
break -- for i
end
end -- for i
if s then
table.insert( r, s )
end
end
r = { }
if sub:find( "/", 2, true ) then
sub = sub:match( "/(%l%l%l?)$" )
if sub then
table.insert( r, sub )
end
elseif sub:find( "^%l%l%l?%-?%a?%a?%a?%a?$" ) and
mw.language.isSupportedLanguage( sub ) then
table.insert( r, sub )
end
f( self )
f( "en" )
Multilingual.polyglott = r
end
return r
end -- favorites  
local feasible = function ( ask, accept )
-- Is ask to be supported by application?
-- Precondition:
-- ask -- lowercase code
-- accept -- sequence table, with offered lowercase codes
-- Postcondition:
-- nil, or true
local r
for i = 1, #accept do
if accept[ i ] == ask then
r = true
break -- for i
end
end -- for i
return r
end -- feasible  
local fetch = function ( access, append )
-- Attach config or library module
-- Precondition:
-- access -- module title
-- append -- string, with subpage part of this; or false
-- Postcondition:
-- Returns: table, with library, or false
local got, sign
if append then
sign = string.format( "%s/%s", access, append )
else
sign = access
end
if type( Multilingual.ext ) ~= "table" then
Multilingual.ext = { }
end
got = Multilingual.ext[ sign ]
if not got and got ~= false then
local global = Multilingual.globals[ access ]
local lib = ( not append or append == "config" )
got = foreignModule( access, lib, append, global )
if type( got ) == "table" then
if lib then
local startup = got[ access ]
if type( startup ) == "function" then
got = startup
end
end
else
got = false
end
Multilingual.ext[ sign ] = got
end
return got
end -- fetch  
local fetchISO639 = function ( access )
-- Retrieve table from commons:Data:ISO639/***.tab
-- Precondition:
-- access -- string, with subpage identification
-- Postcondition:
-- Returns table, with data, even empty
local r
if type( Multilingual.iso639 ) ~= "table" then
Multilingual.iso639 = { }
end
r = Multilingual.iso639[ access ]
if type( r ) == "nil" then
local raw = fetchData( "ISO639/" .. access )
if type( raw ) == "table" then
local t
r = { }
for i = 1, #raw do
t = raw[ i ]
if type( t ) == "table" and
type( t[ 1 ] ) == "string" and
type( t[ 2 ] ) == "string" then
r[ t[ 1 ] ] = t[ 2 ]
else
break -- for i
end
end -- for i
else
r = false
end
Multilingual.iso639[ access ] = r
end
return r or { }
end -- fetchISO639  
local fill = function ( access, alien, )
-- Expand language name template
-- Precondition:
-- access -- string, with language code
-- alien -- language code for which to be generated
-- -- , if available
-- Postcondition:
-- Returns string
local template = Multilingual.tmplLang
local r
if type( template ) ~= "table" then
local cnf = fetch( "Multilingual", "config" )
if cnf then
template = cnf.tmplLang
end
end
if type( template ) == "table" then
local source = template.title
local f, lucky, s
Multilingual.tmplLang = template
if type( source ) ~= "string" and
type( template.namePat ) == "string" and
template.namePat:find( "%s", 1, true ) then
source = string.format( template.namePat, access )
end
if type( source ) == "string" then
if not Multilingual. then
if then
Multilingual. =
else
Multilingual. = mw.getCurrentFrame
end
end
f = function ( a )
return Multilingual.:expandTemplate{ title = a }
end
lucky, s = pcall( f, source )
if lucky then
r = s
end
end
end
return r
end -- fill  
local find = function ( ask, alien )
-- Derive language code from name
-- Precondition:
-- ask -- language name, downcased
-- alien -- language code of ask
-- Postcondition:
-- nil, or string
local codes = mw.language.fetchLanguageNames( alien, "all" )
local r
for k, v in pairs( codes ) do
if mw.ustring.lower( v ) == ask then
r = k
break -- for k, v
end
end -- for k, v
if not r then
r = Multilingual.fair( ask )
end
return r
end -- find  
local fold = function
-- Merge template and #invoke arglist
-- Precondition:
-- -- template
-- Postcondition:
-- table, with combined arglist
local r = { }