Modul:Multilingual: Unterschied zwischen den Versionen

2015-06-19
w>PerfektesChaos
(update)
w>PerfektesChaos
(2015-06-19)
Zeile 1: Zeile 1:
--[=[ 2014-11-28
local Multilingual = { suite  = "Multilingual",
Multilingual
                      serial = "2015-06-19" };
]=]






local Multilingual = { }
-- local globals
local Frame
local Frame
local Got = { }
 
local SelfLang
 
 
local favorite = function ()
    -- Postcondition:
    --    Returns code of curent project language
    if not Multilingual.self then
        Multilingual.self = mw.language.getContentLanguage():getCode()
                                                            :lower()
    end
    return Multilingual.self
end -- favorite()




Zeile 20: Zeile 29:
     --    Returns  table or false, with library
     --    Returns  table or false, with library
     --    Throws error, if not available
     --    Throws error, if not available
     if Got[ access ] == false then
     if type( Multilingual.ext ) ~= "table" then
     elseif not Got[ access ] then
        Multilingual.ext = { }
    end
    if Multilingual.ext[ access ] == false then
     elseif not Multilingual.ext[ access ] then
         local lucky, got = pcall( require, "Module:" .. access )
         local lucky, got = pcall( require, "Module:" .. access )
         if lucky then
         if lucky then
             if type( got ) == "table" then
             if type( got ) == "table" then
                 Got[ access ] = got
                 Multilingual.ext[ access ] = got
                 if type( got[ access ] ) == "function" then
                 if type( got[ access ] ) == "function" then
                     Got[ access ] = got[ access ]()
                     Multilingual.ext[ access ] = got[ access ]()
                 end
                 end
             end
             end
            got = "Module" .. access .. " invalid"
         end
         end
         if type( Got[ access ] ) ~= "table" then
         if type( Multilingual.ext[ access ] ) ~= "table" then
             error( got, 0 )
             if allow then
                Multilingual.ext[ access ] = false
            else
                got = string.format( "Module:%s invalid", access )
                error( got, 0 )
            end
         end
         end
     end
     end
     return Got[ access ]
     return Multilingual.ext[ access ]
end -- fetch()
end -- fetch()
function find( ask, alien )
    -- Derive language code from name
    -- Precondition:
    --    ask    -- language name, downcased
    --    alien  -- language code of ask
    -- Postcondition:
    --    nil, or
    local codes = mw.language.fetchLanguageNames( alien, "all" )
    local r
    for k, v in pairs( codes ) do
        if mw.ustring.lower( v ) == ask then
            r = k
            break -- for k, v
        end
    end -- for k, v
    return r
end -- find()






function isSupported( ask, accept )
function isSupported( ask, accept )
     -- Is ask to supported by application?
     -- Is ask to be supported by application?
     -- Precondition:
     -- Precondition:
     --    ask    -- lowercase code
     --    ask    -- lowercase code
Zeile 56: Zeile 92:


Multilingual.findCode = function ( ask )
Multilingual.findCode = function ( ask )
     -- Retrieve code of local (current project) language name
     -- Retrieve code of local (current project or English) language name
     -- Precondition:
     -- Precondition:
     --    ask  -- string, with presumable language name
     --    ask  -- string, with presumable language name
Zeile 72: Zeile 108:
             r = seek
             r = seek
         else
         else
             local codes
             local slang = favorite()
             if not SelfLang then
            r = find( seek, slang )
                 SelfLang = mw.language.getContentLanguage():getCode()
             if not r  and  slang ~= "en" then
                 r = find( seek, "en" )
             end
             end
            codes = mw.language.fetchLanguageNames( SelfLang, "all" )
            for k, v in pairs( codes ) do
                if mw.ustring.lower( v ) == seek then
                    r = k
                    break -- for k, v
                end
            end -- for k, v
         end
         end
     end
     end
Zeile 98: Zeile 128:
     --                  -- nil, false, "*": native
     --                  -- nil, false, "*": native
     --                  -- "!": current project
     --                  -- "!": current project
    --                  -- "#": code, downcased, space separated
     --                  -- any valid code
     --                  -- any valid code
     --    alter    -- capitalize, if "c"; downcase all, if "d"
     --    alter    -- capitalize, if "c"; downcase all, if "d"
