232 changes: 125 additions & 107 deletions builtin/mainmenu.lua

Large diffs are not rendered by default.

36 changes: 34 additions & 2 deletions builtin/misc_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,24 @@ function string:split(sep)
return fields
end

--------------------------------------------------------------------------------
function file_exists(filename)
local f = io.open(filename, "r")
if f==nil then
return false
else
f:close()
return true
end
end

--------------------------------------------------------------------------------
function string:trim()
return (self:gsub("^%s*(.-)%s*$", "%1"))
end

assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")



--------------------------------------------------------------------------------
function math.hypot(x, y)
local t
Expand Down Expand Up @@ -209,6 +218,29 @@ if engine ~= nil then

return nil
end

function fgettext(text, ...)
text = engine.gettext(text)
local arg = {n=select('#', ...), ...}
if arg.n >= 1 then
-- Insert positional parameters ($1, $2, ...)
result = ''
pos = 1
while pos <= text:len() do
newpos = text:find('[$]', pos)
if newpos == nil then
result = result .. text:sub(pos)
pos = text:len() + 1
else
paramindex = tonumber(text:sub(newpos+1, newpos+1))
result = result .. text:sub(pos, newpos-1) .. tostring(arg[paramindex])
pos = newpos + 2
end
end
text = result
end
return engine.formspec_escape(text)
end
end
--------------------------------------------------------------------------------
-- core only fct
Expand Down
79 changes: 41 additions & 38 deletions builtin/modmgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ function modmgr.tab()
end

local retval =
"vertlabel[0,-0.25;MODS]" ..
"label[0.8,-0.25;Installed Mods:]" ..
"vertlabel[0,-0.25;".. fgettext("MODS") .. "]" ..
"label[0.8,-0.25;".. fgettext("Installed Mods:") .. "]" ..
"textlist[0.75,0.25;4.5,4.3;modlist;" ..
modmgr.render_modlist(modmgr.global_mods) ..
";" .. modmgr.selected_mod .. "]"

retval = retval ..
"button[1,4.85;2,0.5;btn_mod_mgr_install_local;Install]" ..
"button[3,4.85;2,0.5;btn_mod_mgr_download;Download]"
"button[1,4.85;2,0.5;btn_mod_mgr_install_local;".. fgettext("Install") .. "]" ..
"button[3,4.85;2,0.5;btn_mod_mgr_download;".. fgettext("Download") .. "]"

local selected_mod = nil

Expand All @@ -251,11 +251,13 @@ function modmgr.tab()

if selected_mod ~= nil then
if selected_mod.is_modpack then
retval = retval .. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;Rename]"
retval = retval
.. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;" ..
fgettext("Rename") .. "]"
else
--show dependencies
retval = retval ..
"label[6,1.9;Depends:]" ..
"label[6,1.9;".. fgettext("Depends:") .. "]" ..
"textlist[6,2.4;5.7,2;deplist;"

toadd = modmgr.get_dependencies(selected_mod.path)
Expand All @@ -265,7 +267,8 @@ function modmgr.tab()
--TODO read modinfo
end
--show delete button
retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;Delete]"
retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;"
.. fgettext("Delete") .. "]"
end
return retval
end
Expand All @@ -276,12 +279,14 @@ function modmgr.dialog_rename_modpack()
local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]

local retval =
"label[1.75,1;Rename Modpack:]"..
"label[1.75,1;".. fgettext("Rename Modpack:") .. "]"..
"field[4.5,1.4;6,0.5;te_modpack_name;;" ..
mod.name ..
"]" ..
"button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;Accept]" ..
"button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;Cancel]"
"button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;"..
fgettext("Accept") .. "]" ..
"button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;"..
fgettext("Cancel") .. "]"

return retval
end
Expand Down Expand Up @@ -369,31 +374,32 @@ function modmgr.dialog_configure_world()

local retval =
"size[11,6.5]" ..
"label[1.5,-0.25;World: " .. worldspec.name .. "]"
"label[0.5,-0.25;" .. fgettext("World:") .. "]" ..
"label[1.75,-0.25;" .. worldspec.name .. "]"

if modmgr.hide_gamemods then
retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;Hide Game;true]"
retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
else
retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;Hide Game;false]"
retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
end

if modmgr.hide_modpackcontents then
retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;Hide mp content;true]"
retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
else
retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;Hide mp content;false]"
retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
end

