-- .base
-- .region
-- .script
-- .suggest
-- .year
-- .extension
-- .other
-- .n
local tags = mw.text.split( ask, "-" )
local s = tags[ 1 ]
local r
if s:match( "^%a%a%a?$" ) then
r = { base = s:lower,
legal = true,
n = #tags }
for i = 2, r.n do
s = tags[ i ]
if #s == 2 then
if r.region or not s:match( "%a%a" ) then
r.legal = false
else
r.region = s:upper
end
elseif #s == 4 then
if s:match( "%a%a%a%a" ) then
r.legal = ( not r.script )
r.script = s:sub( 1, 1 ):upper ..
s:sub( 2 ):lower
elseif s:match( "20%d%d" ) or
s:match( "1%d%d%d" ) then
r.legal = ( not r.year )
r.year = s
else
r.legal = false
end
elseif #s == 3 then
if r.extlang or not s:match( "%a%a%a" ) then
r.legal = false
else
r.extlang = s:lower
end
elseif #s == 1 then
s = s:lower
if s:match( "[tux]" ) then
r.extension = s
for k = i + 1, r.n do
s = tags[ k ]
if s:match( "^%w+$" ) then
r.extension = string.format( "%s-%s",
r.extension, s )
else
r.legal = false
end
end -- for k
else
r.legal = false
end
break -- for i
else
r.legal = ( not r.other ) and
s:match( "%a%a%a" )
r.other = s:lower
end
if not r.legal then
break -- for i
end
end -- for i
if r.legal then
r.suggest = Multilingual.fix( r.base )
if r.suggest then
r.legal = false
end
end
else
r = { legal = false }
end
if not r.legal then
local cnf = fetch( "Multilingual", "config" )
if cnf and type( cnf.scream ) == "string" then
r.scream = cnf.scream
end
end
return r
end -- Multilingual.getLang  
Multilingual.getName = function ( ask, alien )
-- Which name is assigned to this language code?
-- Precondition:
-- ask -- language code
-- alien -- language of the answer
-- -- nil, false, "*": native
-- -- "!": current project
-- -- any valid code
-- Postcondition:
-- Returns string, or false
local r
if ask then
local slang = alien
local tLang
if slang then
if slang == "*" then
slang = Multilingual.fair( ask )
elseif slang == "!" then
slang = favorites[ 1 ]
else
slang = Multilingual.fair( slang )
end
else
slang = Multilingual.fair( ask )
end
if not slang then
slang = ask or "?????"
end
slang = slang:lower
tLang = fetch( "Multilingual", "names" )
if tLang then
tLang = tLang[ slang ]
if tLang then
r = tLang[ ask ]
end
end
if not r then
if not Multilingual.ext.tMW then
Multilingual.ext.tMW = { }
end
tLang = Multilingual.ext.tMW[ slang ]
if tLang == nil then
tLang = mw.language.fetchLanguageNames( slang )
if tLang then
Multilingual.ext.tMW[ slang ] = tLang
else
Multilingual.ext.tMW[ slang ] = false
end
end
if tLang then
r = tLang[ ask ]
end
end
if not r then
r = mw.language.fetchLanguageName( ask:lower, slang )
if r == "" then
r = false
end
end
else
r = false
end
return r
end -- Multilingual.getName  
Multilingual.i18n = function ( available, alt, )
-- Select translatable message
-- Precondition:
-- available -- table, with mapping language code ./. text
-- alt -- string|nil|false, with fallback text
-- -- , if available
-- Returns
-- 1. string|nil|false, with selected message
-- 2. string|nil|false, with language code
local r1, r2
if type( available ) == "table" then
local codes = { }
local trsl = { }
local slang
for k, v in pairs( available ) do
if type( k ) == "string" and
type( v ) == "string" then
slang = mw.text.trim( k:lower )
table.insert( codes, slang )
trsl[ slang ] = v
end
end -- for k, v
slang = Multilingual.userLang( codes, )
if slang and trsl[ slang ] then
r1 = mw.text.trim( trsl[ slang ] )
if r1 == "" then
r1 = false
else
r2 = slang
end
end
end
if not r1 and type( alt ) == "string" then
r1 = mw.text.trim( alt )
if r1 == "" then
r1 = false
end
end
return r1, r2
end -- Multilingual.i18n  
Multilingual.int = function ( access, alien, apply )
-- Translated system message
-- Precondition:
-- access -- message ID
-- alien -- language code
-- apply -- nil, or sequence table with parameters $1, $2, ...
-- Postcondition:
-- Returns string, or false
local o = mw.message.new( access )
local r
if o:exists then
if type( alien ) == "string" then
o:inLanguage( alien:lower )
end
if type( apply ) == "table" then
o:params( apply )
end
r = o:plain
end
return r or false
end -- Multilingual.int  
Multilingual.isLang = function ( ask, additional )
-- Could this be an ISO language code?
-- Precondition:
-- ask -- language code
-- additional -- true, if Wiki codes like "simple" permitted
-- Postcondition:
-- Returns boolean
local r, s
if additional then
s = ask
else
s = Multilingual.getBase( ask )
end
if s then
r = mw.language.isKnownLanguageTag( s )
if r then
r = not Multilingual.fix( s )
elseif additional then
r = Multilingual.exotic[ s ] or false
end
else
r = false
end
return r
end -- Multilingual.isLang  
Multilingual.isLangWiki = function ( ask )
-- Could this be a Wiki language version?
-- Precondition:
-- ask -- language version specifier
-- Postcondition:
-- Returns boolean
local r
local s = Multilingual.getBase( ask )
if s then
r = mw.language.isSupportedLanguage( s ) or
Multilingual.exotic[ ask ]
else
r = false
end
return r
end -- Multilingual.isLangWiki  
Multilingual.isMinusculable = function ( ask, assigned )
-- Could this language name become downcased?
-- Precondition:
-- ask -- language code, or nil
-- assigned -- language name, or nil
-- Postcondition:
-- Returns boolean
local r = true
if ask then
local cnf = fetch( "Multilingual", "config" )
if cnf then
local s = string.format( " %s ", ask:lower )
if type( cnf.stopMinusculization ) == "string"
and cnf.stopMinusculization:find( s, 1, true ) then
r = false
end
if r and assigned
and type( cnf.seekMinusculization ) == "string"
and cnf.seekMinusculization:find( s, 1, true )
and type( cnf.scanMinusculization ) == "string" then
local scan = assigned:gsub( "[%(%)]", " " ) .. " "
if not scan:find( cnf.scanMinusculization ) then
r = false
end
end
end
end
return r
end -- Multilingual.isMinusculable  
Multilingual.isRTL = function ( ask )
-- Check whether language is written -to-
-- Precondition:
-- ask -- string, with language (or script) code
-- Returns true, if -to-
local r
Multilingual.rtl = Multilingual.rtl or { }
r = Multilingual.rtl[ ask ]
if type( r ) ~= "boolean" then
local bib = fetch( "ISO15924" )
if type( bib ) == "table" and
type( bib.isRTL ) == "function" then
r = bib.isRTL( ask )
else
r = mw.language.new( ask ):isRTL
end
Multilingual.rtl[ ask ] = r
end
return r
end -- Multilingual.isRTL  
Multilingual.message = function ( arglist, )
-- Show text in best match of user language like system message
-- Precondition:
-- arglist -- template arguments
-- -- , if available
-- Postcondition:
-- Returns string with appropriate text
local r
if type( arglist ) == "table" then
local t = { }
local m, p, save
for k, v in pairs( arglist ) do
if type( k ) == "string" and
type( v ) == "string" then
v = mw.text.trim( v )
if v ~= "" then
if k:match( "^%l%l" ) then
t[ k ] = v
elseif k:match( "^%$%d$" ) and k ~= "$0" then
p = p or { }
k = tonumber( k:match( "^%$(%d)$" ) )
p[ k ] = v
if not m or k > m then
m = k
end
end
end
end
end -- for k, v
if type( arglist[ "-" ] ) == "string" then
save = arglist[ arglist[ "-" ] ]
end
r = Multilingual.i18n( t, save, )
if p and r and r:find( "$", 1, true ) then
t = { }
for i = 1, m do
t[ i ] = p[ i ] or ""
end -- for i
r = mw.message.newRawMessage( r, t ):plain
end
end
return r or ""
end -- Multilingual.message  
Multilingual.sitelink = function ( all, )
-- Make link at local or other site with optimal linktext translation
-- Precondition:
-- all -- string or table or number, item ID or entity
-- -- , if available
-- Postcondition:
-- Returns string with any helpful internal link, or plain text
local s = type( all )
local object, r
if s == "table" then
object = all
elseif s == "string" then
object = mw.wikibase.getEntity( all )
elseif s == "number" then
object = mw.wikibase.getEntity( string.format( "Q%d", all ) )
end
if type( object ) == "table" then
local collection = object.sitelinks
local entry
s = false
if type( collection ) == "table" then
Multilingual.site = Multilingual.site or
mw.wikibase.getGlobalSiteId
entry = collection[ Multilingual.site ]
if entry then