Difference between revisions of "Module:Utilities"
From Pathfinder Wiki
DesignerThan (talk | contribs) (Created page with "local p = {} function p.localize( frame, strToTrans, langSuffix, page ) if ( page == nil or page == '' ) then page = 'All' end -- check if input is a number if ( tonu...") |
DesignerThan (talk | contribs) |
||
(35 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
+ | local cargo = mw.ext.cargo | ||
− | function p.localize( frame, strToTrans, | + | function p.localize( strToTrans, page ) |
+ | local frame = mw.getCurrentFrame() | ||
+ | if ( strToTrans == nil or strToTrans == '' ) then | ||
+ | mw.log( 'Utilities.localize: There is no string to translate! Returning empty string.' ) | ||
+ | return '' | ||
+ | end | ||
+ | mw.log( 'Utilities.localize (strToTrans, page - input): '..strToTrans..', '..tostring(page) ) | ||
if ( page == nil or page == '' ) then | if ( page == nil or page == '' ) then | ||
page = 'All' | page = 'All' | ||
end | end | ||
+ | mw.log('Utilities.localize (page - checked): '..page) | ||
+ | |||
+ | -- called every time ... shouldn't be a time issue needs between 40 and 80 microseconds | ||
+ | local langSuffix = frame:preprocess( '/{{PAGELANGUAGE}}' ) | ||
+ | mw.log('Utilities.localize (langSuffix): '..langSuffix) | ||
-- check if input is a number | -- check if input is a number | ||
− | if ( tonumber(strToTrans) | + | if ( tonumber(strToTrans) ~= nil ) then |
+ | mw.log('Utilities.localize: Input String is a number, return it!') | ||
return strToTrans | return strToTrans | ||
end | end | ||
− | local strFromTrans = frame:callParserFunction( '#lst', { 'Localization:'..page..langSuffix, strToTrans}) | + | local strFromTrans = cargo.query( 'Localizations_'..page, 'text', { |
+ | where='id="'..strToTrans..langSuffix..'"', | ||
+ | format='template', | ||
+ | template='CargoOutput-NoFormatting', | ||
+ | default='' | ||
+ | }) | ||
+ | -- local strFromTrans = frame:callParserFunction( '#lst', { 'Localization:'..page..langSuffix, strToTrans } ) | ||
-- did translation work? | -- did translation work? | ||
− | if ( strFromTrans ~= '') then | + | if ( type(strFromTrans[1]) == 'table' and (strFromTrans[1].text and strFromTrans[1].text ~= '')) then |
− | return strFromTrans | + | mw.log('Utilities.localize (strFromTrans[1].text): '..tostring(strFromTrans[1].text)) |
+ | return strFromTrans[1].text | ||
else | else | ||
-- add to error category | -- add to error category | ||
-- [[Category:Missing Localization/{{{2|All}}}/{{{1|unsupplied}}}{{GetLangSuffix}}]] | -- [[Category:Missing Localization/{{{2|All}}}/{{{1|unsupplied}}}{{GetLangSuffix}}]] | ||
− | local newErrorCategory = '[[Category:Missing Localization/'..page..'/'..strToTrans..langSuffix | + | local newErrorCategory = '[[Category:Missing Localization/'..page..'/'..strToTrans..langSuffix..']]' |
local errorCategories = frame:callParserFunction( '#var', 'localizationErrorCategories' ) | local errorCategories = frame:callParserFunction( '#var', 'localizationErrorCategories' ) | ||
frame:callParserFunction( '#vardefine', { 'localizationErrorCategories', errorCategories..newErrorCategory } ) | frame:callParserFunction( '#vardefine', { 'localizationErrorCategories', errorCategories..newErrorCategory } ) | ||
− | return | + | return strToTrans |
end | end | ||
+ | end | ||
+ | |||
+ | function p.getNumber( str ) | ||
+ | return tonumber(string.match(str, '%d+')) | ||
+ | end | ||
+ | |||
+ | function p.isSysOp( ) | ||
+ | frame = mw.getCurrentFrame() | ||
+ | if (frame:preprocess('{{#ifsysop:true|false}}') == 'true') then | ||
+ | return true | ||
+ | else | ||
+ | return false | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function p.isUserGroup( usrGroup ) | ||
+ | -- usrGroup can be a comma seperated list of usrGroups | ||
+ | if (frame:preprocess('{{#ifingroup:'..usrGroup..'|true|false}}') == 'true') then | ||
+ | return true | ||
+ | else | ||
+ | return false | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function p.getVar(varName) | ||
+ | local retVal = mw.getCurrentFrame():preprocess('{{#var:'..varName..'}}') | ||
+ | mw.log('Utilities.getVar: Get Variable "'..varName..'" returned the following value: '..tostring(retVal)) | ||
+ | return retVal | ||
+ | end | ||
+ | |||
+ | -- this function adds the items of t2 to t1 | ||
+ | function p.concatTable(t1, t2) | ||
+ | if (t1 == nil or #t1 == nil or | ||
+ | t2 == nil or #t2 == nil) then | ||
+ | return t1 | ||
+ | end | ||
+ | for i=1, #t2 do | ||
+ | t1[#t1+1] = t2[i] | ||
+ | end | ||
+ | return t1 | ||
+ | end | ||
+ | |||
+ | function p.outputNoWiki(frame) | ||
+ | return frame:preprocess('<nowiki>'..frame.args[1]..'</nowiki>') | ||
+ | end | ||
+ | |||
+ | function p.logger(frame) | ||
+ | return mw.log('-- Logger: |'..frame.args[1]..'|') | ||
end | end | ||
return p | return p |
Latest revision as of 19:44, 11 February 2022
Documentation for this module may be created at Module:Utilities/doc
local p = {}
local cargo = mw.ext.cargo
function p.localize( strToTrans, page )
local frame = mw.getCurrentFrame()
if ( strToTrans == nil or strToTrans == '' ) then
mw.log( 'Utilities.localize: There is no string to translate! Returning empty string.' )
return ''
end
mw.log( 'Utilities.localize (strToTrans, page - input): '..strToTrans..', '..tostring(page) )
if ( page == nil or page == '' ) then
page = 'All'
end
mw.log('Utilities.localize (page - checked): '..page)
-- called every time ... shouldn't be a time issue needs between 40 and 80 microseconds
local langSuffix = frame:preprocess( '/{{PAGELANGUAGE}}' )
mw.log('Utilities.localize (langSuffix): '..langSuffix)
-- check if input is a number
if ( tonumber(strToTrans) ~= nil ) then
mw.log('Utilities.localize: Input String is a number, return it!')
return strToTrans
end
local strFromTrans = cargo.query( 'Localizations_'..page, 'text', {
where='id="'..strToTrans..langSuffix..'"',
format='template',
template='CargoOutput-NoFormatting',
default=''
})
-- local strFromTrans = frame:callParserFunction( '#lst', { 'Localization:'..page..langSuffix, strToTrans } )
-- did translation work?
if ( type(strFromTrans[1]) == 'table' and (strFromTrans[1].text and strFromTrans[1].text ~= '')) then
mw.log('Utilities.localize (strFromTrans[1].text): '..tostring(strFromTrans[1].text))
return strFromTrans[1].text
else
-- add to error category
-- [[Category:Missing Localization/{{{2|All}}}/{{{1|unsupplied}}}{{GetLangSuffix}}]]
local newErrorCategory = '[[Category:Missing Localization/'..page..'/'..strToTrans..langSuffix..']]'
local errorCategories = frame:callParserFunction( '#var', 'localizationErrorCategories' )
frame:callParserFunction( '#vardefine', { 'localizationErrorCategories', errorCategories..newErrorCategory } )
return strToTrans
end
end
function p.getNumber( str )
return tonumber(string.match(str, '%d+'))
end
function p.isSysOp( )
frame = mw.getCurrentFrame()
if (frame:preprocess('{{#ifsysop:true|false}}') == 'true') then
return true
else
return false
end
end
function p.isUserGroup( usrGroup )
-- usrGroup can be a comma seperated list of usrGroups
if (frame:preprocess('{{#ifingroup:'..usrGroup..'|true|false}}') == 'true') then
return true
else
return false
end
end
function p.getVar(varName)
local retVal = mw.getCurrentFrame():preprocess('{{#var:'..varName..'}}')
mw.log('Utilities.getVar: Get Variable "'..varName..'" returned the following value: '..tostring(retVal))
return retVal
end
-- this function adds the items of t2 to t1
function p.concatTable(t1, t2)
if (t1 == nil or #t1 == nil or
t2 == nil or #t2 == nil) then
return t1
end
for i=1, #t2 do
t1[#t1+1] = t2[i]
end
return t1
end
function p.outputNoWiki(frame)
return frame:preprocess('<nowiki>'..frame.args[1]..'</nowiki>')
end
function p.logger(frame)
return mw.log('-- Logger: |'..frame.args[1]..'|')
end
return p