Skip to content

Commit cafad6a

Browse files
Wuzzy2SmallJoker
authored andcommitted
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.
1 parent ac8ac19 commit cafad6a

File tree

12 files changed

+622
-333
lines changed

12 files changed

+622
-333
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ src/test_config.h
8686
src/cmake_config.h
8787
src/cmake_config_githash.h
8888
src/unittest/test_world/world.mt
89-
src/lua/build/
90-
locale/
89+
/locale/
9190
.directory
9291
*.cbp
9392
*.layout

builtin/client/chatcommands.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
-- Minetest: builtin/client/chatcommands.lua
22

3-
43
core.register_on_sending_chat_message(function(message)
54
if message:sub(1,2) == ".." then
65
return false
76
end
87

98
local first_char = message:sub(1,1)
109
if first_char == "/" or first_char == "." then
11-
core.display_chat_message(core.gettext("issued command: ") .. message)
10+
core.display_chat_message(core.gettext("Issued command: ") .. message)
1211
end
1312

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

2120
if not cmd then
22-
core.display_chat_message(core.gettext("-!- Empty command"))
21+
core.display_chat_message("-!- " .. core.gettext("Empty command."))
2322
return true
2423
end
2524

@@ -36,7 +35,7 @@ core.register_on_sending_chat_message(function(message)
3635
core.display_chat_message(result)
3736
end
3837
else
39-
core.display_chat_message(core.gettext("-!- Invalid command: ") .. cmd)
38+
core.display_chat_message("-!- " .. core.gettext("Invalid command: ") .. cmd)
4039
end
4140

4241
return true
@@ -66,7 +65,7 @@ core.register_chatcommand("clear_chat_queue", {
6665
description = core.gettext("Clear the out chat queue"),
6766
func = function(param)
6867
core.clear_out_chat_queue()
69-
return true, core.gettext("The out chat queue is now empty")
68+
return true, core.gettext("The out chat queue is now empty.")
7069
end,
7170
})
7271

builtin/client/death_formspec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- handled by the engine.
33

44
core.register_on_death(function()
5-
core.display_chat_message("You died.")
5+
core.display_chat_message(core.gettext("You died."))
66
local formspec = "size[11,5.5]bgcolor[#320000b4;true]" ..
77
"label[4.85,1.35;" .. fgettext("You died") ..
88
"]button_exit[4,3;3,0.5;btn_respawn;".. fgettext("Respawn") .."]"

builtin/common/chatcommands.lua

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
-- Minetest: builtin/common/chatcommands.lua
22

3+
-- For server-side translations (if INIT == "game")
4+
-- Otherwise, use core.gettext
5+
local S = core.get_translator("__builtin")
6+
37
core.registered_chatcommands = {}
48

59
function core.register_chatcommand(cmd, def)
@@ -29,25 +33,12 @@ function core.override_chatcommand(name, redefinition)
2933
core.registered_chatcommands[name] = chatcommand
3034
end
3135

32-
local cmd_marker = "/"
33-
34-
local function gettext(...)
35-
return ...
36-
end
37-
38-
local function gettext_replace(text, replace)
39-
return text:gsub("$1", replace)
40-
end
41-
42-
43-
if INIT == "client" then
44-
cmd_marker = "."
45-
gettext = core.gettext
46-
gettext_replace = fgettext_ne
47-
end
48-
4936
local function do_help_cmd(name, param)
5037
local function format_help_line(cmd, def)
38+
local cmd_marker = "/"
39+
if INIT == "client" then
40+
cmd_marker = "."
41+
end
5142
local msg = core.colorize("#00ffff", cmd_marker .. cmd)
5243
if def.params and def.params ~= "" then
5344
msg = msg .. " " .. def.params
@@ -65,9 +56,21 @@ local function do_help_cmd(name, param)
6556
end
6657
end
6758
table.sort(cmds)
68-
return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
69-
.. gettext_replace("Use '$1help <cmd>' to get more information,"
70-
.. " or '$1help all' to list everything.", cmd_marker)
59+
local msg
60+
if INIT == "game" then
61+
msg = S("Available commands: @1",
62+
table.concat(cmds, " ")) .. "\n"
63+
.. S("Use '/help <cmd>' to get more "
64+
.. "information, or '/help all' to list "
65+
.. "everything.")
66+
else
67+
msg = core.gettext("Available commands: ")
68+
.. table.concat(cmds, " ") .. "\n"
69+
.. core.gettext("Use '.help <cmd>' to get more "
70+
.. "information, or '.help all' to list "
71+
.. "everything.")
72+
end
73+
return true, msg
7174
elseif param == "all" then
7275
local cmds = {}
7376
for cmd, def in pairs(core.registered_chatcommands) do
@@ -76,19 +79,31 @@ local function do_help_cmd(name, param)
7679
end
7780
end
7881
table.sort(cmds)
79-
return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
82+
local msg
83+
if INIT == "game" then
84+
msg = S("Available commands:")
85+
else
86+
msg = core.gettext("Available commands:")
87+
end
88+
return true, msg.."\n"..table.concat(cmds, "\n")
8089
elseif INIT == "game" and param == "privs" then
8190
local privs = {}
8291
for priv, def in pairs(core.registered_privileges) do
8392
privs[#privs + 1] = priv .. ": " .. def.description
8493
end
8594
table.sort(privs)
86-
return true, "Available privileges:\n"..table.concat(privs, "\n")
95+
return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
8796
else
8897
local cmd = param
8998
local def = core.registered_chatcommands[cmd]
9099
if not def then
91-
return false, gettext("Command not available: ")..cmd
100+
local msg
101+
if INIT == "game" then
102+
msg = S("Command not available: @1", cmd)
103+
else
104+
msg = core.gettext("Command not available: ") .. cmd
105+
end
106+
return false, msg
92107
else
93108
return true, format_help_line(cmd, def)
94109
end
@@ -97,16 +112,16 @@ end
97112

98113
if INIT == "client" then
99114
core.register_chatcommand("help", {
100-
params = gettext("[all | <cmd>]"),
101-
description = gettext("Get help for commands"),
115+
params = core.gettext("[all | <cmd>]"),
116+
description = core.gettext("Get help for commands"),
102117
func = function(param)
103118
return do_help_cmd(nil, param)
104119
end,
105120
})
106121
else
107122
core.register_chatcommand("help", {
108-
params = "[all | privs | <cmd>]",
109-
description = "Get help for commands or list privileges",
123+
params = S("[all | privs | <cmd>]"),
124+
description = S("Get help for commands or list privileges"),
110125
func = do_help_cmd,
111126
})
112127
end

builtin/common/information_formspecs.lua

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ local LIST_FORMSPEC_DESCRIPTION = [[
2020
button_exit[5,7;3,1;quit;%s]
2121
]]
2222

23-
local formspec_escape = core.formspec_escape
23+
local F = core.formspec_escape
24+
local S = core.get_translator("__builtin")
2425
local check_player_privs = core.check_player_privs
2526

2627

@@ -51,32 +52,33 @@ core.after(0, load_mod_command_tree)
5152

5253
local function build_chatcommands_formspec(name, sel, copy)
5354
local rows = {}
54-
rows[1] = "#FFF,0,Command,Parameters"
55+
rows[1] = "#FFF,0,"..F(S("Command"))..","..F(S("Parameters"))
5556

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

5961
for i, data in ipairs(mod_cmds) do
60-
rows[#rows + 1] = COLOR_BLUE .. ",0," .. formspec_escape(data[1]) .. ","
62+
rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
6163
for j, cmds in ipairs(data[2]) do
6264
local has_priv = check_player_privs(name, cmds[2].privs)
6365
rows[#rows + 1] = ("%s,1,%s,%s"):format(
6466
has_priv and COLOR_GREEN or COLOR_GRAY,
65-
cmds[1], formspec_escape(cmds[2].params))
67+
cmds[1], F(cmds[2].params))
6668
if sel == #rows then
6769
description = cmds[2].description
6870
if copy then
69-
core.chat_send_player(name, ("Command: %s %s"):format(
71+
core.chat_send_player(name, S("Command: @1 @2",
7072
core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params))
7173
end
7274
end
7375
end
7476
end
7577

7678
return LIST_FORMSPEC_DESCRIPTION:format(
77-
"Available commands: (see also: /help <cmd>)",
79+
F(S("Available commands: (see also: /help <cmd>)")),
7880
table.concat(rows, ","), sel or 0,
79-
description, "Close"
81+
F(description), F(S("Close"))
8082
)
8183
end
8284

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

9395
local rows = {}
94-
rows[1] = "#FFF,0,Privilege,Description"
96+
rows[1] = "#FFF,0,"..F(S("Privilege"))..","..F(S("Description"))
9597

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

103105
return LIST_FORMSPEC:format(
104-
"Available privileges:",
106+
F(S("Available privileges:")),
105107
table.concat(rows, ","),
106-
"Close"
108+
F(S("Close"))
107109
)
108110
end
109111

0 commit comments

Comments
 (0)