Modul:Vorlage:lang
Die Dokumentation für dieses Modul kann unter Modul:Vorlage:lang/Doku erstellt werden
local Export = { serial = "2017-04-24", suite = "lang" } --[=[ Vorlage:lang und Sprachnamenvorlagen usw. unterstützen ]=] local Config = { errCat = false, errClass = "error_lang", errClasses = false, errHide = true, errNS = false, errDoubled = { en = "Doubled:", de = "Doppelangabe:" }, errInvalid = { en = "Invalid:", de = "Ungültig:" }, errMissing = { en = "Missing parameter", de = "Parameter fehlt" }, errUnkown = { en = "Unkown parameter:", de = "Parameter unbekannt:" }, iast = { class = "IAST", lang = { as = true, bn = true, gu = true, hi = true, kn = true, kok = true, ks = true, mai = true, ml = true, mr = true, ne = true, ["or"] = true, pa = true, sa = true, sd = true, ta = true, te = true, ur = true, und = true }, support = "International Alphabet of Sanskrit Transliteration" }, ipa = "IPA", params = { "Text1", "Audio", "IPA", "class", "style", "nachgestellt", "demo", "NoCat" }, percents = { Arab = 120, Hebr = 115 }, orderOther = { grc = 1, hbo = 2, la = 3, en = 9 }, owns = { de = "‚%s‘" }, tmplAudio = false, -- template for media player link tmplOtherLang = false, -- template for otherLanguaguage link transcriptions = { Arab = { en = "[[Arabic alphabet|arabic]]", de = "[[Arabisches Alphabet|arabisch]]" }, Cyrl = { en = "[[Cyrillic alphabets|cyrillic]]", de = "[[Kyrillisches Alphabet|kyrillisch]]" } } } local function Fetch( ask ) -- Fetch module -- Parameter: -- ask -- string, with name -- "Multilingual" -- "TemplUtl" -- Returns string, with error message, if not available local r if type( Config[ ask ] ) ~= "table" then local lucky, g = pcall( require, "Module:" .. ask ) if type( g ) == "table" then g = g[ ask ] if type( g ) == "function" then Config[ ask ] = g() end g = false else g = false end if type( Config[ ask ] ) ~= "table" then if not g then g = "Invalid library *** Module:" .. ask end g = mw.html.create( "span" ) :attr( "class", "error" ) :wikitext( g ) r = tostring( g ) end end return r end -- Fetch() local function facet( assign ) -- Format language name -- apply -- string, with language name, might be linked -- Returns string local e = mw.html.create( "span" ) :css( { ["font-style"] = "normal", ["font-weight"] = "normal" } ) :wikitext( assign ) return tostring( e ) end -- facet() local function facility() -- Fetch current site language -- Returns language code if not Config.standard then Config.standard = mw.language.getContentLanguage():getCode() end return Config.standard end -- facility() local function factory( apply ) -- Localization of messages -- apply -- string, with message key -- Returns message text; at least english local r entry = Config[ apply ] if entry then r = entry[ facility() ] if not r then r = entry.en end else r = string.format( "<span class=\"error\">????.%s.????</span>", apply ) end return r end -- factory() local function faculty( adjust ) -- Test template arg for boolean -- adjust -- string or nil -- Returns boolean local s = type( adjust ) local r if s == "string" then r = mw.text.trim( adjust ) r = ( r ~= "" and r ~= "0" ) elseif s == "boolean" then r = adjust else r = false end return r end -- faculty() local function family() -- attempt to load local config if not Config.data then local lucky, sub if not Config.frame then Config.frame = mw.getCurrentFrame() end sub = string.format( "%s/config", Config.frame:getTitle() ) lucky, Config.data = pcall( mw.loadData, sub ) if type( Config.data ) == "table" then for k, v in pairs( Config.data ) do Config[ k ] = v end -- for k, v else Config.data = sub .. " not found" end end end -- family() local function fault( alert, about ) -- Format message with class="error" or similar -- alert -- string, with message key -- about -- string, with explanation -- Returns message with markup local story = factory( alert ) local err = mw.html.create( "span" ) local r err:addClass( Config.errClass ) :css( { ["margin-left"] = "1em", ["margin-right"] = "1em" } ) family() if not Config.frame then Config.frame = mw.getCurrentFrame() end story = string.format( "[[%s]] – %s", Config.frame:getParent():getTitle(), story ) if about then story = string.format( "%s %s", story, about ) end if Config.errClasses then err:addClass( Config.errClasses ) end err:wikitext( story ) r = Fetch( "TemplUtl" ) if Config.TemplUtl then r = Config.TemplUtl.failure( tostring( err ), not Config.errHide ) end if type( Config.errCat ) == "string" then if Config.errNS then local ns = mw.title.getCurrentTitle().namespace local st = type( Config.errNS ) if st == "string" then local space = string.format( ".*%%s%d%%s.*", ns ) local spaces = string.format( " %s ", Config.errNS ) if spaces:match( space ) then Config.errNS = false end elseif st == "table" then for i = 1, #Config.errNS do if Config.errNS[ i ] == ns then Config.errNS = false break -- for i end end -- for i end end if not Config.errNS then r = string.format( "%s[[Category:%s]]", r, Config.errCat ) end end return r end -- fault() local function fiast( apply ) -- Link to IAST if supported -- apply -- string, with text -- Returns string local r = apply if Config.iast.support then r = string.format( "[[%s|%s]]", Config.iast.support, r ) end return r end -- fiast() local function foreign( apply, acquire, advanced ) -- Format text in some language -- apply -- string, with text -- acquire -- string, with basic code of language -- advanced -- string, with full code of language -- Returns string, starting with comma local story = apply local ltr, r if advanced == acquire then ltr = not mw.language.new( acquire ):isRTL() else ltr = not ( advanced:match( "-Arab$" ) or advanced:match( "-Hebr$" ) ) end if ltr then local e = mw.html.create( "span" ) :attr( "lang", advanced ) local p = { ["font-weight"] = "normal" } local s = "normal" if acquire == Config.standard and Config.owns[ acquire ] then story = string.format( Config.owns[ acquire ], apply ) elseif advanced == acquire or advanced == acquire .. "-Latn" then s = "italic" end p["font-style"] = s e:css( p ) :wikitext( story ) r = tostring( e ) else r = frame():expandTemplate{ title = advanced, args = { [1] = apply } } end return r end -- foreign() local function foreigns( aliens ) -- Create list of translations -- aliens -- sequence table, with assignment tables -- Returns string, starting with comma local r = "" local pars = { } local s, t family() facility() Fetch( "Multilingual" ) for i = 1, #aliens do t = aliens[ i ] if t.short == Config.standard then t.n = 0 else local o = Config.orderOther[ t.short ] if o then t.n = o end end end -- for i table.sort( aliens, function ( a1, a2 ) return ( a1.n < a2.n ) end ) if not Config.frame then Config.frame = mw.getCurrentFrame() end for i = 1, #aliens do t = aliens[ i ] if Config.tmplOtherLang and t.n > 0 then s = string.format( Config.tmplOtherLang.namePat, t.short ) pars[ Config.tmplOtherLang.textpar ] = t.story s = Config.frame:expandTemplate{ title = s, args = pars } else s = mw.language.fetchLanguageName( t.short, Config.standard ) if Config.Multilingual and Config.Multilingual.isMinusculable( s ) then s = mw.ustring.lower( mw.ustring.sub( s, 1, 1 ) ) .. mw.ustring.sub( s, 2 ) end s = string.format( "%s %s", facet( s ), foreign( t.story, t.short, t.slang ) ) end r = string.format( "%s, %s", r, s ) end -- for i return r end -- foreigns() local function frame() -- Fetch current frame -- Returns frame if not Config.frame then Config.frame = mw.getCurrentFrame() end return Config.frame end -- frame() local function frontend( action, argsF, argsT, about ) -- Template service -- action -- string, "flat" or "full" etc. -- argsF -- table, with #invoke parameters, or false -- argsT -- table, with template parameters -- about -- string or nil, invocation name -- Returns frame local lucky, r lucky, r = pcall( Export[ action ], argsF, argsT ) if not lucky then local e = mw.html.create( "span" ) :attr( "class", "error" ) if about then r = string.format( "{{%s}} %s", about, r ) end e:wikitext( r ) r = tostring( e ) end return r end -- frontend() local function frontier( frame, action ) -- Template transclusion -- frame -- object -- action -- string, "flat" or "full" etc. -- Returns appropriate string Config.frame = frame return frontend( action, frame.args, frame:getParent().args, frame:getTitle() ) end -- frontier() local function full( arglist ) -- Invocation of template -- arglist -- table, with parameters -- Returns appropriate string local r if arglist.Text1 then local slang = Config.slang local params, s if Config.scripting == "Latn" then if slang == facility() then arglist.style = false elseif not arglist.style then arglist.style = "font-style:italic" end if arglist.Text2 then arglist.Text2 = fault( "errInvalid", "Latn+2=" ) end elseif not Config.low and not arglist.style then arglist.style = "font-style:normal" end if Config.state then slang = string.format( "%s-%s", slang, Config.state ) end if Config.scripting then slang = string.format( "%s-%s", slang, Config.scripting ) end r = Export.format( slang, arglist.Text1, arglist.style, arglist.Audio, arglist.class ) if arglist.Text2 then params = { lang = string.format( "%s-Latn", Config.slang ), style = "font-style:italic" } s = mw.text.tag( "span", params, arglist.Text2 ) r = string.format( "%s %s", r, s ) end if arglist.sanskrit then params = { lang = string.format( "%s-Latn", Config.slang ), style = "font-style:italic" } if Config.iast.class then params.class = Config.iast.class end s = arglist.sanskrit if arglist.Text2 then r = string.format( "%s, %s: ", r, fiast( "IAST" ) ) else r = r .. " " s = fiast( s ) end r = r .. mw.text.tag( "span", params, s ) end if not Config.low then if Config.scripting then s = arglist[ Config.scripting ] if s and faculty( s ) and not arglist.Text2 and Config.transcriptions[ Config.scripting ] then s = facility() s = Config.transcriptions[ Config.scripting ][ s ] elseif Config.service then s = facet( Config.service ) else s = "" end else s = Config.service or "" end if arglist.later then r = string.format( "%s (%s)", r, s ) else r = string.format( "%s %s", s, r ) end end if arglist.IPA then params = { [1] = arglist.IPA } s = frame():expandTemplate{ title = Config.ipa, args = params } r = string.format( "%s [%s]", r, s ) end if arglist.trsl then r = r .. foreigns( arglist.trsl ) end elseif arglist.Text2 then r = fault( "errInvalid", "2=" ) else if Config.sole then r = Config.sole else r = Config.service end end return r end -- full() local function furnish( argsF, argsT ) -- General entry point; basic argument consumption -- argsF -- table, with #invoke parameters, or false -- argsT -- table, with template parameters -- Returns appropriate string local r = { } local s if argsF then if argsF.errHide ~= nil then Config.errHide = faculty( argsF.errHide ) end Config.errCat = argsF.errCat Config.errClasses = argsF.errClasses Config.errNS = argsF.errNS if argsF.SUITABLE then local params = mw.text.split( argsF.SUITABLE, " ", true ) for k, v in pairs( params ) do table.insert( Config.params, v ) end -- for k, v end end if Config.scripting == "" then Config.scripting = false end if Config.scripting then table.insert( Config.params, Config.scripting ) end if type( argsT ) == "table" then local n = table.maxn( Config.params ) local unknown for k, v in pairs( argsT ) do s = type( k ) if s == "number" then if Config.low then k = k - 1 end if k <= 2 then if k == 0 then k = false else v = mw.text.trim( v ) if v == "" then v = false end if k == 1 then r.Text1 = v k = false elseif Config.scripting == "Latn" then k = "2" else r.Text2 = v k = false end end else k = tostring( k ) end elseif k:match( "^%l%l%l?-?" ) then s = k:match( "^(%l%l%l?)$" ) or k:match( "^(%l%l%l?)-%u%u$" ) or k:match( "^(%l%l%l?)-%u%l%l%l$" ) v = mw.text.trim( v ) if v == "" then v = false end if v and s and s ~= Config.slang and mw.language.isSupportedLanguage( s ) then if not r.trsl then r.trsl = { } end table.insert( r.trsl, { n = #r.trsl + 10, short = s, slang = k, story = v } ) k = false end elseif k == "IAST" then if Config.iast.lang[ Config.slang ] then v = mw.text.trim( v ) if v ~= "" then r.sanskrit = v end k = false end end if k then for i = 1, n do if Config.params[ i ] == k then if v ~= "" then r[ k ] = v end k = false break -- for i end end -- for i end if k then if not unknown then unknown = { } end table.insert( unknown, k ) end end -- for k, v if r.demo or faculty( r.NoCat ) then Config.errCat = 0 Config.errHide = false end r.later = faculty( r.nachgestellt ) if r.b and Config[ "OBSOLETING-bw" ] then if r.de then r = fault( "errDoubled", "'de=' und 'b='" ) else r.de = r.b end end if unknown then r = string.format( "'<code>%s</code>'", table.concat( unknown, " " ) ) r = fault( "errUnkown", r ) end end if type( r ) == "table" then r = full( r ) end return r end -- furnish() Export.failsafe = function ( assert ) local r if not assert or assert <= Export.serial then r = Export.serial else r = false end return r end -- Export.failsafe() Export.flat = function ( argsF, argsT, auxilary ) -- Invocation of basic language template -- argsF -- table, with #invoke parameters, or false -- argsT -- table, with template parameters -- auxilary -- Multilingual library, or false -- Returns appropriate string local r if type( auxilary ) == "table" then Config.Multilingual = auxilary else r = Fetch( "Multilingual" ) end if Config.Multilingual then local slang = argsT[ 1 ] local show = argsT[ 2 ] if slang then slang = mw.text.trim( slang ) if slang == "" then slang = false end end if show then show = mw.text.trim( show ) if show == "" then show = false end end if slang and show then local q = Config.Multilingual.getLang( slang ) if q then Config.low = true Config.slang = q.base Config.state = q.region Config.scripting = q.script r = furnish( argsF, argsT ) else local e = mw.html.create( "span" ) :attr( "lang", slang ) :wikitext( show ) r = tostring( e ) end else r = fault( "errMissing" ) if show then r = show .. r end end end return r end -- Export.flat() Export.fold = function ( argsF, argsT ) -- Invocation of RTL template -- argsF -- table, with #invoke parameters, or false -- argsT -- table, with template parameters -- Returns appropriate string, or nil local params = { argsT[ 1 ], argsT[ 2 ] } local r, s for i = 1, 2 do if params[ i ] then params[ i ] = mw.text.trim( params[ i ] ) if params[ i ] == "" then params[ i ] = false end end end -- for i if params[ 2 ] then s = params[ 2 ]:gsub( mw.ustring.char( 8206 ), "‎" ) :gsub( "�*8206;", "‎" ) :gsub( "�*200[Ee];", "‎" ) :gsub( mw.ustring.char( 8207 ), "‏" ) :gsub( "�*8207;", "‏" ) :gsub( "�*200[Ff];", "‏" ) if s:find( "&", 1, true ) then local shift = "^‏%s*" while s:match( shift ) do s = s:gsub( shift, "" ) end -- while shift = "%s*‎$" while s:match( shift ) do s = s:gsub( shift, "" ) end -- while if s == "" then s = false end end end if s then local bdi = mw.html.create( "bdi" ) :attr( "dir", "rtl" ) :attr( "lang", params[ 1 ] or "ar" ) :css( "unicode-bidi", "isolate" ) :wikitext( s ) local bdo = mw.html.create( "bdo" ) :attr( "dir", "ltr" ) :node( bdi ) if argsT.class and argsT.class ~= "" then bdi:addClass( argsT.class ) end if argsT.style and argsT.style ~= "" then bdi:cssText( argsT.style ) end r = tostring( bdo ) end return r end -- Export.fold() Export.format = function ( alien, apply, appear, audio, alike ) -- Markup foreign language text -- alien -- string, with language code -- apply -- string, with text -- appear -- string, with additional CSS, or nil -- audio -- string, with title of an audio file, or nil -- alike -- string, with additional class(es), or nil -- Returns appropriate string with HTML tag local params = { lang = alien } local r if appear then params.style = appear end if alike then params.class = alike end r = mw.text.tag( "span", params, apply ) family() if audio and Config.tmplAudio then params = { [ Config.tmplAudio.filepar ] = audio, [ Config.tmplAudio.textpar ] = r } r = frame():expandTemplate{ title = Config.tmplAudio.title, args = params } end return r end -- Export.format() Export.full = function ( argsF, argsT ) -- Invocation of language name template -- argsF -- table, with #invoke parameters, or false -- argsT -- table, with template parameters -- Returns appropriate string if argsF then Config.long = faculty( argsF.LONG ) Config.scripting = argsF.SCRIPTING Config.service = argsF.SERVICE Config.slang = argsF.CODE Config.sole = argsF.SOLE if Config["OBSOLETING-bw"] then table.insert( Config.params, "b" ) table.insert( Config.params, "w" ) end end Config.low = false return furnish( argsF, argsT ) end -- Export.full() -- Export local p = { } p.test = function ( action, argsF, argsT ) -- action -- string, "flat" or "full" etc. -- argsF -- table, with #invoke parameters, or false -- argsT -- table, with template parameters return frontend( action, argsF, argsT ) end -- p.test() p.feedIAST = function ( frame ) Config.frame = frame return Config.iast.lang[ frame.args[ 1 ] ] and "1" or "" end -- p.feedIAST() p.flat = function ( frame ) return frontier( frame, "flat" ) end -- p.flat() p.fold = function ( frame ) return frontier( frame, "fold" ) or "" end -- p.fold() p.full = function ( frame ) return frontier( frame, "full" ) end -- p.full() p.failsafe = function ( frame ) local s = type( frame ) local since if s == "table" then since = frame.args[ 1 ] elseif s == "string" then since = frame end if since then since = mw.text.trim( since ) if since == "" then since = false end end return Export.failsafe( since ) or "" end -- p.failsafe() p.lang = function () return Export end -- p.lang() return p