Module:Util/strings/interpolate

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

interpolate(str, tbl)

Approximation of string interpolation

Parameters

Returns

  • The formatted string.

Examples

#InputOutputStatus
1
interpolate(
  "${wiki} is a ${franchise} encyclopedia that anyone can edit.",
  {
    wiki = "Zelda Wiki",
    franchise = "Zelda",
  }
)
"Zelda Wiki is a Zelda encyclopedia that anyone can edit."

-- By http://lua-users.org/wiki/RiciLake
local function interpolate(str, tbl)
	local outStr = string.gsub(str, '($%b{})', function(w) 
		return tbl[w:sub(3, -2)] or w 
	end)
	return outStr
end

return interpolate