translate MOD and YOU messages

This commit is contained in:
Marcel Märtens 2022-09-12 14:07:11 +02:00
parent cc3fcfce8c
commit ba260e8419
6 changed files with 43 additions and 22 deletions

View File

@ -1,4 +1,6 @@
hud-chat-all = Alle
hud-chat-you = Du
hud-chat-mod = Mod
hud-chat-chat_tab_hover_tooltip = Rechtsklick für Einstellungen
hud-outcome-burning = An Verbrennung gestorben
hud-outcome-curse = An Verfluchung gestorben

View File

@ -1,4 +1,6 @@
hud-chat-all = All
hud-chat-you = You
hud-chat-mod = Mod
hud-chat-chat_tab_hover_tooltip = Right click for settings
hud-outcome-burning = died of: burning
hud-outcome-curse = died of: curse

View File

@ -49,6 +49,8 @@ termcolor = { version = "1.1", optional = true }
common-frontend = { package = "veloren-common-frontend", path = "../common/frontend", optional = true }
[dev-dependencies]
voxygen-chat-i18n = { package = "veloren-voxygen-chat-i18n", path = "../voxygen/chat-i18n" }
voxygen-i18n = { package = "veloren-voxygen-i18n", path = "../voxygen/i18n" }
[[example]]
name = "chat-cli"

View File

@ -29,7 +29,7 @@ fn main() {
// Initialize logging.
common_frontend::init_stdout(None);
info!("locading localisation");
info!("loading localisation");
let localisation = voxygen_i18n::LocalizationHandle::load_expect("en");
@ -128,4 +128,4 @@ fn main() {
player_printed = true;
}
}
}
}

View File

@ -2643,8 +2643,6 @@ impl Client {
/// Get important information from client that is necessary for message
/// localisation
///
/// Note: it uses the suffix `name` e.g. in `attacker_name` if Context is Raw, otherwise it returns just `attacker`
pub fn lockup_msg_context(
&self,
msg: &comp::ChatMsg,
@ -2672,7 +2670,7 @@ impl Client {
};
match chat_type {
comp::ChatType::Online(uid) | comp::ChatType::Offline(uid) => {
result.insert("player", alias_of_uid(uid));
result.insert("name", alias_of_uid(uid));
},
comp::ChatType::CommandError => (),
comp::ChatType::CommandInfo => (),
@ -2685,7 +2683,7 @@ impl Client {
result.insert("attacker", alias_of_uid(attacker_uid));
},
KillSource::NonPlayer(attacker_name, _) => {
result.insert("attacker_name", ChatTypeContext::Raw(attacker_name.clone()));
result.insert("attacker", ChatTypeContext::Raw(attacker_name.clone()));
},
KillSource::Environment(environment) => {
result.insert("environment", ChatTypeContext::Raw(environment.clone()));
@ -2827,6 +2825,7 @@ mod tests {
/// CONTACT @Core Developer BEFORE MERGING CHANGES TO THIS TEST
fn constant_api_test() {
use common::clock::Clock;
use voxygen_chat_i18n::internationalisate_chat_message;
const SPT: f64 = 1.0 / 60.0;
@ -2846,6 +2845,7 @@ mod tests {
password,
|suggestion: &str| suggestion == auth_server,
));
let localisation = voxygen_i18n::LocalizationHandle::load_expect("en");
let _ = veloren_client.map(|mut client| {
//clock
@ -2864,7 +2864,13 @@ mod tests {
match event {
Event::Chat(msg) => {
let msg: comp::ChatMsg = msg;
let _s: String = client.format_message(&msg, true);
let _s: String = internationalisate_chat_message(
msg,
|msg| client.lockup_msg_context(msg),
&localisation.read(),
true,
)
.message;
},
Event::Disconnect => {},
Event::DisconnectionNotification(_) => {

View File

@ -9,15 +9,15 @@ use std::collections::HashMap;
pub fn internationalisate_chat_message(
mut msg: ChatMsg,
lookup_fn: impl Fn(&ChatMsg) -> HashMap<&'static str, ChatTypeContext>,
localized_strings: &Localization,
localisation: &Localization,
show_char_name: bool,
) -> ChatMsg {
let info = lookup_fn(&msg);
if let Some(template_key) = get_chat_template_key(&msg.chat_type) {
msg.message = localized_strings
msg.message = localisation
.get_msg_ctx(template_key, &i18n::fluent_args! {
"attacker" => "{attacker}",
"attacker" => "{attacker_name}",
"name" => "{player}",
"name" => "{name}",
"died_of_buff" => "{died_of_buff}",
"victim" => "{victim}",
"environment" => "{environment}",
@ -29,20 +29,14 @@ pub fn internationalisate_chat_message(
KillSource::Player(_, KillType::Buff(buffkind))
| KillSource::NonExistent(KillType::Buff(buffkind))
| KillSource::NonPlayer(_, KillType::Buff(buffkind)) => {
msg.message = insert_killing_buff(*buffkind, localized_strings, &msg.message);
msg.message = insert_killing_buff(*buffkind, localisation, &msg.message);
},
_ => {},
}
}
}
let info = lookup_fn(&msg);
let gen_alias = |you, info: PlayerInfo| {
let mod_str = if info.is_moderator { "MOD - " } else { "" };
let you_str = if you { "You" } else { &info.player_alias };
format!("{}{}", mod_str, you_str)
};
let message_format = |you, info: PlayerInfo, message: &str, group: Option<&String>| {
let alias = gen_alias(you, info.clone());
let alias = insert_alias(you, info.clone(), localisation);
let name = if show_char_name {
info.character.map(|c| c.name)
} else {
@ -70,7 +64,7 @@ pub fn internationalisate_chat_message(
}
for (name, datum) in info.into_iter() {
let replacement = match datum {
ChatTypeContext::PlayerAlias { you, info } => gen_alias(you, info),
ChatTypeContext::PlayerAlias { you, info } => insert_alias(you, info, localisation),
ChatTypeContext::Raw(text) => text,
};
msg.message = msg.message.replace(&format!("{{{}}}", name), &replacement);
@ -105,7 +99,7 @@ fn get_chat_template_key(chat_type: &ChatType<String>) -> Option<&str> {
})
}
fn insert_killing_buff(buff: BuffKind, localized_strings: &Localization, template: &str) -> String {
fn insert_killing_buff(buff: BuffKind, localisation: &Localization, template: &str) -> String {
let buff_outcome = match buff {
BuffKind::Burning => "hud-outcome-burning",
BuffKind::Bleeding => "hud-outcome-bleeding",
@ -132,5 +126,20 @@ fn insert_killing_buff(buff: BuffKind, localized_strings: &Localization, templat
},
};
template.replace("{died_of_buff}", &localized_strings.get_msg(buff_outcome))
template.replace("{died_of_buff}", &localisation.get_msg(buff_outcome))
}
fn insert_alias(you: bool, info: PlayerInfo, localisation: &Localization) -> String {
const YOU: &str = "hud-chat-you";
const MOD: &str = "hud-chat-mod";
match (info.is_moderator, you) {
(false, false) => info.player_alias,
(false, true) => localisation.get_msg(YOU).to_string(),
(true, false) => format!("{} - {}", &localisation.get_msg(MOD), info.player_alias),
(true, true) => format!(
"{} - {}",
&localisation.get_msg(MOD),
&localisation.get_msg(YOU)
),
}
}