Skip to content

Commit 5bb5dba

Browse files
Lazerbeak12345luk3yx
andauthoredJun 11, 2023
feat: add locale support (luk3yx#9)
Co-authored-by: luk3yx <luk3yx@users.noreply.github.com>
1 parent 4ccabce commit 5bb5dba

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed
 

‎example.lua

+29-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- You can run /flow-example in singleplayer to open this form
22
local gui = flow.widgets
3+
local S = minetest.get_translator("flow")
34

45
local elements = {"box", "label", "image", "field", "checkbox", "list"}
56
local alignments = {"auto", "start", "end", "centre", "fill"}
@@ -58,19 +59,19 @@ local my_gui = flow.make_gui(function(player, ctx)
5859

5960
gui.HBox{
6061
gui.Image{w = 1, h = 1, texture_name = "air.png"},
61-
gui.Label{label = "Hello world!"},
62+
gui.Label{label = S"Hello world!"},
6263
},
63-
gui.Label{label="This is an example form."},
64+
gui.Label{label=S"This is an example form."},
6465
gui.Checkbox{
6566
name = "checkbox",
6667

6768
-- flow will detect that you have accessed ctx.form.checkbox and
6869
-- will automatically redraw the formspec if the value is changed.
69-
label = ctx.form.checkbox and "Uncheck me!" or "Check me!",
70+
label = ctx.form.checkbox and S"Uncheck me!" or S"Check me!",
7071
},
7172
gui.Button{
7273
-- Names are optional
73-
label = "Toggle checkbox",
74+
label = S"Toggle checkbox",
7475

7576
-- Important: Do not use the `player` and `ctx` variables from the
7677
-- above formspec.
@@ -79,14 +80,14 @@ local my_gui = flow.make_gui(function(player, ctx)
7980
ctx.form.checkbox = not ctx.form.checkbox
8081

8182
-- Send a chat message
82-
minetest.chat_send_player(player:get_player_name(), "Toggled!")
83+
minetest.chat_send_player(player:get_player_name(), S"Toggled!")
8384

8485
-- Return true to tell flow to redraw the formspec
8586
return true
8687
end,
8788
},
8889

89-
gui.Label{label="A demonstration of expansion:"},
90+
gui.Label{label=S"A demonstration of expansion:"},
9091

9192
-- The finer details of scroll containers are handled automatically.
9293
-- Clients that don't support scroll_container[] will see a paginator
@@ -98,8 +99,8 @@ local my_gui = flow.make_gui(function(player, ctx)
9899
-- order changes.
99100
name = "vbox1",
100101

101-
gui.Label{label="By default, objects do not expand\nin the " ..
102-
"same direction as the hbox/vbox:"},
102+
gui.Label{label=S("By default, objects do not expand\nin the " ..
103+
"same direction as the hbox/vbox:")},
103104
gui.HBox{
104105
gui.Box{
105106
w = 1,
@@ -108,8 +109,10 @@ local my_gui = flow.make_gui(function(player, ctx)
108109
},
109110
},
110111

111-
gui.Label{label="Items are expanded in the opposite\ndirection," ..
112-
" however:"},
112+
gui.Label{
113+
label=S("Items are expanded in the opposite\ndirection,"
114+
.. " however:")
115+
},
113116
gui.HBox{
114117
min_h = 2,
115118
gui.Box{
@@ -119,8 +122,8 @@ local my_gui = flow.make_gui(function(player, ctx)
119122
},
120123
},
121124

122-
gui.Label{label="To automatically expand an object, add\n" ..
123-
"`expand = true` to its definition."},
125+
gui.Label{label=S("To automatically expand an object, add\n" ..
126+
"`expand = true` to its definition.")},
124127
gui.HBox{
125128
gui.Box{
126129
w = 1,
@@ -130,8 +133,8 @@ local my_gui = flow.make_gui(function(player, ctx)
130133
},
131134
},
132135

133-
gui.Label{label="Multiple expanded items will share the\n" ..
134-
"remaining space evenly."},
136+
gui.Label{label=S("Multiple expanded items will share the\n" ..
137+
"remaining space evenly.")},
135138

136139
gui.HBox{
137140
gui.Box{
@@ -164,10 +167,10 @@ local my_gui = flow.make_gui(function(player, ctx)
164167
},
165168
},
166169

167-
gui.Label{label="Try it yourself!"},
170+
gui.Label{label=S"Try it yourself!"},
168171
gui.HBox{
169172
gui.VBox{
170-
gui.Label{label="Element:"},
173+
gui.Label{label=S"Element:"},
171174
gui.Dropdown{
172175
name = "element",
173176
items = elements,
@@ -195,12 +198,18 @@ local my_gui = flow.make_gui(function(player, ctx)
195198
},
196199
gui.HBox{
197200
gui.VBox{
198-
gui.Checkbox{name = "expand", label = "Expand"},
199-
gui.Checkbox{name = "box2", label = "Second box"},
201+
gui.Checkbox{name = "expand", label = S"Expand"},
202+
gui.Checkbox{name = "box2", label = S"Second box"},
200203
},
201204
gui.VBox{
202-
gui.Checkbox{name = "vbox", label = "Use vbox instead of hbox"},
203-
gui.Checkbox{name = "expand_box2", label = "Expand second box"},
205+
gui.Checkbox{
206+
name = "vbox",
207+
label = S"Use vbox instead of hbox"
208+
},
209+
gui.Checkbox{
210+
name = "expand_box2",
211+
label = S"Expand second box"
212+
},
204213
},
205214
},
206215
try_it_yourself_box,

‎init.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
local DEBUG_MODE = false
2121
flow = {}
22+
local S = minetest.get_translator("flow")
2223

2324

2425
local Form = {}
@@ -1074,7 +1075,7 @@ function gui.PaginatedVBox(def)
10741075
end,
10751076
},
10761077
gui.Label {
1077-
label = "Page " .. current_page .. " of " .. #pages,
1078+
label = S("Page @1 of @2", current_page, #pages),
10781079
align_h = "centre",
10791080
expand = true,
10801081
},
@@ -1206,7 +1207,7 @@ if minetest.is_singleplayer() then
12061207
local example_form
12071208
minetest.register_chatcommand("flow-example", {
12081209
privs = {server = true},
1209-
help = "Shows an example formspec",
1210+
help = S"Shows an example form",
12101211
func = function(name)
12111212
-- Only load example.lua when it's needed
12121213
if not example_form then

‎locale/template.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# textdomain: flow
2+
3+
### init.lua ###
4+
Page @1 of @2=
5+
Shows an example form=
6+
7+
### example.lua ###
8+
Hello world!=
9+
This is an example form.=
10+
11+
# checkbox labels and states
12+
Uncheck me!=
13+
Check me!=
14+
Toggle checkbox=
15+
Toggled!=
16+
17+
# expansion
18+
A demonstration of expansion:=
19+
By default, objects do not expand@nin the same direction as the hbox/vbox:=
20+
Items are expanded in the opposite@ndirection, however:=
21+
To automatically expand an object, add@n`expand = true` to its definition.=
22+
Multiple expanded items will share the@nremaining space evenly.=
23+
24+
# Demo
25+
Try it yourself!=
26+
Element:=
27+
Expand=
28+
Second box=
29+
Use vbox instead of hbox=
30+
Expand second box=

‎test.lua

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ minetest.is_singleplayer = dummy
2525
minetest.get_player_information = dummy
2626
minetest.show_formspec = dummy
2727

28+
function minetest.get_translator(modname)
29+
assert(modname == "flow")
30+
return function(str) return str end
31+
end
32+
2833
-- Stub minetest player api
2934
local function stub_player(name)
3035
assert(type(name) == "string")

0 commit comments

Comments
 (0)
Please sign in to comment.