편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.
최신판 | 당신의 편집 | ||
22번째 줄: | 22번째 줄: | ||
case-insensitive; they are not "SupportedLanguages" for MediaWiki, so | case-insensitive; they are not "SupportedLanguages" for MediaWiki, so | ||
they are not "KnownLanguageTags" for MediaWiki). | they are not "KnownLanguageTags" for MediaWiki). | ||
To be more restrictive, we exclude any character | To be more restrictive, we exclude any character that is not | ||
ASCII and not a lowercase letter, minus-hyphen, or digit, and any code | |||
that does not start by a letter or does not finish by a letter or digit. | |||
of that has more than 8 characters between hyphens, or has two hyphens. | |||
--]] | --]] | ||
or string.find(subpage, "^[%l][%-%d%l]*[%d%l]$") ~= nil | or string.find(subpage, "^[%l][%-%d%l]*[%d%l]$") ~= nil | ||
and string.find(subpage, "[%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l]") == nil | and string.find(subpage, "[%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l]") == nil | ||
and string.find(subpage, "%-%-") == nil | and string.find(subpage, "%-%-") == nil | ||
then | then | ||
return subpage | return subpage | ||
50번째 줄: | 37번째 줄: | ||
end | end | ||
--[[Get the last subpage of | --[[Get the last subpage of the current page if it is a translation. | ||
]] | ]] | ||
function this. | function this.getLanguageSubpage() | ||
--[[This code does not work ib all namespaces where the Translate tool works. | |||
--[[This code does not work | |||
-- It works in the main namespace on Meta because it allows subpages there | -- It works in the main namespace on Meta because it allows subpages there | ||
-- It would not work in the main namespace of English Wikipedia (but the | -- It would not work in the main namespace of English Wikipedia (but the | ||
84번째 줄: | 52번째 줄: | ||
-- bug of Meta-Wiki. The work-around is to split the full title and then | -- bug of Meta-Wiki. The work-around is to split the full title and then | ||
-- get the last titlepart. | -- get the last titlepart. | ||
local subpage = title.subpageText | local subpage = mw.title.getCurrentTitle().subpageText | ||
--]] | --]] | ||
local titleparts = mw.text.split(mw.title.getCurrentTitle().fullText, '/') | |||
local subpage = titleparts[#titleparts] | |||
return this.checkLanguage(subpage, '') | |||
end | end | ||
108번째 줄: | 63번째 줄: | ||
]] | ]] | ||
function this.getFrameLanguageSubpage(frame) | function this.getFrameLanguageSubpage(frame) | ||
local titleparts = mw.text.split(frame:getParent():getTitle(), '/') | |||
local subpage = titleparts[#titleparts] | |||
return this.checkLanguage(subpage, '') | |||
end | end | ||
128번째 줄: | 85번째 줄: | ||
end | end | ||
function this. | function this.titleNew(pagename, namespace) | ||
local message, title | local message, title | ||
local valid, title = xpcall(function() | local valid, title = xpcall(function() | ||
return mw.title.new(pagename, namespace) -- costly | return mw.title.new(pagename, namespace) -- costly | ||
end, function(msg) -- catch undocumented exception (!?) | end, function(msg) -- catch undocumented exception (!?) | ||
-- thrown when namespace does not exist | -- thrown when namespace does not exist | ||
message = msg | message = msg | ||
end) | end) | ||
if valid and title ~= nil and | if valid and title ~= nil and title.id ~= 0 | ||
then | then | ||
return title | return title | ||
end | end | ||
return { | return { | ||
message = message, | |||
id = 0, | |||
namespace = '', | |||
prefixedText = pagename, | |||
} | } | ||
end | end | ||
--[[If on a translation subpage (like Foobar/de), this function | --[[If on a translation subpage (like Foobar/de), this function renders | ||
a given template in the same language, if the translation is available. | a given template in the same language, if the translation is available. | ||
Otherwise, the template is | Otherwise, the template is rendered in its default language, without | ||
modification. | modification. | ||
This is aimed at replacing the current implementation of Template: | This is aimed at replacing the current implementation of Template:TNT. | ||
]] | ]] | ||
function this. | function this.renderTranslatedTemplate(frame) | ||
local args = frame.args | local args = frame.args | ||
local pagename = args['template'] | local pagename = args['template'] | ||
171번째 줄: | 120번째 줄: | ||
]] | ]] | ||
local title | local title | ||
local namespace = args[' | local namespace = args['namespace'] or '' | ||
if (namespace ~= '') -- Checks for | if (namespace ~= '') -- Checks for namespace parameter for custom ns. | ||
then | then | ||
title = this. | title = this.titleNew(pagename, namespace) -- Costly | ||
else -- Supposes that set page is in ns10. | else -- Supposes that set page is in ns10. | ||
namespace = 'Template' | namespace = 'Template' | ||
title = this. | title = this.titleNew(pagename, namespace) -- Costly | ||
if title.id == | if title.id == 0 | ||
then -- not found in the Template namespace, assume the main namespace (for backward compatibility) | then -- not found in the Template namespace, assume the main namespace (for backward compatibility) | ||
namespace = '' | namespace = '' | ||
title = this. | title = this.titleNew(pagename, namespace) -- Costly | ||
end | end | ||
end | end | ||
189번째 줄: | 138번째 줄: | ||
if (subpage == '') | if (subpage == '') | ||
then | then | ||
subpage = this. | subpage = this.getLanguageSubpage() | ||
end | end | ||
if (subpage == '') | if (subpage == '') | ||
then | then | ||
-- Check if a translation of the pagename exists in English | -- Check if a translation of the pagename exists in English | ||
local newtitle = this. | local newtitle = this.titleNew(pagename .. '/' .. 'en', namespace) -- Costly | ||
-- Use the translation when it exists | -- Use the translation when it exists | ||
if newtitle.id ~= | if newtitle.id ~= 0 | ||
then | then | ||
title = newtitle | title = newtitle | ||
202번째 줄: | 151번째 줄: | ||
else | else | ||
-- Check if a translation of the pagename exists in that language | -- Check if a translation of the pagename exists in that language | ||
local newtitle = this. | local newtitle = this.titleNew(pagename .. '/' .. subpage, namespace) -- Costly | ||
if newtitle.id == | if newtitle.id == 0 | ||
then | then | ||
-- Check if a translation of the pagename exists in English | -- Check if a translation of the pagename exists in English | ||
newtitle = this. | newtitle = this.titleNew(pagename .. '/' .. 'en', namespace) -- Costly | ||
end | end | ||
-- Use the translation when it exists | -- Use the translation when it exists | ||
if newtitle.id ~= | if newtitle.id ~= 0 | ||
then | then | ||
title = newtitle | title = newtitle | ||
end | end | ||
end | end | ||
-- At this point the title should exist | |||
if | -- At this point the title should exist, otherwise render a red link to the missing page (resolved in its assumed namespace) | ||
if title.id == 0 | |||
then | |||
return '[[' .. title.prefixedText .. ']]' | |||
end | end | ||
-- Copy args pseudo-table to a proper table so we can feed it to expandTemplate. | -- Copy args pseudo-table to a proper table so we can feed it to expandTemplate. | ||
-- Then render the pagename. | -- Then render the pagename. | ||
local pargs = (frame:getParent() or {}).args | local pargs = (frame:getParent() or {}).args | ||
local arguments | local arguments | ||
if (args['noshift'] or '') == '' | if (args['noshift'] or '') == '' | ||
then | then | ||
arguments = {} | |||
for k, v in pairs(pargs) do | for k, v in pairs(pargs) do | ||
-- numbered args >= 1 need to be shifted | -- numbered args >= 1 need to be shifted | ||
268번째 줄: | 192번째 줄: | ||
else -- special case where TNT is used as autotranslate | else -- special case where TNT is used as autotranslate | ||
-- (don't shift again what is shifted in the invokation) | -- (don't shift again what is shifted in the invokation) | ||
arguments = pargs | |||
end | end | ||
arguments['template'] = title -- override the existing parameter of the base template name supplied with the full name of the actual template expanded | arguments['template'] = title.prefixedText -- override the existing parameter of the base template name supplied with the full name of the actual template expanded | ||
arguments[' | arguments['namespace'] = nil -- discard the specified namespace override | ||
arguments['uselang'] = args | arguments['uselang'] = args.uselang -- argument added to parent frame | ||
arguments['noshift'] = args | arguments['noshift'] = args.noshift -- argument added to parent frame | ||
return frame:expandTemplate{title = ':' .. title | return frame:expandTemplate{title = ':' .. title.prefixedText, args = arguments} | ||
end | end | ||
return this | return this |