ARBEIT MACHT FREI
Moduł:Conversion-zh i Moduł:Conversion-zh/sandbox: Różnica pomiędzy stronami
(Różnica między stronami)
m 1 wersja |
m 1 wersja |
||
| Linia 1: | Linia 1: | ||
local this = {} | local this = {} | ||
local mWikitextLC = require( 'Module:WikitextLC' ) | |||
local yesno = require( 'Module:Yesno' ) | |||
local data = mw.loadData( 'Module:Conversion-zh/data' ) | |||
local function _copy( src ) | |||
local out = {} | |||
for k, v in pairs( src ) do | |||
out[ k ] = v | |||
end | |||
return out | |||
end | |||
this.fallbackList = { | this.fallbackList = { | ||
['zh']={'zh','zh-hans','zh-hant','zh-cn','zh-tw','zh-hk','zh-mo','zh-sg','zh-my'}, | [ 'zh' ] = { 'zh', 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-mo', 'zh-sg', 'zh-my' }, | ||
['zh-hans']={'zh-hans','zh-cn','zh-sg','zh-my','zh'}, | [ 'zh-hans' ] = { 'zh-hans', 'zh-cn', 'zh-sg', 'zh-my', 'zh' }, | ||
['zh-cn']={'zh-cn','zh-hans','zh-my','zh-sg','zh'}, | [ 'zh-cn' ] = { 'zh-cn', 'zh-hans', 'zh-my', 'zh-sg', 'zh' }, | ||
['zh-sg']={'zh-sg','zh-hans','zh-cn','zh-my','zh'}, | [ 'zh-sg' ] = { 'zh-sg', 'zh-hans', 'zh-cn', 'zh-my', 'zh' }, | ||
['zh-my']={'zh-my','zh-hans','zh-cn','zh-sg','zh'}, | [ 'zh-my' ] = { 'zh-my', 'zh-hans', 'zh-cn', 'zh-sg', 'zh' }, | ||
['zh-hant']={'zh-hant','zh-tw','zh-hk','zh-mo','zh'}, | [ 'zh-hant' ] = { 'zh-hant', 'zh-tw', 'zh-hk', 'zh-mo', 'zh' }, | ||
['zh-tw']={'zh-tw','zh-hant','zh-hk','zh-mo','zh'}, | [ 'zh-tw' ] = { 'zh-tw', 'zh-hant', 'zh-hk', 'zh-mo', 'zh' }, | ||
['zh-hk']={'zh-hk','zh-hant','zh-tw','zh-mo','zh'}, | [ 'zh-hk' ] = { 'zh-hk', 'zh-hant', 'zh-tw', 'zh-mo', 'zh' }, | ||
['zh-mo']={'zh-mo','zh-hant','zh-tw','zh-hk','zh'} | [ 'zh-mo' ] = { 'zh-mo', 'zh-hant', 'zh-tw', 'zh-hk', 'zh' } | ||
} | |||
this.namespaceData = {} | |||
for id, conData in pairs( data.namespace ) do | |||
this.namespaceData[ id ] = _copy( conData ) | |||
this.namespaceData[ id ].zh = mw.site.namespaces[ id ].name | |||
end | |||
this.variants = { | |||
'zh', | |||
'zh-hans', | |||
'zh-hant', | |||
'zh-cn', | |||
'zh-tw', | |||
'zh-hk', | |||
'zh-mo', | |||
'zh-sg', | |||
'zh-my', | |||
} | } | ||
function this._main(args) | local function filterVariantsKey( src ) | ||
local ret = {} | |||
for _, variant in ipairs( this.variants ) do | |||
ret[ variant ] = src[ variant ] | |||
end | |||
return ret | |||
end | |||
this._ll = {} | |||
function this._ll.convert( args, userlanguage ) | |||
local fallback = this.fallbackList[ userlanguage ] | |||
if not fallback then | |||
fallback = this.fallbackList[ 'zh' ] | |||
end | |||
for _, langArgName in ipairs( fallback ) do | |||
if args[ langArgName ] ~= nil then | |||
return args[ langArgName ] | |||
end | |||
end | |||
return '' | |||
end | |||
function this._ll.convertNamespace( nsId, lang ) | |||
local namespace = mw.site.namespaces[ nsId ] or mw.site.namespaces[ tonumber(nsId) ] | |||
if not namespace then | |||
error( 'bad namespace ' .. tostring( nsId ) ) | |||
end | |||
local conData = this.namespaceData[ namespace.id ] | |||
if conData then | |||
return this._ll.convert( conData, lang ) | |||
else | |||
return namespace.name | |||
end | |||
end | |||
this._lc = {} | |||
function this._lc.convert( args ) | |||
return mWikitextLC.selective( filterVariantsKey( args ) ) | |||
end | |||
function this._lc.convertNamespace( nsId ) | |||
local namespace = mw.site.namespaces[ nsId ] or mw.site.namespaces[ tonumber( nsId ) ] | |||
if not namespace then | |||
error( 'bad namespace ' .. nsId ) | |||
end | |||
if namespace.id == 0 then | |||
return '' | |||
end | |||
local conData = this.namespaceData[ namespace.id ] | |||
if conData then | |||
local ret = {} | |||
for _, variant in ipairs( this.variants ) do | |||
if conData[ variant ] then | |||
ret[ variant ] = conData[ variant ] | |||
end | |||
end | |||
return mWikitextLC.selective( ret ) | |||
else | |||
return namespace.name .. ':' | |||
end | |||
end | |||
local function wrapNamespaceAsPrefix( result ) | |||
return result and ( result .. ':' ) or '' | |||
end | |||
function this._convert( args, lang, useLC ) | |||
return useLC | |||
and this._lc.convert( args ) | |||
or this._ll.convert( args, lang ) | |||
end | |||
function this._convertNamespace( nsId, lang, useLC ) | |||
return useLC | |||
and this._lc.convertNamespace( nsId ) | |||
or this._ll.convertNamespace( nsId, lang ) | |||
end | |||
function lc_zh_merge_HideNamespaceWarning( param ) | |||
mw.addWarning( | |||
'Use of <code>{{[[Template:LC zh|LC zh]]|' | |||
.. param:gsub( '|', '|' ) | |||
.. '}}</code> to hide namespace is deprecated, remove argument namespace or use <code>|nsp=1</code> instead.' | |||
) | |||
end | |||
function this._main( args, cfg ) | |||
local frame = mw.getCurrentFrame() | local frame = mw.getCurrentFrame() | ||
local | cfg = cfg or {} | ||
local | local sourceMode = args[ 'mode' ] or args[ '1' ] or args[ 1 ] -- N (Namespace) / T (Title) / C (Content / Default) | ||
sourceMode = ( not cfg.disableSourceMode and sourceMode ) and tostring( sourceMode ):upper() or 'C' | |||
local namespace = args.namespace or args[ '2' ] or args[ 2 ] -- Only use on mode Title. Pass empty string / nil (lua) or skip this argument to hide namespace | |||
local hideNamespace = yesno( args.nsp or args.hide_ns ) | |||
if cfg.LC_zh_fallback and args[ 3 ] ~= nil then | |||
if yesno( args[ '2' ] or args[ 2 ] ) == false then | |||
lc_zh_merge_HideNamespaceWarning( '2=0|3=[namespace]' ) | |||
hideNamespace = true | |||
else | |||
mw.addWarning( | |||
'Use of <code>{{[[Template:LC zh|LC zh]]|3=[namespace]}}</code> is deprecated, use <code>namespcae=[namespace]</code> instead.' | |||
) | |||
namespace = args[ 3 ] | |||
end | |||
end | |||
local pageLangCode = frame:preprocess( '{{PAGELANGUAGE}}' ) | |||
local userLanguage = frame:callParserFunction{ name = 'int', args = { 'lang' } } | |||
local argNoUseLC = yesno( args.nolc ) | |||
argNoUseLC = argNoUseLC == nil | |||
local useLC = yesno( args.lc ) | |||
or ( yesno( args.nolc ) ~= nil and not yesno( args.nolc ) ) | |||
or ( cfg.useLC ~= nil and yesno( useLC ) or pageLangCode == 'zh' ) | |||
if cfg.LC_zh_fallback and args.nons ~= nil then | |||
mw.addWarning( | |||
'Use of <code>{{[[Template:LC zh|LC zh]]|nons=0}}</code> is deprecated, use <code>hide_ns=1|nolc=true</code> instead.' | |||
) | |||
if yesno( args.nons ) == false then | |||
hideNamespace = true | |||
useLC = false | |||
end | |||
end | end | ||
if | if sourceMode == 'N' then | ||
return args | if namespace then | ||
return this._convertNamespace( args, userLanguage, useLC ) | |||
end | end | ||
return '' | |||
elseif sourceMode == 'T' then | |||
local mainText = this._convert( args, userLanguage, useLC ) | |||
local nsText = '' | |||
if namespace then | |||
nsText = wrapNamespaceAsPrefix( this._convertNamespace( namespace, userLanguage, useLC ) ) | |||
end | |||
return nsText .. mainText | |||
elseif sourceMode == 'C' then | |||
return this._convert( args, userLanguage, useLC ) | |||
end | end | ||
return '' | return '' | ||
end | end | ||
function this.main(frame) | function this.main( frame ) | ||
local getArgs | local getArgs | ||
local args | local args | ||
if not getArgs then | if not getArgs then | ||
getArgs = require('Module:Arguments').getArgs | getArgs = require( 'Module:Arguments' ).getArgs | ||
end | |||
args = getArgs( frame, { parentFirst = true } ) | |||
local cfg | |||
if frame:getParent() then | |||
cfg = frame.args | |||
end | end | ||
return this._main( args, cfg ) | |||
return this._main(args) | |||
end | end | ||
return this | return this | ||