Skip to content

Commit

Permalink
add localize function for Lua and localize exist strings
Browse files Browse the repository at this point in the history
  • Loading branch information
HarpyWar committed Jun 28, 2014
1 parent 69545f2 commit f40ac4b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lua/command/redirect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function command_redirect(account, text)
local dest = api.account_get_by_name(args[1])

if next(dest) == nil or dest.online == "false" then
api.message_send_text(account.name, message_type_error, account.name, "User '" ..args[1].. "' is offline")
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "User \"{}\" is offline", args[1]))
return 1
end

Expand Down
3 changes: 3 additions & 0 deletions lua/extend/message.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ function message_send_all(text)
end
end

function localize(username, arg1, arg2, arg3, arg4, arg5)
return api.localize(username, arg1, arg2, arg3, arg4, arg5)
end
2 changes: 1 addition & 1 deletion lua/handle_command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function handle_command(account, text)

-- check if command group is in account.commandgroups
if math_and(account.commandgroups, cg) == 0 then
api.message_send_text(account.name, message_type_error, account.name, "This command is reserved for admins.")
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command is reserved for admins."))
return 1
end

Expand Down
22 changes: 11 additions & 11 deletions lua/quiz/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ end
function q_command_start(account, filename)

if not account_is_operator_or_admin(account.name) then
api.message_send_text(account.name, message_type_error, account.name, "You must be at least a Channel Operator to use this command.")
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "You must be at least a Channel Operator to use this command."))
return 1
end

local channel = api.channel_get_by_id(account.channel_id)
if not channel then
api.message_send_text(account.name, message_type_error, account.name, "This command can only be used inside a channel.")
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command can only be used inside a channel."))
return 1
end

if config.quiz_channel then
api.message_send_text(account.name, message_type_error, account.name, 'Quiz has already ran in channel "'..config.quiz_channel..'". Use /qstop to force finish.')
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Quiz has already ran in channel \"{}\". Use /quiz stop to force finish.", config.quiz_channel))
return 1
end

-- check if file exists
if not filename or not file_exists(q_directory() .. "/questions/" .. filename .. ".txt") then
api.message_send_text(account.name, message_type_error, account.name, "Available Quiz dictionaries: ")
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Available Quiz dictionaries: "))
api.message_send_text(account.name, message_type_error, account.name, " " .. config.quiz_filelist)
return 1
end
Expand All @@ -67,12 +67,12 @@ end
function q_command_stop(account)

if not account_is_operator_or_admin(account.name) then
api.message_send_text(account.name, message_type_error, account.name, "You must be at least a Channel Operator to use this command.")
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "You must be at least a Channel Operator to use this command."))
return 1
end

if not config.quiz_channel then
api.message_send_text(account.name, message_type_error, account.name, 'Quiz is not running.')
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Quiz is not running."))
return 1
end

Expand All @@ -89,14 +89,14 @@ function q_command_toplist(account)
return 0
end

local output = "Top " .. config.quiz_users_in_top .. " Quiz records:"
local output = localize(account.name, "Top {} Quiz records:", config.quiz_users_in_top)
api.message_send_text(account.name, message_type_info, account.name, output)

-- display TOP of total records
for i,t in pairs(q_records_total) do
if (i > config.quiz_users_in_top) then break end

local output = string.format(" %d. %s [%d points]", i, t.username, t.points)
local output = string.format(" %d. %s [%d %s]", i, t.username, t.points, localize(account.name, "points"))
api.message_send_text(account.name, message_type_info, account.name, output)
end

Expand All @@ -116,17 +116,17 @@ function q_command_stats(account, username)
-- find user in records
for i,t in pairs(q_records_total) do
if string.upper(t.username) == string.upper(username) then
api.message_send_text(account.name, message_type_info, account.name, t.username.. "'s Quiz record:")
api.message_send_text(account.name, message_type_info, account.name, localize(account.name, "{}'s Quiz record:", t.username))

local output = string.format(" %d. %s [%d points]", i, t.username, t.points)
local output = string.format(" %d. %s [%d %s]", i, t.username, t.points, localize(account.name, "points"))
api.message_send_text(account.name, message_type_info, account.name, output)

found = true
end
end

if not found then
api.message_send_text(account.name, message_type_info, account.name, username .. " has never played Quiz.")
api.message_send_text(account.name, message_type_info, account.name, localize(account.name, "{} has never played Quiz.", username))
end

return 1
Expand Down
37 changes: 36 additions & 1 deletion src/bnetd/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "attrlayer.h"
#include "icons.h"
#include "helpfile.h"
#include "i18n.h"

#include "luawrapper.h"
#include "luaobjects.h"
Expand Down Expand Up @@ -129,6 +130,7 @@ namespace pvpgn
st.at(1, loglevel);
st.at(2, function);
st.at(3, text);
eventlog(t_eventlog_level(loglevel), function, text);
}
catch (const std::exception& e)
{
Expand All @@ -138,7 +140,6 @@ namespace pvpgn
{
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
}
eventlog(t_eventlog_level(loglevel), function, text);

return 0;
}
Expand Down Expand Up @@ -766,7 +767,41 @@ namespace pvpgn
return 0;
}

/* Localize text */
extern int __localize(lua_State* L)
{
const char *username, *text;
const char *arg1, *arg2, *arg3, *arg4, *arg5;
try
{
lua::stack st(L);
// get args
st.at(1, username);
st.at(2, text);
st.at(3, arg1);
st.at(4, arg2);
st.at(5, arg3);
st.at(6, arg4);
st.at(7, arg5);

if (t_account * account = accountlist_find_account(username))
{
if (t_connection * c = account_get_conn(account))
{
st.push(localize(c, text, arg1, arg2, arg3, arg4, arg5));
}
}
}
catch (const std::exception& e)
{
eventlog(eventlog_level_error, __FUNCTION__, e.what());
}
catch (...)
{
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
}
return 1;
}
}
}
#endif
2 changes: 2 additions & 0 deletions src/bnetd/luafunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace pvpgn
extern int __icon_get_rank(lua_State* L);
extern int __describe_command(lua_State* L);
extern int __messagebox_show(lua_State* L);

extern int __localize(lua_State* L);
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/bnetd/luainterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ namespace pvpgn
{ "describe_command", __describe_command },
{ "messagebox_show", __messagebox_show },

{ "localize", __localize },

{ 0, 0 }
};
vm.reg("api", api);
Expand Down

0 comments on commit f40ac4b

Please sign in to comment.