diff options
author | Wuzzy <wuzzy2@mail.ru> | 2021-03-05 15:27:33 +0000 |
---|---|---|
committer | SmallJoker <mk939@ymail.com> | 2021-03-05 16:29:48 +0100 |
commit | cafad6ac03348aa77e8ee4bb035840e73de4b2a9 (patch) | |
tree | bf4b24df4e92ea49ccccb19101671bb136abba12 /builtin/common | |
parent | ac8ac191691a13162667314358e96f07a65d0d1a (diff) | |
download | minetest-cafad6ac03348aa77e8ee4bb035840e73de4b2a9.tar.gz minetest-cafad6ac03348aa77e8ee4bb035840e73de4b2a9.tar.bz2 minetest-cafad6ac03348aa77e8ee4bb035840e73de4b2a9.zip |
Translate builtin (#10693)
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.
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/chatcommands.lua | 69 | ||||
-rw-r--r-- | builtin/common/information_formspecs.lua | 28 |
2 files changed, 57 insertions, 40 deletions
diff --git a/builtin/common/chatcommands.lua b/builtin/common/chatcommands.lua index 52edda659..c945e7bdb 100644 --- a/builtin/common/chatcommands.lua +++ b/builtin/common/chatcommands.lua @@ -1,5 +1,9 @@ -- 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 = {} function core.register_chatcommand(cmd, def) @@ -29,25 +33,12 @@ function core.override_chatcommand(name, redefinition) core.registered_chatcommands[name] = chatcommand 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 format_help_line(cmd, def) + local cmd_marker = "/" + if INIT == "client" then + cmd_marker = "." + end local msg = core.colorize("#00ffff", cmd_marker .. cmd) if def.params and def.params ~= "" then msg = msg .. " " .. def.params @@ -65,9 +56,21 @@ local function do_help_cmd(name, param) end end table.sort(cmds) - return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n" - .. gettext_replace("Use '$1help <cmd>' to get more information," - .. " or '$1help all' to list everything.", cmd_marker) + local msg + if INIT == "game" then + 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 local cmds = {} for cmd, def in pairs(core.registered_chatcommands) do @@ -76,19 +79,31 @@ local function do_help_cmd(name, param) end end 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 local privs = {} for priv, def in pairs(core.registered_privileges) do privs[#privs + 1] = priv .. ": " .. def.description end table.sort(privs) - return true, "Available privileges:\n"..table.concat(privs, "\n") + return true, S("Available privileges:").."\n"..table.concat(privs, "\n") else local cmd = param local def = core.registered_chatcommands[cmd] 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 return true, format_help_line(cmd, def) end @@ -97,16 +112,16 @@ end if INIT == "client" then core.register_chatcommand("help", { - params = gettext("[all | <cmd>]"), - description = gettext("Get help for commands"), + params = core.gettext("[all | <cmd>]"), + description = core.gettext("Get help for commands"), func = function(param) return do_help_cmd(nil, param) end, }) else core.register_chatcommand("help", { - params = "[all | privs | <cmd>]", - description = "Get help for commands or list privileges", + params = S("[all | privs | <cmd>]"), + description = S("Get help for commands or list privileges"), func = do_help_cmd, }) end diff --git a/builtin/common/information_formspecs.lua b/builtin/common/information_formspecs.lua index 3e2f1f079..e814b4c43 100644 --- a/builtin/common/information_formspecs.lua +++ b/builtin/common/information_formspecs.lua @@ -20,7 +20,8 @@ local LIST_FORMSPEC_DESCRIPTION = [[ 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 @@ -51,22 +52,23 @@ core.after(0, load_mod_command_tree) local function build_chatcommands_formspec(name, sel, copy) 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" .. - "Double-click to copy the entry to the chat history." + local description = S("For more information, click on " + .. "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 - 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 local has_priv = check_player_privs(name, cmds[2].privs) rows[#rows + 1] = ("%s,1,%s,%s"):format( 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 description = cmds[2].description 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)) end end @@ -74,9 +76,9 @@ local function build_chatcommands_formspec(name, sel, copy) end 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, - description, "Close" + F(description), F(S("Close")) ) end @@ -91,19 +93,19 @@ local function build_privs_formspec(name) table.sort(privs, function(a, b) return a[1] < b[1] end) 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) for i, data in ipairs(privs) do rows[#rows + 1] = ("%s,0,%s,%s"):format( player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY, - data[1], formspec_escape(data[2].description)) + data[1], F(data[2].description)) end return LIST_FORMSPEC:format( - "Available privileges:", + F(S("Available privileges:")), table.concat(rows, ","), - "Close" + F(S("Close")) ) end |