From 951981c79a5ca99b689fc5d5d32a99716da9795c Mon Sep 17 00:00:00 2001 From: Peppy Date: Tue, 21 Apr 2020 20:00:41 +0200 Subject: [PATCH] Add i18n whith get_translator --- init.lua | 6 ++++++ shop.lua | 43 +++++++++++++++++++++---------------------- shop_functions.lua | 28 +++++++++++++++------------- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/init.lua b/init.lua index df1c299..a9a9a5a 100644 --- a/init.lua +++ b/init.lua @@ -6,6 +6,12 @@ local modpath = minetest.get_modpath("exchange_shop") local has_currency = minetest.get_modpath("currency") local has_bitchange = minetest.get_modpath("bitchange") +-- Internationalisaton +exchange_shop.S = minetest.get_translator("exchange_shop") +exchange_shop.FS = function(...) + return minetest.formspec_escape(exchange_shop.S(...)) +end + -- Currency migrate options exchange_shop.migrate = { use_lbm = false, diff --git a/shop.lua b/shop.lua index 2117d89..c626989 100644 --- a/shop.lua +++ b/shop.lua @@ -5,7 +5,8 @@ This code is based on the idea of Dan Duncombe's exchange shop https://web.archive.org/web/20160403113102/https://forum.minetest.net/viewtopic.php?id=7002 ]] - +local S = exchange_shop.S +local FS = exchange_shop.FS local shop_positions = {} local function get_exchange_shop_formspec(mode, pos, meta) @@ -22,10 +23,10 @@ local function get_exchange_shop_formspec(mode, pos, meta) -- customer local formspec = ( (overflow and "size[8,9]" or "size[8,8]").. - "label[1,0.4;You give:]".. + "label[1,0.4;" .. FS("You give:") .. "]".. "list["..name..";cust_ow;1,1;2,2;]".. - "button[3,2.4;2,1;exchange;Exchange]".. - "label[5,0.4;You get:]".. + "button[3,2.4;2,1;exchange;" .. FS("Exchange") .. "]".. + "label[5,0.4;" .. FS("You get:") .. "]".. "list["..name..";cust_og;5,1;2,2;]" ) -- Insert fallback slots @@ -51,17 +52,17 @@ local function get_exchange_shop_formspec(mode, pos, meta) -- owner local formspec = ( "size[10,10]".. - "label[0,0.1;Title:]".. + "label[0,0.1;" .. FS("Title:") .. "]".. "field[1.2,0.5;3,0.5;title;;"..title.."]".. "field_close_on_enter[title;false]".. - "button[3.9,0.2;1,0.5;set_title;Set]".. + "button[3.9,0.2;1,0.5;set_title;" .. FS("Set") .. "]".. "container[0,2]".. - "label[0,-0.6;You need:]".. + "label[0,-0.6;" .. FS("You need") .. FS(":") .. "]".. "list["..name..";cust_ow;0,0;2,2;]".. - "label[2.5,-0.6;You give:]".. + "label[2.5,-0.6;" .. FS("You give") .. FS(":") .. "]".. "list["..name..";cust_og;2.5,0;2,2;]".. "container_end[]".. - "label[5,0.1;Current stock:]" + "label[5,0.1;" .. FS("Current stock:") .. "]" ) if overflow then @@ -74,17 +75,17 @@ local function get_exchange_shop_formspec(mode, pos, meta) if mode == "owner_custm" then formspec = (formspec.. - "button[7.5,0.2;2.5,0.5;view_stock;Income]".. + "button[7.5,0.2;2.5,0.5;view_stock;" .. FS("Income") .. "]".. "list["..name..";custm;5,1;5,4;]".. listring("custm")) else formspec = (formspec.. - "button[7.5,0.2;2.5,0.5;view_custm;Outgoing]".. + "button[7.5,0.2;2.5,0.5;view_custm;" .. FS("Outgoing") .. "]".. "list["..name..";stock;5,1;5,4;]".. listring("stock")) end return (formspec.. - "label[1,5.4;Use (E) + (Right click) for customer interface]".. + "label[1,5.4;" .. FS("Use (E) + (Right click) for customer interface") .. "]".. "list[current_player;main;1,6;8,4;]") end return "" @@ -118,10 +119,9 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) if title ~= fields.title then if fields.title ~= "" then meta:set_string("infotext", "'" .. fields.title - .. "' (owned by " .. shop_owner .. ")") + .. "' (" .. S("owned by @1", shop_owner) .. ")") else - meta:set_string("infotext", "Exchange shop (owned by " - .. shop_owner ..")") + meta:set_string("infotext", S("Exchange shop (owned by @1", shop_owner) .. ")" ) end meta:set_string("title", minetest.formspec_escape(fields.title)) end @@ -139,7 +139,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) -- Throw error message if err_msg then minetest.chat_send_player(player_name, minetest.colorize("#F33", - "Exchange shop: " .. err_msg)) + S("Exchange shop:") .. " " .. err_msg)) end if resend then minetest.show_formspec(player_name, "exchange_shop:shop_formspec", @@ -158,7 +158,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) end) minetest.register_node(exchange_shop.shopname, { - description = "Exchange Shop", + description = S"Exchange Shop", tiles = { "shop_top.png", "shop_top.png", "shop_side.png","shop_side.png", @@ -171,12 +171,11 @@ minetest.register_node(exchange_shop.shopname, { local meta = minetest.get_meta(pos) local owner = placer:get_player_name() meta:set_string("owner", owner) - meta:set_string("infotext", "Exchange shop (owned by " - .. owner .. ")") + meta:set_string("infotext", S("Exchange shop (owned by @1", owner) .. ")") end, on_construct = function(pos) local meta = minetest.get_meta(pos) - meta:set_string("infotext", "Exchange shop (constructing)") + meta:set_string("infotext", S("Exchange shop (constructing)")) meta:set_string("owner", "") local inv = meta:get_inventory() inv:set_size("stock", exchange_shop.storage_size) -- needed stock for exchanges @@ -195,7 +194,7 @@ minetest.register_node(exchange_shop.shopname, { return true end minetest.chat_send_player(player:get_player_name(), - "Cannot dig exchange shop: one or multiple stocks are in use.") + S("Cannot dig exchange shop: one or multiple stocks are in use.")) return false end, on_rightclick = function(pos, node, clicker, itemstack) @@ -221,7 +220,7 @@ minetest.register_node(exchange_shop.shopname, { allow_metadata_inventory_put = function(pos, listname, index, stack, player) if listname == "custm" then minetest.chat_send_player(player:get_player_name(), - "Exchange shop: Insert your trade goods into 'Outgoing'.") + S("Exchange shop: Insert your trade goods into 'Outgoing'.")) return 0 end local meta = minetest.get_meta(pos) diff --git a/shop_functions.lua b/shop_functions.lua index ab320de..9e69d71 100644 --- a/shop_functions.lua +++ b/shop_functions.lua @@ -1,3 +1,5 @@ +local S = exchange_shop.S + function exchange_shop.has_access(meta, player_name) local owner = meta:get_string("owner") if player_name == owner or owner == "" then @@ -67,8 +69,8 @@ end function exchange_shop.exchange_action(player_inv, shop_inv) if not shop_inv:is_empty("cust_ej") or not shop_inv:is_empty("custm_ej") then - return "One or multiple ejection fields are filled. ".. - "Please empty them or contact the shop owner." + return S("One or multiple ejection fields are filled.") .. " " .. + S("Please empty them or contact the shop owner.") end local owner_wants = shop_inv:get_list("cust_ow") local owner_gives = shop_inv:get_list("cust_og") @@ -81,8 +83,8 @@ function exchange_shop.exchange_action(player_inv, shop_inv) break end if i1 ~= i2 and name1 == item2:get_name() then - return "The field 'Owner needs' can not contain multiple ".. - "times the same items. Please contact the shop owner." + return S("The field '@1' can not contain multiple times the same items.", S("You need")) .. " " .. + S("Please contact the shop owner.") end end end @@ -95,8 +97,8 @@ function exchange_shop.exchange_action(player_inv, shop_inv) break end if i1 ~= i2 and name1 == item2:get_name() then - return "The field 'Owner gives' can not contain multiple ".. - "times the same items. Please contact the shop owner." + return S("The field '@1' can not contain multiple times the same items.", S("You give")) .. " " .. + S("Please contact the shop owner.") end end end @@ -104,8 +106,8 @@ function exchange_shop.exchange_action(player_inv, shop_inv) -- Check for space in the shop for i, item in pairs(owner_wants) do if not shop_inv:room_for_item("custm", item) then - return "The stock in this shop is full. ".. - "Please contact the shop owner." + return S("The stock in this shop is full.") .. " " .. + S("Please contact the shop owner.") end end @@ -114,21 +116,21 @@ function exchange_shop.exchange_action(player_inv, shop_inv) -- Check availability of the shop's items for i, item in pairs(owner_gives) do if not list_contains_item(shop_inv, "stock", item) then - return "This shop is sold out." + return S("This shop is sold out.") end end -- Check for space in the player's inventory for i, item in pairs(owner_gives) do if not player_inv:room_for_item("main", item) then - return "You do not have enough space in your inventory." + return S("You do not have enough space in your inventory.") end end -- Check availability of the player's items for i, item in pairs(owner_wants) do if not list_contains_item(player_inv, "main", item) then - return "You do not have the required items." + return S("You do not have the required items.") end end @@ -157,6 +159,6 @@ function exchange_shop.exchange_action(player_inv, shop_inv) end end if not fully_exchanged then - return "Warning! Stacks are overflowing somewhere!", true + return S("Warning! Stacks are overflowing somewhere!"), true end -end \ No newline at end of file +end