Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:TNTFallback: Difference between revisions

From Neodyland Wiki
Create Module:TNTFallback in order to ease transition of {{Documentation}} to ext.translate+TNT
 
msg: Add support for blank `fallback` paramter
Line 23: Line 23:
local params = { n = 0 };
local params = { n = 0 };
local lang = nil;
local lang = nil;
local fallback = "";
local fallback = nil;


for k, v in pairs(frame.args) do
for k, v in pairs(frame.args) do
Line 36: Line 36:
elseif ((k == "lang") and (v ~= "_")) then
elseif ((k == "lang") and (v ~= "_")) then
lang = v;
lang = v;
elseif (k == "fallback") then
elseif ((k == "fallback") and (v ~= "")) then
fallback = v;
fallback = v;
end
end
Line 48: Line 48:
end
end


if (
if (fallback and (
-- not translated
-- not translated
(result and result == TNT.formatInLanguage("en", dataset, key, unpack(params, 1, params.n)))
(result and result == TNT.formatInLanguage("en", dataset, key, unpack(params, 1, params.n)))
-- no message
-- no message
or (not result)
or (not result)
) then
)) then
if (not lang) then
if (not lang) then
if (frame:callParserFunction("int:lang") ~= "en") then
if (frame:callParserFunction("int:lang") ~= "en") then

Revision as of 11:41, 4 October 2021

Lua error in package.lua at line 80: module 'Module:No globals' not found.[ create · Lua error in package.lua at line 80: module 'Module:No globals' not found. ]
This documentation is transcluded from Module:TNTFallback/doc.
--------------------------------------------------------------------------------
-- This module implements a wrapper for [[Module:TNT]] that allows returning
-- a fallback message; used by {{Documentation}}.
--
-- @module TNTFallback
-- @alias  p
-- @author [[User:ExE Boss]]
-- @require [[Module:No globals]]
-- @require [[Module:TNT]]
--------------------------------------------------------------------------------

require("Module:No globals");
local TNT = require("Module:TNT");

local p = {};

--------------------------------------------------------------------------------
-- Based on [[Module:TNT]]'s `msg` function,
-- but takes an optional `fallback` parameter.
--------------------------------------------------------------------------------
function p.msg(frame)
	local dataset, key;
	local params = { n = 0 };
	local lang = nil;
	local fallback = nil;

	for k, v in pairs(frame.args) do
		if (k == 1) then
			dataset = v;
		elseif (k == 2) then
			key = v;
		elseif (type(k) == "number" and k > 2) then
			local i = k - 2;
			params[i] = v;
			params.n = math.max(params.n, i);
		elseif ((k == "lang") and (v ~= "_")) then
			lang = v;
		elseif ((k == "fallback") and (v ~= "")) then
			fallback = v;
		end
	end

	local result;
	if (lang) then
		result = TNT.formatInLanguage(lang, dataset, key, unpack(params, 1, params.n));
	else
		result = TNT.format(dataset, key, unpack(params, 1, params.n));
	end

	if (fallback and (
		-- not translated
		(result and result == TNT.formatInLanguage("en", dataset, key, unpack(params, 1, params.n)))
		-- no message
		or (not result)
	)) then
		if (not lang) then
			if (frame:callParserFunction("int:lang") ~= "en") then
				return mw.message.newRawMessage(fallback, unpack(params, 1, params.n)):plain();
			end
		elseif (lang ~= "en") then
			return mw.message.newRawMessage(fallback, unpack(params, 1, params.n)):plain();
		end
	end

	return result;
end

return p;
Cookies help us deliver our services. By using our services, you agree to our use of cookies.