Modul:Multilingual: Unterschied zwischen den Versionen

2019-12-07
w>PerfektesChaos
(2019-11-30)
w>PerfektesChaos
(2019-12-07)
Zeile 1: Zeile 1:
local Multilingual = { suite  = "Multilingual",
local Multilingual = { suite  = "Multilingual",
                       serial  = "2019-11-30",
                       serial  = "2019-12-07",
                       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 = { de = true,
                        en = true,
                        es = true,
                        fr = true,
                        it = true,
                        nl = true,
                        pt = true,
                        ru = true }




Zeile 108: Zeile 101:
     return r
     return r
end -- foreignModule()
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
        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 – 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()




Zeile 122: Zeile 184:
     --    * en
     --    * en
     local r = Multilingual.polyglott
     local r = Multilingual.polyglott
    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
     if not r then
     if not r then
         local self = mw.language.getContentLanguage():getCode():lower()
         local self = mw.language.getContentLanguage():getCode():lower()
         local sub  = mw.title.getCurrentTitle().subpageText
         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 = { }
         r = { }
         if sub:find( "/", 2, true ) then
         if sub:find( "/", 2, true ) then
Zeile 143: Zeile 205:
                 table.insert( r, sub )
                 table.insert( r, sub )
             end
             end
        elseif sub:find( "^%l%l%l?%-?%a?%a?%a?%a?$" )  and
              mw.language.isSupportedLanguage( sub ) then
            table.insert( r, sub )
         end
         end
         f( self )
         f( self )
Zeile 207: Zeile 272:
     return got
     return got
end -- fetch()
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()




Zeile 331: Zeile 432:
         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 457:
             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 521:
     -- 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 600:
     -- 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.040: Zeile 1.173:


Multilingual.tabData = function ( access, alert, alt, frame )
Multilingual.tabData = function ( access, alert, 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
Zeile 1.049: Zeile 1.182:
     --        1. string|nil|false, with selected message
     --        1. string|nil|false, with selected message
     --        2. string|nil|false, with language code, or "error"
     --        2. string|nil|false, with 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 )
        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
        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
         if type( alert ) == "string" then
             local seek = mw.text.trim( alert )
             local seek = mw.text.trim( alert )
Zeile 1.135: Zeile 1.216:
             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.326: Zeile 1.409:
     --    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


Anonymer Benutzer