Modul:Multilingual: Unterschied zwischen den Versionen
w>PerfektesChaos 2019-11-30 |
K 48 Versionen von skanwiki:Modul:Multilingual importiert |
||
| (8 dazwischenliegende Versionen von 4 Benutzern werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
local Multilingual = { suite = "Multilingual", | local Multilingual = { suite = "Multilingual", | ||
serial = " | serial = "2020-12-10", | ||
item = 47541920, | item = 47541920, | ||
globals = { ISO15924 = 71584769, | globals = { ISO15924 = 71584769, | ||
| Zeile 30: | Zeile 30: | ||
loadData: Multilingual/config Multilingual/names | loadData: Multilingual/config Multilingual/names | ||
]=] | ]=] | ||
local Failsafe = Multilingual | local Failsafe = Multilingual | ||
local | local GlobalMod = Multilingual | ||
local User | local GlobalData = Multilingual | ||
local User = { sniffer = "showpreview" } | |||
Multilingual.globals.Multilingual = Multilingual.item | Multilingual.globals.Multilingual = Multilingual.item | ||
Multilingual.exotic = { simple = true, | Multilingual.exotic = { simple = true, | ||
no = 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 } | |||
| Zeile 68: | Zeile 63: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns whatever, probably table | -- Returns whatever, probably table | ||
-- | -- 2020-01-01 | ||
local storage = access | local storage = access | ||
local finer = function () | local finer = function () | ||
| Zeile 103: | Zeile 98: | ||
end | end | ||
if not lucky and alert then | if not lucky and alert then | ||
error( "Missing or invalid page: " .. storage | error( "Missing or invalid page: " .. storage ) | ||
end | end | ||
end | end | ||
| Zeile 111: | Zeile 106: | ||
local | 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 | |||
local r = | 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" | |||
if | else | ||
storage = storage .. ".tab" | |||
end | end | ||
end | end | ||
return r | if type( storage ) == "string" then | ||
end -- | 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 – 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 | local favorites = function () | ||
-- | -- Provide fallback codes | ||
-- Postcondition: | -- Postcondition: | ||
-- | -- Returns table with sequence of preferred languages | ||
local r | -- * ahead elements | ||
for i = 1, # | -- * 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 | end | ||
end -- for i | f( self ) | ||
return r | 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() | end -- feasible() | ||
| Zeile 210: | Zeile 278: | ||
local | local fetchISO639 = function ( access ) | ||
-- | -- Retrieve table from commons:Data:ISO639/***.tab | ||
-- Precondition: | -- Precondition: | ||
-- access -- string, with language code | -- 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, frame ) | |||
-- Expand language name template | |||
-- Precondition: | |||
-- access -- string, with language code | |||
-- alien -- language code for which to be generated | -- alien -- language code for which to be generated | ||
-- frame -- frame, if available | -- frame -- frame, if available | ||
| Zeile 331: | Zeile 435: | ||
end | end | ||
if User.sin then | if User.sin then | ||
local order = { } | local order = { } | ||
local post = { } | local post = { } | ||
local three = { } | |||
local unfold = { } | |||
local s, sin | local s, sin | ||
for i = 1, #accept do | for i = 1, #accept do | ||
s = accept[ i ] | s = accept[ i ] | ||
if not User.trials[ s ] then | if not User.trials[ s ] then | ||
if s:find( "-", 3, true ) then | if #s > 2 then | ||
if s:find( "-", 3, true ) then | |||
table.insert( unfold, s ) | |||
else | |||
table.insert( three, s ) | |||
end | |||
else | else | ||
if Multilingual.prefer[ s ] then | |||
table.insert( order, s ) | |||
else | |||
table.insert( post, s ) | |||
end | |||
end | end | ||
end | end | ||
| Zeile 346: | Zeile 460: | ||
for i = 1, #post do | for i = 1, #post do | ||
table.insert( order, post[ i ] ) | table.insert( order, post[ i ] ) | ||
end -- for i | |||
for i = 1, #three do | |||
table.insert( order, three[ i ] ) | |||
end -- for i | |||
for i = 1, #unfold do | |||
table.insert( order, unfold[ i ] ) | |||
end -- for i | end -- for i | ||
for i = 1, #order do | for i = 1, #order do | ||
| Zeile 404: | Zeile 524: | ||
-- Precondition: | -- Precondition: | ||
-- able -- language version specifier to be supported | -- able -- language version specifier to be supported | ||
-- another -- language specifier of a possible replacement | -- another -- language specifier of a possible replacement, | ||
-- or not to retrieve a fallback table | |||
-- Postcondition: | -- Postcondition: | ||
-- Returns boolean | -- Returns boolean, or table with fallback codes | ||
local r | local r | ||
if type( able ) == "string" and | if type( able ) == "string" and #able > 0 then | ||
if type( another ) == "string" and #another > 0 then | |||
if able == another then | |||
r = true | r = true | ||
else | |||
local s = Multilingual.getBase( able ) | |||
if s == another then | |||
r = true | |||
else | |||
local others = mw.language.getFallbacksFor( s ) | |||
r = feasible( another, others ) | |||
end | |||
end | |||
else | else | ||
local s = Multilingual.getBase( able ) | local s = Multilingual.getBase( able ) | ||
if s | if s then | ||
r | r = mw.language.getFallbacksFor( s ) | ||
if r[ 1 ] == "en" then | |||
local d = fetchISO639( "fallback" ) | |||
r = | if type( d ) == "table" and | ||
type( d[ s ] ) == "string" then | |||
r = mw.text.split( d[ s ], "|" ) | |||
table.insert( r, "en" ) | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Zeile 468: | Zeile 603: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string with correction, or false if no problem known | -- Returns string with correction, or false if no problem known | ||
local r = fetchISO639( "correction" )[ attempt:lower() ] | |||
return r or false | |||
end -- Multilingual.fix() | end -- Multilingual.fix() | ||
| Zeile 1.039: | Zeile 1.175: | ||
Multilingual.tabData = function ( access, | Multilingual.tabData = function ( access, at, alt, frame ) | ||
-- | -- Retrieve translated keyword from commons:Data:****.tab | ||
-- Precondition: | -- Precondition: | ||
-- access -- string, with page identification on Commons | -- access -- string, with page identification on Commons | ||
-- | -- at -- string, with keyword | ||
-- alt -- string|nil|false, with fallback text | -- alt -- string|nil|false, with fallback text | ||
-- frame -- frame, if available | -- frame -- frame, if available | ||
-- Returns | -- Returns | ||
-- 1. string|nil|false, with selected message | -- 1. string|nil|false, with selected message | ||
-- 2. | -- 2. language code, or "error" | ||
local | local data = fetchData( access ) | ||
local | local r1, r2 | ||
if | if type( data ) == "table" then | ||
if type( at ) == "string" then | |||
local seek = mw.text.trim( at ) | |||
if seek == "" then | |||
r1 = "EMPTY Multilingual.tabData key" | |||
else | |||
local e, poly | |||
for i = 1, #data do | |||
e = data[ i ] | |||
if type( e ) == "table" then | |||
if type( | |||
local seek = mw.text.trim( | |||
if seek == "" then | |||
r1 = "EMPTY Multilingual.tabData key" | |||
else | |||
local e, poly | |||
for i = 1, #data do | |||
e = data[ i ] | |||
if type( e ) == "table" then | |||
if e[ 1 ] == seek then | if e[ 1 ] == seek then | ||
if type( e[ 2 ] ) == "table" then | if type( e[ 2 ] ) == "table" then | ||
| Zeile 1.135: | Zeile 1.219: | ||
r1 = "INVALID Multilingual.tabData key" | r1 = "INVALID Multilingual.tabData key" | ||
end | end | ||
else | |||
r1 = data | |||
end | end | ||
if r1 then | if r1 then | ||
| Zeile 1.140: | Zeile 1.226: | ||
elseif data then | elseif data then | ||
r1, r2 = Multilingual.i18n( data, alt, frame ) | r1, r2 = Multilingual.i18n( data, alt, frame ) | ||
r2 = r2 or "error" | |||
end | end | ||
return r1, r2 | return r1, r2 | ||
| Zeile 1.266: | Zeile 1.353: | ||
-- Retrieve versioning and check for compliance | -- Retrieve versioning and check for compliance | ||
-- Precondition: | -- Precondition: | ||
-- atleast -- string, with required version | -- atleast -- string, with required version | ||
-- | -- or wikidata|item|~|@ or false | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string -- with queried version, also if problem | -- Returns string -- with queried version/item, also if problem | ||
-- false -- if appropriate | -- false -- if appropriate | ||
-- | -- 2020-08-17 | ||
local since = atleast | local since = atleast | ||
local last = ( since == "~" ) | |||
local linked = ( since == "@" ) | |||
local link = ( since == "item" ) | |||
local r | local r | ||
if last or since == "wikidata" then | if last or link or linked or since == "wikidata" then | ||
local item = Failsafe.item | local item = Failsafe.item | ||
since = false | since = false | ||
if type( item ) == "number" and item > 0 then | if type( item ) == "number" and item > 0 then | ||
local | local suited = string.format( "Q%d", item ) | ||
if link then | |||
r = suited | |||
else | |||
local entity = mw.wikibase.getEntity( suited ) | |||
if type( entity ) == "table" then | |||
local seek = Failsafe.serialProperty or "P348" | |||
local vsn = entity:formatPropertyValues( seek ) | |||
if type( vsn ) == "table" and | |||
r = false | type( vsn.value ) == "string" and | ||
vsn.value ~= "" then | |||
r = vsn.value | if last and vsn.value == Failsafe.serial then | ||
r = false | |||
elseif linked then | |||
if mw.title.getCurrentTitle().prefixedText | |||
== mw.wikibase.getSitelink( suited ) then | |||
r = false | |||
else | |||
r = suited | |||
end | |||
else | |||
r = vsn.value | |||
end | |||
end | end | ||
end | end | ||
| Zeile 1.326: | Zeile 1.426: | ||
-- 1 -- language version specifier to be supported | -- 1 -- language version specifier to be supported | ||
-- 2 -- language specifier of a possible replacement | -- 2 -- language specifier of a possible replacement | ||
local s1 | local s1 = mw.text.trim( frame.args[ 1 ] or "" ) | ||
local s2 | local s2 = mw.text.trim( frame.args[ 2 ] or "" ) | ||
local r = Multilingual.fallback( s1, s2 ) | |||
if type( r ) == "table" then | |||
r = r[ 1 ] | |||
else | |||
r = r and "1" or "" | |||
end | |||
return r | |||
end -- p.fallback | end -- p.fallback | ||
| Zeile 1.521: | Zeile 1.627: | ||
local seek = frame.args[ 2 ] | local seek = frame.args[ 2 ] | ||
local salt = frame.args.alt | local salt = frame.args.alt | ||
local r = Multilingual.tabData( suite, seek, salt, frame ) | |||
return r | |||
end -- p.tabData | end -- p.tabData | ||