Module:User:Verdy p/Sandbox
Module documentation
[create]
local p = {}
function p.test(frame)
local int_Lang = ''
local Mediawiki_Lang = ''
if (frame ~= nil)
then
--The first four are incorrect. int:foo is not a parser function, it is like more like msg:foo, right?
--int_Lang = frame:callParserFunction{name = 'ns', args = {'1'}} -- OK returns "Talk", like {{ns:1}} in MediaWiki
--int_Lang = frame:callParserFunction{name = 'ns', args = {'User'}}-- OK returns "User", like {{ns:User}} in MediaWiki
--int_Lang = frame:callParserFunction{name = 'ns', args = {'Lang'}}-- FAILS: function "ns" not found (strange exception, should be: unknown "Lang" namespace...)
--int_Lang = frame:callParserFunction{name = 'int', args = {'Lang'}} -- this crashes the current MediaWiki instance !!
int_Lang = frame:preprocess('{{int:lang}}')
Mediawiki_Lang = frame:expandTemplate{title = 'Mediawiki:Lang'}
end
return
'* {{int:Lang}} = "' ..
int_Lang .. '" ;\n' .. -- How can I otherwise know the value of "uselang=" in query string or the user's languaga? it works with {{int:Lang}} in MediaWiki
'* {{Mediawiki:Lang}} = "' ..
Mediawiki_Lang .. '" ;\n' ..
'* mw.getContentLanguage():getCode() = "' ..
mw.getContentLanguage():getCode() .. '" ;\n' ..
'* mw.language.getContentLanguage():getCode() = "' ..
mw.language.getContentLanguage():getCode() .. '" ;\n' ..
'* mw.message.getDefaultLanguage():getCode() = "' ..
mw.message.getDefaultLanguage():getCode() .. '" ;\n'
end
function p.getLanguageSubpage()
-- Get the last subpage (this function isolated for debugging purpose)
local subpage = mw.title.getCurrentTitle().subpageText
--[[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
-- Return the subpage only if it is a valid language code.
if (mw.language.isKnownLanguageTag(subpage))
then
return subpage
end
end
-- Otherwise there's currently no known language subpage
return ''
end
--[[If on a translation subpage (like Foobar/de), this function renders
a given template in the same language, if the translation is available.
Otherwise, the template is rendered in its default language, without
modification.
This is aimed at replacing the current implementation of Template:TNT.
]]
function p.renderTranslatedTemplate(frame)
--[[get the mandatory parameter named "template" (and other arguments for that template)
either in arguments of the frame passed by the Lua caller of this function (this
could be the parameters of #invoke prser function used in Template:TNT), or
directly from the caller of Template:TNT.
]]
local args = frame.args or {}
local template = args['template']
if (template == nil)
then
args = frame:getParent() or {}
template = args['template']
if (template == nil)
then
--[[If not found, retry with parameter 1 (needed for simpler uses Template:TNT,
but requires shifting all numbered parameters in caller's syntax).
It's just better to use named parameter "template" as no shifting is needed.
]]
template = args['1']
if (template == nil)
then
return '' -- nothing to render. Should we return an error?
end
--[[Copy args pseudo-table to a proper table so we can feed it
below to expandTemplate.
]]
local arguments = {}
for k, v in pairs(args) do
-- 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
end
else
arguments[k] = v
end
end
args = arguments
end
end
template = mw.text.trim(template)
if (template == '')
then
return '' -- empty template name specified, nothing to render. Should we return an error?
end
--[[Check whether the template is actually in the Template namespace, or
if we're transcluding a main-namespace page.
(added for backward compatibility of Template:TNT)
]]
local title
local namespace = mw.text.trim(args['namespace'] or '')
if (namespace ~= '') -- Checks for namespace parameter for custom ns
then
title = mw.title.new(template, namespace) -- counted as EXPENSIVE (adds a link to current page)
else
title = mw.title.new(template, 'Template') -- counted as EXPENSIVE (adds a link to current page)
if (title.id == 0)
then -- not found in the Template namespace
-- (legacy) assume the main namespace
title = mw.title.new(template, '') -- counted as EXPENSIVE (adds a link to current page)
end
end
--[[Check that the base template effectively exists in that namespace
(to avoid returning an
unexplained "script error" when calling expandTemplate below).
]]
if (title.id == 0)
then -- not found
-- render like a (missing) template transclusion
return '{{' .. title.fullText .. '}}'
end
--[[Get the last subpage in current viewed page, and check if it matches a known
language code to look for the correct translation of the template.
]]
local subpage = p.getLanguageSubpage()
if (subpage ~= '')
then
-- Check if a translation of the template exists in that language; if so, let's use it
local translatedTitle = title.subPageTitle(subpage) -- counted as EXPENSIVE (adds a link to current page)
if (translatedTitle.id ~= 0)
then -- found, replace the base title
title = translatedTitle
else
-- TODO: implement language fallbacks
-- keep base title
end
end
return frame:expandTemplate{title = title.fullText, args = arguments}
end
return p