add localize function for Lua and localize exist strings

This commit is contained in:
HarpyWar 2014-06-28 21:33:09 +04:00
parent 69545f25bf
commit f40ac4b008
7 changed files with 56 additions and 14 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
@ -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
@ -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
@ -116,9 +116,9 @@ 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
@ -126,7 +126,7 @@ function q_command_stats(account, username)
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

View file

@ -48,6 +48,7 @@
#include "attrlayer.h"
#include "icons.h"
#include "helpfile.h"
#include "i18n.h"
#include "luawrapper.h"
#include "luaobjects.h"
@ -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)
{
@ -138,7 +140,6 @@ namespace pvpgn
{
eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
}
eventlog(t_eventlog_level(loglevel), function, text);
return 0;
}
@ -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

View file

@ -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);
}
}

View file

@ -147,6 +147,8 @@ namespace pvpgn
{ "describe_command", __describe_command },
{ "messagebox_show", __messagebox_show },
{ "localize", __localize },
{ 0, 0 }
};
vm.reg("api", api);