25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Don't do double utf-8 conversion on translated strings in str:format

This caused garbled text in some places in webadmin when using language
that used characters > 128.

Thanks to chengshq for noticing this and providing preliminary patch.
This commit is contained in:
Paweł Chmielowski 2020-11-09 12:20:23 +01:00
parent 9ed2ca5079
commit 81e872c110
7 changed files with 24 additions and 18 deletions

View File

@ -930,7 +930,7 @@ user_info(User, Server, Query, Lang) ->
end; end;
_ -> translate:translate(Lang, ?T("Online")) _ -> translate:translate(Lang, ?T("Online"))
end, end,
[?XC(<<"h1">>, (str:format(translate:translate(Lang, ?T("User ~ts")), [?XC(<<"h1">>, (str:translate_and_format(Lang, ?T("User ~ts"),
[us_to_list(US)])))] [us_to_list(US)])))]
++ ++
case Res of case Res of
@ -1087,7 +1087,7 @@ get_node(global, Node, [], Query, Lang) ->
Base = get_base_path(global, Node, 2), Base = get_base_path(global, Node, 2),
MenuItems2 = make_menu_items(global, Node, Base, Lang), MenuItems2 = make_menu_items(global, Node, Base, Lang),
[?XC(<<"h1">>, [?XC(<<"h1">>,
(str:format(translate:translate(Lang, ?T("Node ~p")), [Node])))] (str:translate_and_format(Lang, ?T("Node ~p"), [Node])))]
++ ++
case Res of case Res of
ok -> [?XREST(?T("Submitted"))]; ok -> [?XREST(?T("Submitted"))];
@ -1109,7 +1109,7 @@ get_node(global, Node, [], Query, Lang) ->
get_node(Host, Node, [], _Query, Lang) -> get_node(Host, Node, [], _Query, Lang) ->
Base = get_base_path(Host, Node, 4), Base = get_base_path(Host, Node, 4),
MenuItems2 = make_menu_items(Host, Node, Base, Lang), MenuItems2 = make_menu_items(Host, Node, Base, Lang),
[?XC(<<"h1">>, (str:format(translate:translate(Lang, ?T("Node ~p")), [Node]))), [?XC(<<"h1">>, (str:translate_and_format(Lang, ?T("Node ~p"), [Node]))),
?XE(<<"ul">>, MenuItems2)]; ?XE(<<"ul">>, MenuItems2)];
get_node(global, Node, [<<"db">>], Query, Lang) -> get_node(global, Node, [<<"db">>], Query, Lang) ->
case ejabberd_cluster:call(Node, mnesia, system_info, [tables]) of case ejabberd_cluster:call(Node, mnesia, system_info, [tables]) of
@ -1165,7 +1165,7 @@ get_node(global, Node, [<<"db">>], Query, Lang) ->
end, end,
STables), STables),
[?XC(<<"h1">>, [?XC(<<"h1">>,
(str:format(translate:translate(Lang, ?T("Database Tables at ~p")), (str:translate_and_format(Lang, ?T("Database Tables at ~p"),
[Node])) [Node]))
)] )]
++ ++
@ -1203,7 +1203,7 @@ get_node(global, Node, [<<"backup">>], Query, Lang) ->
[?XRES(<<(translate:translate(Lang, ?T("Error")))/binary, ": ", [?XRES(<<(translate:translate(Lang, ?T("Error")))/binary, ": ",
((str:format("~p", [Error])))/binary>>)] ((str:format("~p", [Error])))/binary>>)]
end, end,
[?XC(<<"h1">>, (str:format(translate:translate(Lang, ?T("Backup of ~p")), [Node])))] [?XC(<<"h1">>, (str:translate_and_format(Lang, ?T("Backup of ~p"), [Node])))]
++ ++
ResS ++ ResS ++
[?XCT(<<"p">>, [?XCT(<<"p">>,
@ -1357,7 +1357,7 @@ get_node(global, Node, [<<"stats">>], _Query, Lang) ->
TransactionsLogged = ejabberd_cluster:call(Node, mnesia, system_info, TransactionsLogged = ejabberd_cluster:call(Node, mnesia, system_info,
[transaction_log_writes]), [transaction_log_writes]),
[?XC(<<"h1">>, [?XC(<<"h1">>,
(str:format(translate:translate(Lang, ?T("Statistics of ~p")), [Node]))), (str:translate_and_format(Lang, ?T("Statistics of ~p"), [Node]))),
?XAE(<<"table">>, [], ?XAE(<<"table">>, [],
[?XE(<<"tbody">>, [?XE(<<"tbody">>,
[?XE(<<"tr">>, [?XE(<<"tr">>,
@ -1425,7 +1425,7 @@ get_node(global, Node, [<<"update">>], Query, Lang) ->
FmtLowLevelScript = (?XC(<<"pre">>, FmtLowLevelScript = (?XC(<<"pre">>,
(str:format("~p", [LowLevelScript])))), (str:format("~p", [LowLevelScript])))),
[?XC(<<"h1">>, [?XC(<<"h1">>,
(str:format(translate:translate(Lang, ?T("Update ~p")), [Node])))] (str:translate_and_format(Lang, ?T("Update ~p"), [Node])))]
++ ++
case Res of case Res of
ok -> [?XREST(?T("Submitted"))]; ok -> [?XREST(?T("Submitted"))];

View File

@ -663,7 +663,7 @@ get_outgoing_s2s(Host, Lang) ->
Host == FH orelse str:suffix(DotHost, FH)], Host == FH orelse str:suffix(DotHost, FH)],
lists:map( lists:map(
fun (T) -> fun (T) ->
Name = str:format(tr(Lang, ?T("To ~ts")),[T]), Name = str:translate_and_format(Lang, ?T("To ~ts"),[T]),
#disco_item{jid = jid:make(Host), #disco_item{jid = jid:make(Host),
node = <<"outgoing s2s/", T/binary>>, node = <<"outgoing s2s/", T/binary>>,
name = Name} name = Name}
@ -675,7 +675,7 @@ get_outgoing_s2s(Host, Lang, To) ->
lists:map( lists:map(
fun ({F, _T}) -> fun ({F, _T}) ->
Node = <<"outgoing s2s/", To/binary, "/", F/binary>>, Node = <<"outgoing s2s/", To/binary, "/", F/binary>>,
Name = str:format(tr(Lang, ?T("From ~ts")), [F]), Name = str:translate_and_format(Lang, ?T("From ~ts"), [F]),
#disco_item{jid = jid:make(Host), node = Node, name = Name} #disco_item{jid = jid:make(Host), node = Node, name = Name}
end, end,
lists:keysort( lists:keysort(

View File

@ -1444,7 +1444,8 @@ get_error_text(#stanza_error{text = Txt}) ->
make_reason(Packet, From, StateData, Reason1) -> make_reason(Packet, From, StateData, Reason1) ->
#user{nick = FromNick} = maps:get(jid:tolower(From), StateData#state.users), #user{nick = FromNick} = maps:get(jid:tolower(From), StateData#state.users),
Condition = get_error_condition(xmpp:get_error(Packet)), Condition = get_error_condition(xmpp:get_error(Packet)),
str:format(Reason1, [FromNick, Condition]). Reason2 = unicode:characters_to_list(Reason1),
str:format(Reason2, [FromNick, Condition]).
-spec expulse_participant(stanza(), jid(), state(), binary()) -> -spec expulse_participant(stanza(), jid(), state(), binary()) ->
state(). state().
@ -3493,8 +3494,8 @@ get_config(Lang, StateData, From) ->
DefaultRoomMaxUsers = get_default_room_maxusers(StateData), DefaultRoomMaxUsers = get_default_room_maxusers(StateData),
Config = StateData#state.config, Config = StateData#state.config,
MaxUsersRoom = get_max_users(StateData), MaxUsersRoom = get_max_users(StateData),
Title = str:format( Title = str:translate_and_format(
translate:translate(Lang, ?T("Configuration of room ~s")), Lang, ?T("Configuration of room ~s"),
[jid:encode(StateData#state.jid)]), [jid:encode(StateData#state.jid)]),
Fs = [{roomname, Config#config.title}, Fs = [{roomname, Config#config.title},
{roomdesc, Config#config.description}, {roomdesc, Config#config.description},

View File

@ -988,7 +988,7 @@ user_queue(User, Server, Query, Lang) ->
end, end,
Hdrs = get_messages_subset(User, Server, HdrsAll), Hdrs = get_messages_subset(User, Server, HdrsAll),
FMsgs = format_user_queue(Hdrs), FMsgs = format_user_queue(Hdrs),
PageTitle = str:format(translate:translate(Lang, ?T("~ts's Offline Messages Queue")), [us_to_list(US)]), PageTitle = str:translate_and_format(Lang, ?T("~ts's Offline Messages Queue"), [us_to_list(US)]),
(?H1GL(PageTitle, <<"modules/#mod-offline">>, <<"mod_offline">>)) (?H1GL(PageTitle, <<"modules/#mod-offline">>, <<"mod_offline">>))
++ [?XREST(?T("Submitted"))] ++ ++ [?XREST(?T("Submitted"))] ++
[?XAE(<<"form">>, [?XAE(<<"form">>,

View File

@ -222,8 +222,7 @@ process_iq(#iq{type = get, from = From, to = To, id = ID, lang = Lang} = IQ,
"with this server")), "with this server")),
URL = mod_register_opt:redirect_url(Server), URL = mod_register_opt:redirect_url(Server),
if (URL /= undefined) and not IsRegistered -> if (URL /= undefined) and not IsRegistered ->
Txt = translate:translate(Lang, ?T("To register, visit ~s")), Desc = str:translate_and_format(Lang, ?T("To register, visit ~s"), [URL]),
Desc = str:format(Txt, [URL]),
xmpp:make_iq_result( xmpp:make_iq_result(
IQ, #register{instructions = Desc, IQ, #register{instructions = Desc,
sub_els = [#oob_x{url = URL}]}); sub_els = [#oob_x{url = URL}]});

View File

@ -1053,7 +1053,7 @@ user_roster(User, Server, Query, Lang) ->
end, end,
SItems)))])] SItems)))])]
end, end,
PageTitle = str:format(translate:translate(Lang, ?T("Roster of ~ts")), [us_to_list(US)]), PageTitle = str:translate_and_format(Lang, ?T("Roster of ~ts"), [us_to_list(US)]),
(?H1GL(PageTitle, <<"modules/#mod-roster">>, <<"mod_roster">>)) (?H1GL(PageTitle, <<"modules/#mod-roster">>, <<"mod_roster">>))
++ ++
case Res of case Res of

View File

@ -64,10 +64,11 @@
to_float/1, to_float/1,
prefix/2, prefix/2,
suffix/2, suffix/2,
format/2, format/2,
to_integer/1, to_integer/1,
sha/1, sha/1,
to_hexlist/1]). to_hexlist/1,
translate_and_format/3]).
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -288,6 +289,11 @@ suffix(B1, B2) ->
format(Format, Args) -> format(Format, Args) ->
unicode:characters_to_binary(io_lib:format(Format, Args)). unicode:characters_to_binary(io_lib:format(Format, Args)).
-spec translate_and_format(binary(), binary(), list()) -> binary().
translate_and_format(Lang, Format, Args) ->
format(unicode:characters_to_list(translate:translate(Lang, Format)), Args).
-spec sha(iodata()) -> binary(). -spec sha(iodata()) -> binary().