if mod == nil then
mod = {name=""}
end
retval = retval ..
"label[0,0.45;Mod:]" ..
"label[0,0.45;" .. fgettext("Mod:") .. "]" ..
"label[0.75,0.45;" .. mod.name .. "]" ..
"label[0,1;Depends:]" ..
"label[0,1;" .. fgettext("Depends:") .. "]" ..
"textlist[0,1.5;5,4.25;world_config_depends;" ..
modmgr.get_dependencies(mod.path) .. ";0]" ..
"button[9.25,6.35;2,0.5;btn_config_world_save;Save]" ..
"button[7.4,6.35;2,0.5;btn_config_world_cancel;Cancel]"
"button[9.25,6.35;2,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
"button[7.4,6.35;2,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"

if mod ~= nil and mod.name ~= "" then
if mod.is_modpack then
Expand All @@ -409,22 +415,21 @@ function modmgr.dialog_configure_world()
end

if all_enabled == false then
retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_enable;Enable MP]"
retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
else
retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_disable;Disable MP]"
retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
end
else
if mod.enabled then
retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;enabled;true]"
retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
else
retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;enabled;false]"
retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
end
end

end

retval = retval ..
"button[8.5,-0.125;2.5,0.5;btn_all_mods;Enable all]" ..
"button[8.5,-0.125;2.5,0.5;btn_all_mods;" .. fgettext("Enable all") .. "]" ..
"textlist[5.5,0.5;5.5,5.75;world_config_modlist;"

retval = retval .. modmgr.render_modlist(modmgr.modlist)
Expand Down Expand Up @@ -540,7 +545,7 @@ function modmgr.handle_modmgr_buttons(fields)
end

if fields["btn_mod_mgr_install_local"] ~= nil then
engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
engine.show_file_open_dialog("mod_mgt_open_dlg",fgettext("Select Mod File:"))
end

if fields["btn_mod_mgr_download"] ~= nil then
Expand Down Expand Up @@ -579,8 +584,8 @@ function modmgr.installmod(modfilename,basename)
local modpath = modmgr.extract(modfile)

if modpath == nil then
gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
"\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
gamedata.errormessage = fgettext("Install Mod: file: \"$1\"", modfile.name) ..
fgettext("\nInstall Mod: unsupported filetype \"$1\"", modfile.type)
return
end

Expand All @@ -601,11 +606,10 @@ function modmgr.installmod(modfilename,basename)
if clean_path ~= nil then
local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
if not engine.copy_dir(basefolder.path,targetpath) then
gamedata.errormessage = "Failed to install " .. basename .. " to " .. targetpath
gamedata.errormessage = fgettext("Failed to install $1 to $2", basename, targetpath)
end
else
gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack "
.. modfilename
gamedata.errormessage = fgettext("Install Mod: unable to find suitable foldername for modpack $1", modfilename)
end
end

Expand All @@ -625,8 +629,7 @@ function modmgr.installmod(modfilename,basename)
local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
engine.copy_dir(basefolder.path,targetpath)
else
gamedata.errormessage = "Install Mod: unable to find real modname for: "
.. modfilename
gamedata.errormessage = fgettext("Install Mod: unable to find real modname for: $1", modfilename)
end
end

Expand Down Expand Up @@ -824,11 +827,11 @@ function modmgr.handle_delete_mod_buttons(fields)
mod.path ~= "" and
mod.path ~= engine.get_modpath() then
if not engine.delete_dir(mod.path) then
gamedata.errormessage ="Modmgr: failed to delete >" .. mod.path .. "<"
gamedata.errormessage = fgettext("Modmgr: failed to delete \"$1\"", mod.path)
end
modmgr.refresh_globals()
else
gamedata.errormessage ="Modmgr: invalid modpath >" .. mod.path .. "<"
gamedata.errormessage = fgettext("Modmgr: invalid modpath \"$1\"", mod.path)
end
end

Expand All @@ -845,9 +848,9 @@ function modmgr.dialog_delete_mod()
local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]

local retval =
"field[1.75,1;10,3;;Are you sure you want to delete ".. mod.name .. "?;]"..
"button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
"button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
"field[1.75,1;10,3;;" .. fgettext("Are you sure you want to delete \"$1\"?", mod.name) .. ";]"..
"button[4,4.2;1,0.5;dlg_delete_mod_confirm;" .. fgettext("Yes") .. "]" ..
"button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;" .. fgettext("No of course not!") .. "]"

