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- | serial = "2018-04-16", | ||
item = | 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 | User.favorize = function ( accept, frame ) | ||
-- | -- Guess user language | ||
-- Precondition: | -- Precondition: | ||
-- | -- accept -- sequence table, with offered ISO 639 etc. codes | ||
-- | -- frame -- frame, if available | ||
-- Postcondition: | -- Postcondition: | ||
-- nil | -- Returns string with best code, or nil | ||
if not ( User.self or User.langs ) then | |||
if not User.trials then | |||
return | User.tell = mw.message.new( User.sniffer ) | ||
end -- | 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 | -- 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 | ||
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() | ||
| Zeile 584: | Zeile 653: | ||
-- Postcondition: | -- Postcondition: | ||
-- Returns string with appropriate code | -- Returns string with appropriate code | ||
local r, slang | local s = type( accept ) | ||
if | local codes, r, slang | ||
if | if s == "string" then | ||
codes = mw.text.split( accept:lower(), " " ) | |||
else | elseif s == "table" then | ||
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 = | slang = User.favorize( codes, frame ) | ||
if not slang then | |||
if | slang = favorite() or "en" | ||
end | end | ||
if | 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 | if feasible( slang, codes ) then | ||
r = slang | r = slang | ||
end | end | ||
end | end | ||
if not r then | if not r then | ||
local others = mw.language.getFallbacksFor( slang ) | |||
for i = 1, #others do | |||
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 = | 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. | local r = Multilingual.fallback( frame.args[ 1 ], "de" ) | ||
return r and "1" or "" | return r and "1" or "" | ||
end -- p.kannDeutsch | end -- p.kannDeutsch | ||