feat: add locale support (#9)

Co-authored-by: luk3yx <luk3yx@users.noreply.github.com>
This commit is contained in:
Lazerbeak12345 2023-06-11 04:10:19 -06:00 committed by GitHub
parent 4ccabcef0f
commit 5bb5dba625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 22 deletions

View File

@ -1,5 +1,6 @@
-- You can run /flow-example in singleplayer to open this form -- You can run /flow-example in singleplayer to open this form
local gui = flow.widgets local gui = flow.widgets
local S = minetest.get_translator("flow")
local elements = {"box", "label", "image", "field", "checkbox", "list"} local elements = {"box", "label", "image", "field", "checkbox", "list"}
local alignments = {"auto", "start", "end", "centre", "fill"} local alignments = {"auto", "start", "end", "centre", "fill"}
@ -58,19 +59,19 @@ local my_gui = flow.make_gui(function(player, ctx)
gui.HBox{ gui.HBox{
gui.Image{w = 1, h = 1, texture_name = "air.png"}, gui.Image{w = 1, h = 1, texture_name = "air.png"},
gui.Label{label = "Hello world!"}, gui.Label{label = S"Hello world!"},
}, },
gui.Label{label="This is an example form."}, gui.Label{label=S"This is an example form."},
gui.Checkbox{ gui.Checkbox{
name = "checkbox", name = "checkbox",
-- flow will detect that you have accessed ctx.form.checkbox and -- flow will detect that you have accessed ctx.form.checkbox and
-- will automatically redraw the formspec if the value is changed. -- will automatically redraw the formspec if the value is changed.
label = ctx.form.checkbox and "Uncheck me!" or "Check me!", label = ctx.form.checkbox and S"Uncheck me!" or S"Check me!",
}, },
gui.Button{ gui.Button{
-- Names are optional -- Names are optional
label = "Toggle checkbox", label = S"Toggle checkbox",
-- Important: Do not use the `player` and `ctx` variables from the -- Important: Do not use the `player` and `ctx` variables from the
-- above formspec. -- above formspec.
@ -79,14 +80,14 @@ local my_gui = flow.make_gui(function(player, ctx)
ctx.form.checkbox = not ctx.form.checkbox ctx.form.checkbox = not ctx.form.checkbox
-- Send a chat message -- Send a chat message
minetest.chat_send_player(player:get_player_name(), "Toggled!") minetest.chat_send_player(player:get_player_name(), S"Toggled!")
-- Return true to tell flow to redraw the formspec -- Return true to tell flow to redraw the formspec
return true return true
end, end,
}, },
gui.Label{label="A demonstration of expansion:"}, gui.Label{label=S"A demonstration of expansion:"},
-- The finer details of scroll containers are handled automatically. -- The finer details of scroll containers are handled automatically.
-- Clients that don't support scroll_container[] will see a paginator -- Clients that don't support scroll_container[] will see a paginator
@ -98,8 +99,8 @@ local my_gui = flow.make_gui(function(player, ctx)
-- order changes. -- order changes.
name = "vbox1", name = "vbox1",
gui.Label{label="By default, objects do not expand\nin the " .. gui.Label{label=S("By default, objects do not expand\nin the " ..
"same direction as the hbox/vbox:"}, "same direction as the hbox/vbox:")},
gui.HBox{ gui.HBox{
gui.Box{ gui.Box{
w = 1, w = 1,
@ -108,8 +109,10 @@ local my_gui = flow.make_gui(function(player, ctx)
}, },
}, },
gui.Label{label="Items are expanded in the opposite\ndirection," .. gui.Label{
" however:"}, label=S("Items are expanded in the opposite\ndirection,"
.. " however:")
},
gui.HBox{ gui.HBox{
min_h = 2, min_h = 2,
gui.Box{ gui.Box{
@ -119,8 +122,8 @@ local my_gui = flow.make_gui(function(player, ctx)
}, },
}, },
gui.Label{label="To automatically expand an object, add\n" .. gui.Label{label=S("To automatically expand an object, add\n" ..
"`expand = true` to its definition."}, "`expand = true` to its definition.")},
gui.HBox{ gui.HBox{
gui.Box{ gui.Box{
w = 1, w = 1,
@ -130,8 +133,8 @@ local my_gui = flow.make_gui(function(player, ctx)
}, },
}, },
gui.Label{label="Multiple expanded items will share the\n" .. gui.Label{label=S("Multiple expanded items will share the\n" ..
"remaining space evenly."}, "remaining space evenly.")},
gui.HBox{ gui.HBox{
gui.Box{ gui.Box{
@ -164,10 +167,10 @@ local my_gui = flow.make_gui(function(player, ctx)
}, },
}, },
gui.Label{label="Try it yourself!"}, gui.Label{label=S"Try it yourself!"},
gui.HBox{ gui.HBox{
gui.VBox{ gui.VBox{
gui.Label{label="Element:"}, gui.Label{label=S"Element:"},
gui.Dropdown{ gui.Dropdown{
name = "element", name = "element",
items = elements, items = elements,
@ -195,12 +198,18 @@ local my_gui = flow.make_gui(function(player, ctx)
}, },
gui.HBox{ gui.HBox{
gui.VBox{ gui.VBox{
gui.Checkbox{name = "expand", label = "Expand"}, gui.Checkbox{name = "expand", label = S"Expand"},
gui.Checkbox{name = "box2", label = "Second box"}, gui.Checkbox{name = "box2", label = S"Second box"},
}, },
gui.VBox{ gui.VBox{
gui.Checkbox{name = "vbox", label = "Use vbox instead of hbox"}, gui.Checkbox{
gui.Checkbox{name = "expand_box2", label = "Expand second box"}, name = "vbox",
label = S"Use vbox instead of hbox"
},
gui.Checkbox{
name = "expand_box2",
label = S"Expand second box"
},
}, },
}, },
try_it_yourself_box, try_it_yourself_box,

View File

@ -19,6 +19,7 @@
local DEBUG_MODE = false local DEBUG_MODE = false
flow = {} flow = {}
local S = minetest.get_translator("flow")
local Form = {} local Form = {}
@ -1074,7 +1075,7 @@ function gui.PaginatedVBox(def)
end, end,
}, },
gui.Label { gui.Label {
label = "Page " .. current_page .. " of " .. #pages, label = S("Page @1 of @2", current_page, #pages),
align_h = "centre", align_h = "centre",
expand = true, expand = true,
}, },
@ -1206,7 +1207,7 @@ if minetest.is_singleplayer() then
local example_form local example_form
minetest.register_chatcommand("flow-example", { minetest.register_chatcommand("flow-example", {
privs = {server = true}, privs = {server = true},
help = "Shows an example formspec", help = S"Shows an example form",
func = function(name) func = function(name)
-- Only load example.lua when it's needed -- Only load example.lua when it's needed
if not example_form then if not example_form then

30
locale/template.txt Normal file
View File

@ -0,0 +1,30 @@
# textdomain: flow
### init.lua ###
Page @1 of @2=
Shows an example form=
### example.lua ###
Hello world!=
This is an example form.=
# checkbox labels and states
Uncheck me!=
Check me!=
Toggle checkbox=
Toggled!=
# expansion
A demonstration of expansion:=
By default, objects do not expand@nin the same direction as the hbox/vbox:=
Items are expanded in the opposite@ndirection, however:=
To automatically expand an object, add@n`expand = true` to its definition.=
Multiple expanded items will share the@nremaining space evenly.=
# Demo
Try it yourself!=
Element:=
Expand=
Second box=
Use vbox instead of hbox=
Expand second box=

View File

@ -25,6 +25,11 @@ minetest.is_singleplayer = dummy
minetest.get_player_information = dummy minetest.get_player_information = dummy
minetest.show_formspec = dummy minetest.show_formspec = dummy
function minetest.get_translator(modname)
assert(modname == "flow")
return function(str) return str end
end
-- Stub minetest player api -- Stub minetest player api
local function stub_player(name) local function stub_player(name)
assert(type(name) == "string") assert(type(name) == "string")