Skip to content

Commit

Permalink
Translatate builtin (#10693)
Browse files Browse the repository at this point in the history
This PR is the second attempt to translate builtin.
Server-sent translation files can be added to `builtin/locale/`, whereas client-side translations depend on gettext.
  • Loading branch information
Wuzzy2 committed Mar 5, 2021
1 parent ac8ac19 commit ea4c8a7
Show file tree
Hide file tree
Showing 12 changed files with 622 additions and 333 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ src/test_config.h
src/cmake_config.h src/cmake_config.h
src/cmake_config_githash.h src/cmake_config_githash.h
src/unittest/test_world/world.mt src/unittest/test_world/world.mt
src/lua/build/ /locale/
locale/
.directory .directory
*.cbp *.cbp
*.layout *.layout
Expand Down
9 changes: 4 additions & 5 deletions builtin/client/chatcommands.lua
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,13 @@
-- Minetest: builtin/client/chatcommands.lua -- Minetest: builtin/client/chatcommands.lua



core.register_on_sending_chat_message(function(message) core.register_on_sending_chat_message(function(message)
if message:sub(1,2) == ".." then if message:sub(1,2) == ".." then
return false return false
end end


local first_char = message:sub(1,1) local first_char = message:sub(1,1)
if first_char == "/" or first_char == "." then if first_char == "/" or first_char == "." then
core.display_chat_message(core.gettext("issued command: ") .. message) core.display_chat_message(core.gettext("Issued command: ") .. message)
end end


if first_char ~= "." then if first_char ~= "." then
Expand All @@ -19,7 +18,7 @@ core.register_on_sending_chat_message(function(message)
param = param or "" param = param or ""


if not cmd then if not cmd then
core.display_chat_message(core.gettext("-!- Empty command")) core.display_chat_message("-!- " .. core.gettext("Empty command."))
return true return true
end end


Expand All @@ -36,7 +35,7 @@ core.register_on_sending_chat_message(function(message)
core.display_chat_message(result) core.display_chat_message(result)
end end
else else
core.display_chat_message(core.gettext("-!- Invalid command: ") .. cmd) core.display_chat_message("-!- " .. core.gettext("Invalid command: ") .. cmd)
end end


return true return true
Expand Down Expand Up @@ -66,7 +65,7 @@ core.register_chatcommand("clear_chat_queue", {
description = core.gettext("Clear the out chat queue"), description = core.gettext("Clear the out chat queue"),
func = function(param) func = function(param)
core.clear_out_chat_queue() core.clear_out_chat_queue()
return true, core.gettext("The out chat queue is now empty") return true, core.gettext("The out chat queue is now empty.")
end, end,
}) })


Expand Down
2 changes: 1 addition & 1 deletion builtin/client/death_formspec.lua
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- handled by the engine. -- handled by the engine.


core.register_on_death(function() core.register_on_death(function()
core.display_chat_message("You died.") core.display_chat_message(core.gettext("You died."))
local formspec = "size[11,5.5]bgcolor[#320000b4;true]" .. local formspec = "size[11,5.5]bgcolor[#320000b4;true]" ..
"label[4.85,1.35;" .. fgettext("You died") .. "label[4.85,1.35;" .. fgettext("You died") ..
"]button_exit[4,3;3,0.5;btn_respawn;".. fgettext("Respawn") .."]" "]button_exit[4,3;3,0.5;btn_respawn;".. fgettext("Respawn") .."]"
Expand Down
69 changes: 42 additions & 27 deletions builtin/common/chatcommands.lua
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,9 @@
-- Minetest: builtin/common/chatcommands.lua -- Minetest: builtin/common/chatcommands.lua


-- For server-side translations (if INIT == "game")
-- Otherwise, use core.gettext
local S = core.get_translator("__builtin")

core.registered_chatcommands = {} core.registered_chatcommands = {}


function core.register_chatcommand(cmd, def) function core.register_chatcommand(cmd, def)
Expand Down Expand Up @@ -29,25 +33,12 @@ function core.override_chatcommand(name, redefinition)
core.registered_chatcommands[name] = chatcommand core.registered_chatcommands[name] = chatcommand
end end


local cmd_marker = "/"

local function gettext(...)
return ...
end

local function gettext_replace(text, replace)
return text:gsub("$1", replace)
end


if INIT == "client" then
cmd_marker = "."
gettext = core.gettext
gettext_replace = fgettext_ne
end

local function do_help_cmd(name, param) local function do_help_cmd(name, param)
local function format_help_line(cmd, def) local function format_help_line(cmd, def)
local cmd_marker = "/"
if INIT == "client" then
cmd_marker = "."
end
local msg = core.colorize("#00ffff", cmd_marker .. cmd) local msg = core.colorize("#00ffff", cmd_marker .. cmd)
if def.params and def.params ~= "" then if def.params and def.params ~= "" then
msg = msg .. " " .. def.params msg = msg .. " " .. def.params
Expand All @@ -65,9 +56,21 @@ local function do_help_cmd(name, param)
end end
end end
table.sort(cmds) table.sort(cmds)
return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n" local msg
.. gettext_replace("Use '$1help <cmd>' to get more information," if INIT == "game" then
.. " or '$1help all' to list everything.", cmd_marker) msg = S("Available commands: @1",
table.concat(cmds, " ")) .. "\n"
.. S("Use '/help <cmd>' to get more "
.. "information, or '/help all' to list "
.. "everything.")
else
msg = core.gettext("Available commands: ")
.. table.concat(cmds, " ") .. "\n"
.. core.gettext("Use '.help <cmd>' to get more "
.. "information, or '.help all' to list "
.. "everything.")
end
return true, msg
elseif param == "all" then elseif param == "all" then
local cmds = {} local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do for cmd, def in pairs(core.registered_chatcommands) do
Expand All @@ -76,19 +79,31 @@ local function do_help_cmd(name, param)
end end
end end
table.sort(cmds) table.sort(cmds)
return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n") local msg
if INIT == "game" then
msg = S("Available commands:")
else
msg = core.gettext("Available commands:")
end
return true, msg.."\n"..table.concat(cmds, "\n")
elseif INIT == "game" and param == "privs" then elseif INIT == "game" and param == "privs" then
local privs = {} local privs = {}
for priv, def in pairs(core.registered_privileges) do for priv, def in pairs(core.registered_privileges) do
privs[#privs + 1] = priv .. ": " .. def.description privs[#privs + 1] = priv .. ": " .. def.description
end end
table.sort(privs) table.sort(privs)
return true, "Available privileges:\n"..table.concat(privs, "\n") return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
else else
local cmd = param local cmd = param
local def = core.registered_chatcommands[cmd] local def = core.registered_chatcommands[cmd]
if not def then if not def then
return false, gettext("Command not available: ")..cmd local msg
if INIT == "game" then
msg = S("Command not available: @1", cmd)
else
msg = core.gettext("Command not available: ") .. cmd
end
return false, msg
else else
return true, format_help_line(cmd, def) return true, format_help_line(cmd, def)
end end
Expand All @@ -97,16 +112,16 @@ end


if INIT == "client" then if INIT == "client" then
core.register_chatcommand("help", { core.register_chatcommand("help", {
params = gettext("[all | <cmd>]"), params = core.gettext("[all | <cmd>]"),
description = gettext("Get help for commands"), description = core.gettext("Get help for commands"),
func = function(param) func = function(param)
return do_help_cmd(nil, param) return do_help_cmd(nil, param)
end, end,
}) })
else else
core.register_chatcommand("help", { core.register_chatcommand("help", {
params = "[all | privs | <cmd>]", params = S("[all | privs | <cmd>]"),
description = "Get help for commands or list privileges", description = S("Get help for commands or list privileges"),
func = do_help_cmd, func = do_help_cmd,
}) })
end end
28 changes: 15 additions & 13 deletions builtin/common/information_formspecs.lua
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ local LIST_FORMSPEC_DESCRIPTION = [[
button_exit[5,7;3,1;quit;%s] button_exit[5,7;3,1;quit;%s]
]] ]]


local formspec_escape = core.formspec_escape local F = core.formspec_escape
local S = core.get_translator("__builtin")
local check_player_privs = core.check_player_privs local check_player_privs = core.check_player_privs




Expand Down Expand Up @@ -51,32 +52,33 @@ core.after(0, load_mod_command_tree)


local function build_chatcommands_formspec(name, sel, copy) local function build_chatcommands_formspec(name, sel, copy)
local rows = {} local rows = {}
rows[1] = "#FFF,0,Command,Parameters" rows[1] = "#FFF,0,"..F(S("Command"))..","..F(S("Parameters"))


local description = "For more information, click on any entry in the list.\n" .. local description = S("For more information, click on "
"Double-click to copy the entry to the chat history." .. "any entry in the list.").. "\n" ..
S("Double-click to copy the entry to the chat history.")


for i, data in ipairs(mod_cmds) do for i, data in ipairs(mod_cmds) do
rows[#rows + 1] = COLOR_BLUE .. ",0," .. formspec_escape(data[1]) .. "," rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
for j, cmds in ipairs(data[2]) do for j, cmds in ipairs(data[2]) do
local has_priv = check_player_privs(name, cmds[2].privs) local has_priv = check_player_privs(name, cmds[2].privs)
rows[#rows + 1] = ("%s,1,%s,%s"):format( rows[#rows + 1] = ("%s,1,%s,%s"):format(
has_priv and COLOR_GREEN or COLOR_GRAY, has_priv and COLOR_GREEN or COLOR_GRAY,
cmds[1], formspec_escape(cmds[2].params)) cmds[1], F(cmds[2].params))
if sel == #rows then if sel == #rows then
description = cmds[2].description description = cmds[2].description
if copy then if copy then
core.chat_send_player(name, ("Command: %s %s"):format( core.chat_send_player(name, S("Command: @1 @2",
core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params)) core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params))
end end
end end
end end
end end


return LIST_FORMSPEC_DESCRIPTION:format( return LIST_FORMSPEC_DESCRIPTION:format(
"Available commands: (see also: /help <cmd>)", F(S("Available commands: (see also: /help <cmd>)")),
table.concat(rows, ","), sel or 0, table.concat(rows, ","), sel or 0,
description, "Close" F(description), F(S("Close"))
) )
end end


Expand All @@ -91,19 +93,19 @@ local function build_privs_formspec(name)
table.sort(privs, function(a, b) return a[1] < b[1] end) table.sort(privs, function(a, b) return a[1] < b[1] end)


local rows = {} local rows = {}
rows[1] = "#FFF,0,Privilege,Description" rows[1] = "#FFF,0,"..F(S("Privilege"))..","..F(S("Description"))


local player_privs = core.get_player_privs(name) local player_privs = core.get_player_privs(name)
for i, data in ipairs(privs) do for i, data in ipairs(privs) do
rows[#rows + 1] = ("%s,0,%s,%s"):format( rows[#rows + 1] = ("%s,0,%s,%s"):format(
player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY, player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
data[1], formspec_escape(data[2].description)) data[1], F(data[2].description))
end end


return LIST_FORMSPEC:format( return LIST_FORMSPEC:format(
"Available privileges:", F(S("Available privileges:")),
table.concat(rows, ","), table.concat(rows, ","),
"Close" F(S("Close"))
) )
end end


Expand Down
Loading

0 comments on commit ea4c8a7

Please sign in to comment.