Zeile 119: Zeile 150:
             if adjacent then
             if adjacent then
                 separator = adjacent
                 separator = adjacent
            elseif alien == "#" then
                separator = " "
             else
             else
                 separator = assembly
                 separator = assembly
Zeile 148: Zeile 181:
                 slang = Multilingual.findCode( single )
                 slang = Multilingual.findCode( single )
                 if slang then
                 if slang then
                     r = Multilingual.getName( slang, alien )
                     if alien == "#" then
                    if active then
                        r = slang
                        local cnf = fetch( "Multilingual/config", true )
                    else
                        if cnf then
                        r = Multilingual.getName( slang, alien )
                            if not frame then
                        if active then
                                if not Frame then
                            local cnf = fetch( "Multilingual/config",
                                    Frame = mw.getCurrentFrame()
                                              true )
                            if cnf then
                                if not frame then
                                    if not Frame then
                                        Frame = mw.getCurrentFrame()
                                    end
                                    frame = Frame
                                end
                                slot = cnf.getLink( slang, frame )
                                if slot then
                                    local wlink = fetch( "WLink" )
                                    slot = wlink.getTarget( slot )
                                else
                                    lapsus = alert
                                 end
                                 end
                                frame = Frame
                            end
                            slot = cnf.getLink( slang, frame )
                            if slot then
                                slot = fetch( "WLink" ).getTarget( slot )
                            else
                                lapsus = alert
                             end
                             end
                         end
                         end
Zeile 237: Zeile 276:
     local r
     local r
     if ask then
     if ask then
         local slang = alien
         local slang   = alien
        local support = "Multilingual/names"
        local tLang
         if slang then
         if slang then
             if slang == "*" then
             if slang == "*" then
                 slang = nil
                 slang = ask:lower()
             elseif slang == "!" then
             elseif slang == "!" then
                 if not SelfLang then
                 slang = favorite()
                    SelfLang = mw.language.getContentLanguage():getCode()
                end
                slang = SelfLang
             else
             else
                 slang = slang:lower()
                 slang = slang:lower()
            end
        else
            slang = ask:lower()
        end
        fetch( support, true )
        tLang = Multilingual.ext.tNames
        if tLang then
            tLang = tLang[ slang ]
            if tLang then
                r = tLang[ ask ]
             end
             end
         end
         end
         r = mw.language.fetchLanguageName( ask, slang )
         if not r then
            if not Multilingual.ext.tMW then
                Multilingual.ext.tMW = { }
            end
            tLang = Multilingual.ext.tMW[ slang ]
            if tLang == nil then
                tLang = mw.language.fetchLanguageNames( slang )
                if tLang then
                    Multilingual.ext.tMW[ slang ] = tLang
                else
                    Multilingual.ext.tMW[ slang ] = false
                end
            end
            if tLang then
                r = tLang[ ask ]
            end
        end
        if not r then
            r = mw.language.fetchLanguageName( ask, slang )
        end
     else
     else
         r = false
         r = false
Zeile 329: Zeile 396:
     local r, slang, support
     local r, slang, support
     if not frame then
     if not frame then
         frame = mw.getCurrentFrame()
         if not Frame then
            Frame = mw.getCurrentFrame()
        end
        frame = Frame
     end
     end
     slang = frame:callParserFunction( "int", "lang" ):lower()
     slang = frame:callParserFunction( "int", "lang" ):lower()
Zeile 335: Zeile 405:
         support = accept:lower() .. " "
         support = accept:lower() .. " "
     else
     else
         support = mw.language.getContentLanguage():getCode()
         support = favorite()
         if mw.language.isKnownLanguageTag( support ) then
         if mw.language.isKnownLanguageTag( support ) then
             support = string.format( "%s en ", support )
             support = string.format( "%s en ", support )
Zeile 345: Zeile 415:
         r = slang
         r = slang
     elseif slang:find( "-", 1, true ) then
     elseif slang:find( "-", 1, true ) then
         slang = Multilingual.getBase()
         slang = Multilingual.getBase( slang )
         if isSupported( slang, support ) then
         if isSupported( slang, support ) then
             r = slang
             r = slang
Anonymer Benutzer