Zum Inhalt springen

Modul:Multilingual: Unterschied zwischen den Versionen

2018-04-16
w>PerfektesChaos
(2018-03-01)
w>PerfektesChaos
(2018-04-16)
Zeile 1: Zeile 1:
local Multilingual = { suite  = "Multilingual",
local Multilingual = { suite  = "Multilingual",
                       serial = "2018-03-01",
                       serial = "2018-04-16",
                       item  = 47541920 }
                       item  = 47541920 }
local User = { sniffer = "publishchanges" }
 
 
 
Multilingual.exotic = { simple = true,
                        no    = true }




Zeile 14: Zeile 20:
     return Multilingual.self
     return Multilingual.self
end -- favorite()
end -- favorite()
function feasible( 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()




Zeile 76: Zeile 101:




function isSupported( ask, accept )
User.favorize = function ( accept, frame )
     -- Is ask to be supported by application?
     -- Guess user language
     -- Precondition:
     -- Precondition:
     --    ask    -- lowercase code
     --    accept  -- sequence table, with offered ISO 639 etc. codes
     --    accept  -- space separated/terminated list of lowercase codes
     --    frame  -- frame, if available
     -- Postcondition:
     -- Postcondition:
     --    nil, or else
     --    Returns string with best code, or nil
    local seek = string.format( " %s ", ask )
    if not ( User.self or User.langs ) then
    local supported = string.format( " %s", accept )
        if not User.trials then
     return supported:find( seek, 1, true )
            User.tell = mw.message.new( User.sniffer )
end -- isSupported()
            if User.tell:exists() then
                User.trials = { }
                if not Multilingual.frame then
                    if frame then
                        Multilingual.frame = frame
                    else
                        Multilingual.frame = mw.getCurrentFrame()
                    end
                end
                User.sin = Multilingual.frame:callParserFunction( "int",
                                                          User.sniffer )
            else
                User.langs = true
            end
        end
        if User.sin then
            local s, sin
            for i = 1, #accept do
                s = accept[ i ]
                if not User.trials[ s ] then
                    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
    end
     return User.self
end -- User.favorize()




Zeile 123: Zeile 180:
     return r or false
     return r or false
end -- Multilingual.fair()
end -- Multilingual.fair()
Multilingual.fallback = function ( able, another )
    -- Is another language suitable as replacement?
    -- Precondition:
    --    able    -- language version specifier to be supported
    --    another  -- language specifier of a possible replacement
    -- Postcondition:
    --    Returns boolean
    local r
    if type( able ) == "string"  and
      type( another ) == "string" then
        if able == another then
            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
    end
    return r or false
end -- Multilingual.fallback()




Zeile 484: Zeile 568:




Multilingual.isLang = function ( ask )
Multilingual.isLang = function ( ask, additional )
     -- Could this be an ISO language code?
     -- Could this be an ISO language code?
     -- Precondition:
     -- Precondition:
     --    ask -- language code
     --    ask         -- language code
    --    additional  -- true, if Wiki codes like "simple" permitted
     -- Postcondition:
     -- Postcondition:
     --    Returns boolean
     --    Returns boolean
     local s = Multilingual.getBase( ask )
     local r, s
     local r
    if additional then
        s = ask
    else
        s = Multilingual.getBase( ask )
     end
     if s then
     if s then
         r = mw.language.isKnownLanguageTag( s )
         r = mw.language.isKnownLanguageTag( s )
        if not r  and  additional then
            r = Multilingual.exotic[ s ] or false
        end
     else
     else
         r = false
         r = false
Zeile 511: Zeile 603:
     local s = Multilingual.getBase( ask )
     local s = Multilingual.getBase( ask )
     if s then
     if s then
         r = mw.language.isSupportedLanguage( s )
         r = mw.language.isSupportedLanguage( s ) or
            Multilingual.exotic[ ask ]
     else
     else
         r = false
         r = false
Zeile 549: Zeile 642:
     return r
     return r
end -- Multilingual.isMinusculable()
end -- Multilingual.isMinusculable()
Multilingual.kannDeutsch = function ( ask )
    -- Kann man mit diesem Sprachcode deutsch verstehen?
    -- Precondition:
    --    ask  -- language version specifier
    -- Postcondition:
    --    Returns boolean
    local s = Multilingual.getBase( ask )
    local r
    if s then
        local support = [=[ de als bar dsb frr gsw hsb ksh |
                            lb nds pdc pdt pfl sli stq vmf ]=]
        if support:find( string.format( " %s ", s ),  1,  true ) then
            r = true
        else
            r = false
        end
    else
        r = false
    end
    return r
end -- Multilingual.kannDeutsch()




Zeile 584: Zeile 653:
     -- Postcondition:
     -- Postcondition:
     --    Returns string with appropriate code
     --    Returns string with appropriate code
     local r, slang, support
     local s = type( accept )
     if not Multilingual.frame then
    local codes, r, slang
         if frame then
     if s == "string" then
             Multilingual.frame = frame
        codes = mw.text.split( accept:lower(), " " )
         else
    elseif s == "table" then
            Multilingual.frame = mw.getCurrentFrame()
         codes = { }
        for i = 1, #accept do
            s = accept[ i ]
            if type( s ) == "string"  then
                table.insert( codes, s:lower() )
             end
         end -- for i
    else
        codes = { }
        slang = favorite()
        if mw.language.isKnownLanguageTag( slang ) then
            table.insert( codes, slang )
         end
         end
     end
     end
     slang = Multilingual.frame:callParserFunction( "int",
     slang = User.favorize( codes, frame )
                                                  "lang" ):lower()
     if not slang then
     if type( accept ) == "string" then
         slang = favorite() or  "en"
         support = accept:lower() .. " "
    else
        support = favorite()
        if mw.language.isKnownLanguageTag( support ) then
            support = string.format( "%s en ", support )
        else
            support = "en "
        end
     end
     end
     if isSupported( slang, support ) then
     if feasible( slang, codes ) then
         r = slang
         r = slang
     elseif slang:find( "-", 1, true ) then
     elseif slang:find( "-", 1, true ) then
         slang = Multilingual.getBase( slang )
         slang = Multilingual.getBase( slang )
         if isSupported( slang, support ) then
         if feasible( slang, codes ) then
             r = slang
             r = slang
         end
         end
     end
     end
     if not r then
     if not r then
         if Multilingual.kannDeutsch( slang ) and
         local others = mw.language.getFallbacksFor( slang )
          isSupported( "de", support ) then
        for i = 1, #others do
            r = "de"
            slang = others[ i ]
         end
            if feasible( slang, codes ) then
                r = slang
                break -- for i
            end
         end -- for i
         if not r then
         if not r then
             r = support:match( "^(%S+) " )
             if feasible( "en", codes ) then
                r = "en"
            else
                r = codes[ 1 ]
            end
         end
         end
     end
     end
     return r
     return r
end -- Multilingual.userLang()
end -- Multilingual.userLang()
Multilingual.userLangCode = function ()
    -- Guess a user language code
    -- Postcondition:
    --    Returns code of current best guess
    return User.self  or  favorite()  or  "en"
end -- Multilingual.userLangCode()




Zeile 673: Zeile 762:
     return Multilingual.fair( frame.args[ 1 ] )  or  ""
     return Multilingual.fair( frame.args[ 1 ] )  or  ""
end -- p.fair
end -- p.fair
p.fallback = function ( frame )
    -- Is another language suitable as replacement?
    --    1  -- language version specifier to be supported
    --    2  -- language specifier of a possible replacement
    local r = Multilingual.fallback( frame.args[ 1 ], frame.args[ 2 ]  )
    return r and "1" or ""
end -- p.fallback




Zeile 809: Zeile 908:
     -- Kann man mit diesem Sprachcode deutsch verstehen?
     -- Kann man mit diesem Sprachcode deutsch verstehen?
     --    1  -- code
     --    1  -- code
     local r = Multilingual.kannDeutsch( frame.args[ 1 ] )
     local r = Multilingual.fallback( frame.args[ 1 ], "de" )
     return r and "1" or ""
     return r and "1" or ""
end -- p.kannDeutsch
end -- p.kannDeutsch
Anonymer Benutzer