Difference between revisions of "Module:Utilities"

From Pathfinder Wiki
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
function p.localize( strToTrans, langSuffix, page )
+
function p.localize( strToTrans, page )
 
local frame = mw.getCurrentFrame()
 
local frame = mw.getCurrentFrame()
 
if ( page == nil or page == '' ) then
 
if ( page == nil or page == '' ) then
Line 7: Line 7:
 
end
 
end
 
local timeStart = os.clock()
 
local timeStart = os.clock()
globalLangSuffix = frame:expandTemplate{ title = 'GetLangSuffix' }
+
local langSuffix = frame:expandTemplate{ title = 'GetLangSuffix' }
 
local timeEnd = os.clock()
 
local timeEnd = os.clock()
 
mw.log('Utilities:localize (globalLangSuffix, timeNeeded): '..globalLangSuffix..', '..timeEnd-timeStart)
 
mw.log('Utilities:localize (globalLangSuffix, timeNeeded): '..globalLangSuffix..', '..timeEnd-timeStart)

Revision as of 21:12, 16 July 2021

Documentation for this module may be created at Module:Utilities/doc

local p = {}

function p.localize( strToTrans, page )
	local frame = mw.getCurrentFrame()
	if ( page == nil or page == '' ) then
		page = 'All'
	end
	local timeStart = os.clock()
	local langSuffix = frame:expandTemplate{ title = 'GetLangSuffix' }
	local timeEnd = os.clock()
	mw.log('Utilities:localize (globalLangSuffix, timeNeeded): '..globalLangSuffix..', '..timeEnd-timeStart)
	
	-- check if input is a number
	if ( tonumber(strToTrans) == nil ) then
		return strToTrans
	end
	
	local strFromTrans = frame:callParserFunction( '#lst', { 'Localization:'..page..langSuffix, strToTrans})
	-- did translation work?
	if ( strFromTrans ~= '') then
		return strFromTrans
	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 nil
	end
	
end

return p