implement official intllib fallback boilerplate

This commit is contained in:
codexp 2018-04-05 20:06:28 +02:00
parent 07916ff19f
commit 29aefc511f
2 changed files with 92 additions and 49 deletions

View File

@ -78,21 +78,19 @@
travelnet = {};
travelnet.targets = {};
travelnet.path = minetest.get_modpath(minetest.get_current_modname())
-- Boilerplate to support localized strings if intllib mod is installed.
if minetest.get_modpath( "intllib" ) and intllib then
travelnet.S = intllib.Getter()
else
travelnet.S = function(s) return s end
end
local S = travelnet.S;
-- Intllib
local S = dofile(travelnet.path .. "/intllib.lua")
travelnet.S = S
minetest.register_privilege("travelnet_attach", { description = S("allows to attach travelnet boxes to travelnets of other players"), give_to_singleplayer = false});
minetest.register_privilege("travelnet_remove", { description = S("allows to dig travelnet boxes which belog to nets of other players"), give_to_singleplayer = false});
-- read the configuration
dofile(minetest.get_modpath("travelnet").."/config.lua"); -- the normal, default travelnet
dofile(travelnet.path.."/config.lua"); -- the normal, default travelnet
@ -218,7 +216,7 @@ travelnet.reset_formspec = function( meta )
"field[0.3,2.8;9,0.9;station_network;"..S("Assign to Network:")..";"..
minetest.formspec_escape(station_network or "").."]"..
"label[0.3,3.1;"..S("You can have more than one network. If unsure, use \"%s\""):format(tostring(station_network)).."\".]"..
"label[0.3,3.1;"..S("You can have more than one network. If unsure, use \"%s\""):format(tostring(station_network))..".]"..
"field[0.3,4.4;9,0.9;owner;"..S("Owned by:")..";]"..
"label[0.3,4.7;"..S("Unless you know what you are doing, leave this empty.").."]"..
"button_exit[1.3,5.3;1.7,0.7;station_help_setup;"..S("Help").."]"..
@ -901,17 +899,17 @@ end
if( travelnet.travelnet_enabled ) then
dofile(minetest.get_modpath("travelnet").."/travelnet.lua"); -- the travelnet node definition
dofile(travelnet.path.."/travelnet.lua"); -- the travelnet node definition
end
if( travelnet.elevator_enabled ) then
dofile(minetest.get_modpath("travelnet").."/elevator.lua"); -- allows up/down transfers only
dofile(travelnet.path.."/elevator.lua"); -- allows up/down transfers only
end
if( travelnet.doors_enabled ) then
dofile(minetest.get_modpath("travelnet").."/doors.lua"); -- doors that open and close automaticly when the travelnet or elevator is used
dofile(travelnet.path.."/doors.lua"); -- doors that open and close automaticly when the travelnet or elevator is used
end
if( travelnet.abm_enabled ) then
dofile(minetest.get_modpath("travelnet").."/restore_network_via_abm.lua"); -- restore travelnet data when players pass by broken networks
dofile(travelnet.path.."/restore_network_via_abm.lua"); -- restore travelnet data when players pass by broken networks
end
-- upon server start, read the savefile

45
intllib.lua Normal file
View File

@ -0,0 +1,45 @@
-- Fallback functions for when `intllib` is not installed.
-- Code released under Unlicense <http://unlicense.org>.
-- Get the latest version of this file at:
-- https://raw.githubusercontent.com/minetest-mods/intllib/master/lib/intllib.lua
local function format(str, ...)
local args = { ... }
local function repl(escape, open, num, close)
if escape == "" then
local replacement = tostring(args[tonumber(num)])
if open == "" then
replacement = replacement..close
end
return replacement
else
return "@"..open..num..close
end
end
return (str:gsub("(@?)@(%(?)(%d+)(%)?)", repl))
end
local gettext, ngettext
if minetest.get_modpath("intllib") then
if intllib.make_gettext_pair then
-- New method using gettext.
gettext, ngettext = intllib.make_gettext_pair()
else
-- Old method using text files.
gettext = intllib.Getter()
end
end
-- Fill in missing functions.
gettext = gettext or function(msgid, ...)
return format(msgid, ...)
end
ngettext = ngettext or function(msgid, msgid_plural, n, ...)
return format(n==1 and msgid or msgid_plural, ...)
end
return gettext, ngettext