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  = "2019-11-30",
                       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 GlobalMod = Multilingual
local GlobalMod = Multilingual
local User     = { sniffer = "showpreview" }
local GlobalData = Multilingual
local User       = { sniffer = "showpreview" }
Multilingual.globals.Multilingual = Multilingual.item
Multilingual.globals.Multilingual = Multilingual.item






Multilingual.correction = { -- Frequently mistaken language code
      aze      = "az",
      cz        = "cs",
      deu      = "de",
      dk        = "da",
      ["en-UK"] = "en-GB",
      ["en-uk"] = "en-GB",
      eng      = "en",
      ger      = "de",
      gr        = "el",
      iw        = "he",
      jp        = "ja",
      lat      = "la",
      se        = "sv",
      tj        = "tg"
    }
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
     -- 2019-10-29
     -- 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, 0 )
             error( "Missing or invalid page: " .. storage )
         end
         end
     end
     end
Zeile 111: Zeile 106:




local favorites = function ()
local fetchData = function ( access )
     -- Provide fallback codes
     -- Retrieve translated keyword from commons:Data:****.tab
     -- Postcondition:
     -- Precondition:
     --    Returns table with sequence of preferred languages
     --    access  -- string, with page identification on Commons
    --     * ahead elements
     --    Returns table, with data, or string, with error message
     --    * user (not yet accessible)
     -- 2019-12-05
    --    * page content language (not yet accessible)
     local storage = access
     --     * page name subpage
     local r
    --     * project
    if type( storage ) == "string" then
     --    * en
        local s
     local r = Multilingual.polyglott
        storage = mw.text.trim( storage )
    local f = function ( add )
        s = storage:lower()
                  local s = add
        if s:sub( 1, 2 ) == "c:" then
                  for i = 1, #r do
            storage = mw.text.trim( storage:sub( 3 ) )
                      if r[ i ] == s then
            s       = storage:lower()
                          s = false
        elseif s:sub( 1, 8 ) == "commons:" then
                          break -- for i
            storage = mw.text.trim( storage:sub( 9 ) )
                      end
            s      = storage:lower()
                  end -- for i
         end
                  if s then
        if s:sub( 1, 5 ) == "data:" then
                      table.insert( r, s )
            storage = mw.text.trim( storage:sub( 6 ) )
                  end
            s      = storage:lower()
              end
         end
    if not r then
         if s == "" or  s == ".tab" then
        local self = mw.language.getContentLanguage():getCode():lower()
             storage = false
         local sub = mw.title.getCurrentTitle().subpageText
        elseif s:sub( -4 ) == ".tab" then
         r = { }
             storage = storage:sub( 1, -5 ) .. ".tab"
         if sub:find( "/", 2, true ) then
        else
             sub = sub:match( "/(%l%l%l?)$" )
             storage = storage .. ".tab"
             if sub then
                table.insert( r, sub )
             end
         end
         end
        f( self )
        f( "en" )
        Multilingual.polyglott = r
     end
     end
     return r
    if type( storage ) == "string" then
end -- favorites()
        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 feasible = function ( ask, accept )
local favorites = function ()
     -- Is ask to be supported by application?
     -- Provide fallback codes
    -- Precondition:
    --    ask    -- lowercase code
    --    accept  -- sequence table, with offered lowercase codes
     -- Postcondition:
     -- Postcondition:
     --    nil, or true
     --    Returns table with sequence of preferred languages
     local r
    --    * ahead elements
     for i = 1, #accept do
    --    * user (not yet accessible)
        if accept[ i ] == ask then
    --    * page content language (not yet accessible)
            r = true
    --    * page name subpage
            break -- for i
    --    * 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 fill = function ( access, alien, frame )
local fetchISO639 = function ( access )
     -- Expand language name template
     -- 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
                        table.insert( post, s )
                        if s:find( "-", 3, true ) then
                            table.insert( unfold, s )
                        else
                            table.insert( three, s )
                        end
                     else
                     else
                        table.insert( order, s )
                        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
      type( another ) == "string" then
        if type( another ) == "string" and  #another > 0 then
        if able == another 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 == another then
             if s then
                 r = true
                 r = mw.language.getFallbacksFor( s )
            else
                 if r[ 1 ] == "en" then
                local others = mw.language.getFallbacksFor( s )
                    local d = fetchISO639( "fallback" )
                 r = feasible( another, others )
                    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
     return Multilingual.correction[ attempt:lower() ] or false
     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, alert, alt, frame )
