Modul:Multilingual: Unterschied zwischen den Versionen

K
43 Versionen von wikivoyage:Modul:Multilingual importiert
w>PerfektesChaos
(2019-11-12)
K (43 Versionen von wikivoyage: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-12",
                       serial  = "2020-12-10",
                       item    = 47541920,
                       item    = 47541920,
                       globals = { ISO15924 = 71584769,
                       globals = { ISO15924 = 71584769,
Zeile 6: Zeile 6:
                     }
                     }
--[=[
--[=[
Utilities for multilingual texts and ISO 639 (BCP47) issues etc.
Utilities for multilingual texts and ISO 639 (BCP47) issues etc.
* fair()
* fair()
* fallback()
* fallback()
Zeile 23: Zeile 23:
* message()
* message()
* sitelink()
* sitelink()
* tabData()
* userLang()
* userLang()
* userLangCode()
* userLangCode()
Zeile 29: 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 67: 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 102: 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
     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
        local s
        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 121: Zeile 187:
     --    * 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 142: Zeile 208:
                 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 206: Zeile 275:
     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 330: Zeile 435:
         end
         end
         if User.sin then
         if User.sin then
            local order  = { }
            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
                     sin = User.tell:inLanguage( s ):plain()
                     if #s > 2 then
                    if sin == User.sin then
                        if s:find( "-", 3, true ) then
                         User.self = s
                            table.insert( unfold, s )
                         break -- for i
                         else
                            table.insert( three, s )
                         end
                     else
                     else
                         User.trials[ s ] = true
                         if Multilingual.prefer[ s ] then
                            table.insert( order, s )
                        else
                            table.insert( post, s )
                        end
                     end
                     end
                end
            end -- for i
            for i = 1, #post do
                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
            for i = 1, #order do
                s = order[ i ]
                sin = User.tell:inLanguage( s ):plain()
                if sin == User.sin then
                    User.self = s
                    break -- for i
                else
                    User.trials[ s ] = true
                 end
                 end
             end -- for i
             end -- for i
Zeile 390: 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 454: 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 770: Zeile 920:
     -- Precondition:
     -- Precondition:
     --    available  -- table, with mapping language code ./. text
     --    available  -- table, with mapping language code ./. text
     --    alt        -- string|nil|false, with fallback
     --    alt        -- string|nil|false, with fallback text
     --    frame      -- frame, if available
     --    frame      -- frame, if available
     --    Returns
     --    Returns
Zeile 932: Zeile 1.082:
     return r
     return r
end -- Multilingual.isRTL()
end -- Multilingual.isRTL()
Multilingual.isTrans = function ( ask, assign, about )
    -- OBSOLETE
    -- Check whether valid transcription for context
    -- Precondition:
    --    ask    -- string, with transcription key
    --    assign  -- string, with language or scripting code
    --    about  -- string or nil, with site scripting code
    -- Postcondition:
    --    Returns boolean
    local bib = fetch( "ISO15924" )
    local r
    if type( bib ) == "table"  and
      type( bib.isTrans ) == "function" then
        r = bib.isTrans( ask, assign, about )
    end
    return r or false
end -- Multilingual.isTrans()




Zeile 965: Zeile 1.095:
     if type( arglist ) == "table" then
     if type( arglist ) == "table" then
         local t = { }
         local t = { }
         local m, p
         local m, p, save
         for k, v in pairs( arglist ) do
         for k, v in pairs( arglist ) do
             if type( k ) == "string"  and
             if type( k ) == "string"  and
Zeile 984: Zeile 1.114:
             end
             end
         end -- for k, v
         end -- for k, v
         r = Multilingual.i18n( t, nil, frame )
        if type( arglist[ "-" ] ) == "string" then
            save = arglist[ arglist[ "-" ] ]
        end
         r = Multilingual.i18n( t, save, frame )
         if p  and  r  and  r:find( "$", 1, true ) then
         if p  and  r  and  r:find( "$", 1, true ) then
             t = { }
             t = { }
Zeile 1.039: Zeile 1.172:
     return r  or  ""
     return r  or  ""
end -- Multilingual.sitelink()
end -- Multilingual.sitelink()
Multilingual.tabData = function ( access, at, alt, frame )
    -- Retrieve translated keyword from commons:Data:****.tab
    -- Precondition:
    --    access  -- string, with page identification on Commons
    --    at      -- string, with keyword
    --    alt    -- string|nil|false, with fallback text
    --    frame  -- frame, if available
    --    Returns
    --        1. string|nil|false, with selected message
    --        2. language code, or "error"
    local data = fetchData( access )
    local r1, r2
    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 e[ 1 ] == seek then
                            if type( e[ 2 ] ) == "table" then
                                poly = e[ 2 ]
                            else
                                r1 = "INVALID Multilingual.tabData bad #"
                                                        .. tostring( i )
                            end
                            break  -- for i
                        end
                    else
                        break  -- for i
                    end
                end  -- for i
                if poly then
                    data = poly
                else
                    r1 = "UNKNOWN Multilingual.tabData key: " .. seek
                end
            end
        else
            r1 = "INVALID Multilingual.tabData key"
        end
    else
        r1 = data
    end
    if r1 then
        r2 = "error"
    elseif data then
        r1, r2 = Multilingual.i18n( data, alt, frame )
        r2 = r2 or "error"
    end
    return r1, r2
end -- Multilingual.tabData()




Zeile 1.162: 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.222: 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.381: Zeile 1.591:
     return Multilingual.isRTL( s ) and "1" or ""
     return Multilingual.isRTL( s ) and "1" or ""
end -- p.isRTL()
end -- p.isRTL()
p.isTrans = function ( frame )
    -- OBSOLETE
    -- Check whether valid transcription for context
    --    1    -- string, with transcription key
    --    2    -- string, with language or scripting code
    --    site  -- string or nil, with site scripting code
    local s1  = mw.text.trim( frame.args[ 1 ]  or  "" )
    local s2  = mw.text.trim( frame.args[ 2 ]  or  "" )
    local site = mw.text.trim( frame.args.site  or  "" )
    return Multilingual.isTrans( s1, s2, site )  and  "1"  or  ""
end -- p.isTrans




Zeile 1.420: Zeile 1.616:
     return r or s
     return r or s
end -- p.sitelink
end -- p.sitelink
p.tabData = function ( frame )
    -- Retrieve best message text from Commons Data
    --    1    -- page identification on Commons
    --    2    -- keyword
    --    alt  -- fallback text
    local suite = frame.args[ 1 ]
    local seek  = frame.args[ 2 ]
    local salt  = frame.args.alt
    local r    = Multilingual.tabData( suite, seek, salt, frame )
    return r
end -- p.tabData