편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.
최신판 | 당신의 편집 | ||
2번째 줄: | 2번째 줄: | ||
function this.checkLanguage(subpage, default) | function this.checkLanguage(subpage, default) | ||
--[[Check first if there's an | --[[Check first if there's an apostrophe, because they break the isKnownLanguageTag | ||
function. This test does not work with regexps, use plain search instead (no need | |||
to use Unicode parser, apostrophes can only appear isolated as one byte in UTF-8). | |||
]] | |||
if (string.find(subpage, "'", 1, true) == nil) | |||
then | then | ||
return subpage | -- Return the subpage only if it is a valid language code. | ||
if (mw.language.isKnownLanguageTag(subpage)) | |||
then | |||
return subpage | |||
end | |||
end | end | ||
-- Otherwise there's currently no known language subpage | -- Otherwise there's currently no known language subpage | ||
50번째 줄: | 18번째 줄: | ||
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번째 줄: | 33번째 줄: | ||
-- 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, mw.language.getContentLanguage():getCode()) | |||
end | end | ||
108번째 줄: | 44번째 줄: | ||
]] | ]] | ||
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번째 줄: | 66번째 줄: | ||
end | end | ||
--[[If on a translation subpage (like Foobar/de), this function renders | |||
--[[If on a translation subpage (like Foobar/de), this function | |||
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번째 줄: | 81번째 줄: | ||
]] | ]] | ||
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 = | title = mw.title.new(pagename, namespace) -- Costly | ||
else -- Supposes that set page is in ns10. | else -- Supposes that set page is in ns10. | ||
namespace = 'Template' | namespace = 'Template' | ||
title = | title = mw.title.new(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 = | title = mw.title.new(pagename, namespace) -- Costly | ||
end | end | ||
end | end | ||
-- Get the last subpage and check if it matches a known language code. | -- Get the last subpage and check if it matches a known language code. | ||
local | local subpage = this.getLanguageSubpage() | ||
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 = | local newtitle = mw.title.new(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번째 줄: | 108번째 줄: | ||
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 = | local newtitle = mw.title.new(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 = | newtitle = mw.title.new(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 arguments = {} | local arguments = {} | ||
for k, v in pairs((frame:getParent() or {}).args) do | |||
-- numbered args >= 1 need to be shifted | -- numbered args >= 1 need to be shifted | ||
local n = tonumber(k) or 0 | |||
if (n > 0) | |||
then | |||
if (n >= 2) | |||
then | |||
arguments[n - 1] = v | arguments[n - 1] = v | ||
end | end | ||
301번째 줄: | 143번째 줄: | ||
end | end | ||
end | end | ||
return frame:expandTemplate{title = ':' .. title.prefixedText, args = arguments} | |||
return frame:expandTemplate{title = ' | |||
end | end | ||
return this | return this |