Multilingual.tabData = function ( access, at, alt, frame )
     -- Select translatable message
     -- Retrieve translated keyword from commons:Data:****.tab
     -- Precondition:
     -- Precondition:
     --    access  -- string, with page identification on Commons
     --    access  -- string, with page identification on Commons
     --    alert  -- string, with error keyword
     --    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. string|nil|false, with language code, or "error"
     --        2. language code, or "error"
     local storage = access
     local data = fetchData( access )
     local data, r1, r2
     local r1, r2
     if type( storage ) == "string" then
     if  type( data ) == "table" then
        storage = mw.text.trim( storage )
         if type( at ) == "string" then
        s = storage:lower()
             local seek = mw.text.trim( at )
        if s:sub( 1, 2 ) == "c:" then
             if seek == "" then
            storage = mw.text.trim( storage:sub( 3 ) )
                 r1 = "EMPTY Multilingual.tabData key"
            s      = storage:lower()
             else
        elseif s:sub( 1, 8 ) == "commons:" then
                 local e, poly
            storage = mw.text.trim( storage:sub( 9 ) )
                 for i = 1, #data do
            s      = storage:lower()
                     e = data[ i ]
        end
                     if type( e ) == "table" then
        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
        if type( Multilingual.TabDATA ) ~= "table" then
            Multilingual.TabDATA = { }
        end
        data = Multilingual.TabDATA[ storage ]
        if type( data ) ~= "table" and  not data then
            local lucky
            lucky, data = pcall( mw.ext.data.get, storage, "_" )
            if type( data ) == "table" then
                data = data.data
                if type( data ) == "table" then
                    Multilingual.TabDATA[ storage ] = data
                else
                    r1 = string.format( "%s [[%s%s]]",
                                        "INVALID Multilingual.tabData",
                                        "commons:Data:",
                                        storage )
                end
            else
                r1 = "BAD STORAGE Multilingual.tabData – commons:"
                    .. storage
            end
            if r1 then
                Multilingual.TabDATA[ storage ] = true
                data = false
            end
        end
    else
        r1 = "BAD STORAGE Multilingual.tabData"
    end
    if data then
         if type( alert ) == "string" then
             local seek = mw.text.trim( alert )
             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 or "wikidata" or "~"
     --    atleast  -- string, with required version
     --                 or false
     --                         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
     -- 2019-10-15
     -- 2020-08-17
    local last  = ( atleast == "~" )
     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 entity = mw.wikibase.getEntity( string.format( "Q%d",
             local suited = string.format( "Q%d", item )
                                                                item ) )
            if link then
            if type( entity ) == "table" then
                r = suited
                local seek = Failsafe.serialProperty or "P348"
            else
                local vsn  = entity:formatPropertyValues( seek )
                local entity = mw.wikibase.getEntity( suited )
                if type( vsn ) == "table"  and
                if type( entity ) == "table" then
                  type( vsn.value ) == "string"  and
                    local seek = Failsafe.serialProperty or "P348"
                  vsn.value ~= "" then
                    local vsn  = entity:formatPropertyValues( seek )
                    if last  and  vsn.value == Failsafe.serial then
                    if type( vsn ) == "table"  and
                         r = false
                      type( vsn.value ) == "string"  and
                    else
                      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   = mw.text.trim( frame.args[ 1 ]  or  "" )
     local s1 = mw.text.trim( frame.args[ 1 ]  or  "" )
     local s2   = mw.text.trim( frame.args[ 2 ]  or  "" )
     local s2 = mw.text.trim( frame.args[ 2 ]  or  "" )
     return Multilingual.fallback( s1, s2 )  and  "1"  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
     return Multilingual.tabData( suite, seek, salt, frame )
     local r    = Multilingual.tabData( suite, seek, salt, frame )
    return r
end -- p.tabData
end -- p.tabData