return retval
end
Expand Down
10 changes: 5 additions & 5 deletions builtin/modstore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ end
--------------------------------------------------------------------------------
function modstore.getmodlist(list)
local retval = ""
retval = retval .. "label[10,-0.4;Page " .. (list.page +1) ..
" of " .. list.pagecount .. "]"
retval = retval .. "label[10,-0.4;" .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"

retval = retval .. "button[11.6,-0.1;0.5,0.5;btn_modstore_page_up;^]"
retval = retval .. "box[11.6,0.35;0.28,8.6;000000]"
Expand Down Expand Up @@ -240,17 +239,18 @@ function modstore.getmodlist(list)
engine.formspec_escape(details.description) .. ";]"
--rating
local ratingy = screenshot_ypos + 0.6
retval = retval .."label[10.1," .. ratingy .. ";Rating: " .. details.rating .."]"
retval = retval .."label[10.1," .. ratingy .. ";" ..
fgettext("Rating") .. ": " .. details.rating .."]"

--install button
local buttony = screenshot_ypos + 1.2
local buttonnumber = (i - (list.page * modstore.modsperpage))
retval = retval .."button[9.6," .. buttony .. ";2,0.5;btn_install_mod_" .. buttonnumber .. ";"

if modmgr.mod_exists(details.basename) then
retval = retval .. "re-Install]"
retval = retval .. fgettext("re-Install") .."]"
else
retval = retval .. "Install]"
retval = retval .. fgettext("Install") .."]"
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,7 @@ minetest.pos_to_string({x=X,y=Y,z=Z}) -> "(X,Y,Z)"
^ Convert position to a printable string
minetest.string_to_pos(string) -> position
^ Same but in reverse
minetest.formspec_escape(string) -> string
^ escapes characters like [, ], and \ that can not be used in formspecs
^ escapes characters [ ] \ , ; that can not be used in formspecs

minetest namespace reference
-----------------------------
Expand Down
7 changes: 7 additions & 0 deletions doc/menu_lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ engine.file_open_dialog(formname,caption)
^ returns nil or selected file/folder

Helpers:
engine.formspec_escape(string) -> string
^ escapes characters [ ] \ , ; that can not be used in formspecs
engine.gettext(string) -> string
^ look up the translation of a string in the gettext message catalog
fgettext(string, ...) -> string
^ call engine.gettext(string), replace "$1"..."$9" with the given
^ extra arguments, call engine.formspec_escape and return the result
dump(obj, dumped={})
^ Return object serialized as a string
string:split(separator)
Expand Down
1 change: 0 additions & 1 deletion src/guiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
m_menu->lockSize(true,v2u32(800,600));
m_menu->setFormSource(m_formspecgui);
m_menu->setTextDest(m_buttonhandler);
m_menu->useGettext(true);

// Initialize scripting

Expand Down
51 changes: 1 addition & 50 deletions src/guiFormSpecMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/numeric.h"
#include "filesys.h"
#include "gettime.h"

#include "gettext.h"


#define MY_CHECKPOS(a,b) \
if (v_pos.size() != 2) { \
errorstream<< "Invalid pos for element " << a << "specified: \"" \
Expand Down Expand Up @@ -88,7 +86,6 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
m_listbox_doubleclick(false),
m_tooltip_element(NULL),
m_allowclose(true),
m_use_gettext(false),
m_lock(false)
{
current_keys_pending.key_down = false;
Expand Down Expand Up @@ -379,9 +376,6 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element) {

std::wstring wlabel = narrow_to_wide(label.c_str());

if (m_use_gettext)
wlabel = wstrgettext(label);

FieldSpec spec = FieldSpec(
narrow_to_wide(name.c_str()),
L"",
Expand Down Expand Up @@ -499,9 +493,6 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,std::stri

std::wstring wlabel = narrow_to_wide(label.c_str());

if (m_use_gettext)
wlabel = wstrgettext(label);

FieldSpec spec = FieldSpec(
narrow_to_wide(name.c_str()),
wlabel,
Expand Down Expand Up @@ -609,7 +600,6 @@ void GUIFormSpecMenu::parseTextList(parserData* data,std::string element) {
std::wstring toadd =
narrow_to_wide(unescape_string(items[i]).c_str() + 7);


e->addItem(toadd.c_str());

irr::video::SColor toset;
Expand Down Expand Up @@ -733,13 +723,6 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) {

std::wstring wlabel = narrow_to_wide(label.c_str());

if (m_use_gettext) {
if (label.length() > 1)
wlabel = wstrgettext(label);
else
wlabel = L"";
}

FieldSpec spec = FieldSpec(
narrow_to_wide(name.c_str()),
wlabel,
Expand Down Expand Up @@ -812,13 +795,6 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,std::vector<std::string>

std::wstring wlabel = narrow_to_wide(label.c_str());

if (m_use_gettext) {
if (label.length() > 1)
wlabel = wstrgettext(label);
else
wlabel = L"";
}

FieldSpec spec = FieldSpec(
narrow_to_wide(name.c_str()),
wlabel,
Expand Down Expand Up @@ -902,13 +878,6 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,std::vector<std::string>& p

std::wstring wlabel = narrow_to_wide(label.c_str());

if (m_use_gettext) {
if (label.length() > 1)
wlabel = wstrgettext(label);
else
wlabel = L"";
}

FieldSpec spec = FieldSpec(
narrow_to_wide(name.c_str()),
wlabel,
Expand Down Expand Up @@ -989,9 +958,6 @@ void GUIFormSpecMenu::parseLabel(parserData* data,std::string element) {

std::wstring wlabel = narrow_to_wide(text.c_str());

if (m_use_gettext)
wlabel = wstrgettext(text);

FieldSpec spec = FieldSpec(
L"",
wlabel,
Expand Down Expand Up @@ -1026,12 +992,6 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data,std::string element) {
text = unescape_string(text);
std::string label = "";

if (m_use_gettext) {
const char* toset = gettext(text.c_str());

text = std::string(toset);
}

for (unsigned int i=0; i < text.length(); i++) {
label += text.c_str()[i];
label += "\n";
Expand Down Expand Up @@ -1098,9 +1058,6 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std:

std::wstring wlabel = narrow_to_wide(label.c_str());

if (m_use_gettext)
wlabel = wstrgettext(label);

FieldSpec spec = FieldSpec(
narrow_to_wide(name.c_str()),
wlabel,
Expand Down Expand Up @@ -1194,15 +1151,9 @@ void GUIFormSpecMenu::parseTabHeader(parserData* data,std::string element) {
for (unsigned int i=0; i< buttons.size(); i++) {
wchar_t* wbutton = 0;

if (m_use_gettext)
wbutton = wgettext(buttons[i].c_str());
else
wbutton = (wchar_t*) narrow_to_wide(buttons[i].c_str()).c_str();
wbutton = (wchar_t*) narrow_to_wide(buttons[i].c_str()).c_str();

e->addTab(wbutton,-1);

if (m_use_gettext)
delete[] wbutton;
}

if ((tab_index >= 0) &&
Expand Down
5 changes: 0 additions & 5 deletions src/guiFormSpecMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ class GUIFormSpecMenu : public GUIModalMenu
m_allowclose = value;
}

void useGettext(bool value) {
m_use_gettext = true;
}

void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0)) {
m_lock = lock;
m_lockscreensize = basescreensize;
Expand Down Expand Up @@ -282,7 +278,6 @@ class GUIFormSpecMenu : public GUIModalMenu
gui::IGUIStaticText *m_tooltip_element;

bool m_allowclose;
bool m_use_gettext;
bool m_lock;
v2u32 m_lockscreensize;
private:
Expand Down
11 changes: 11 additions & 0 deletions src/script/lua_api/l_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,16 @@ int ModApiMainMenu::l_download_file(lua_State *L)
return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_gettext(lua_State *L)
{
const char* str = luaL_checkstring(L, 1);
str = gettext(str);
lua_pushstring(L, str);

return 1;
}

/******************************************************************************/
void ModApiMainMenu::Initialize(lua_State *L, int top)
{
Expand Down Expand Up @@ -1013,4 +1023,5 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_modstore_list);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(gettext);
}
2 changes: 2 additions & 0 deletions src/script/lua_api/l_mainmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class ModApiMainMenu : public ModApiBase {

static int l_sound_stop(lua_State *L);

static int l_gettext(lua_State *L);

//gui

static int l_show_keys_menu(lua_State *L);
Expand Down
2 changes: 1 addition & 1 deletion util/updatepo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ cd ..
# directory at the top level. You a recent enough xgettext that supports
# --package-name
potfile=po/minetest.pot
xgettext --package-name=minetest -kN_ -kwgettext -F -n -o $potfile src/*.cpp src/*.h
xgettext --package-name=minetest -kN_ -kwgettext -kfgettext -F -n -o $potfile src/*.cpp src/*.h builtin/*.lua

# Now iterate on all languages and create the po file if missing, or update it
# if it exists already
Expand Down