veloren/voxygen/i18n-helpers/src/lib.rs

249 lines
11 KiB
Rust
Raw Normal View History

#![feature(let_chains)]
use common::comp::{
chat::{KillSource, KillType},
BuffKind, ChatMsg, ChatType, Content,
};
use common_net::msg::{ChatTypeContext, PlayerInfo};
use i18n::Localization;
pub fn localize_chat_message(
msg: ChatMsg,
lookup_fn: impl Fn(&ChatMsg) -> ChatTypeContext,
2023-04-11 22:51:07 +00:00
localization: &Localization,
show_char_name: bool,
) -> (ChatType<String>, String) {
2022-09-12 12:07:11 +00:00
let info = lookup_fn(&msg);
let name_format = |uid: &common::uid::Uid| match info.player_alias.get(uid).cloned() {
2023-04-11 22:51:07 +00:00
Some(pi) => insert_alias(info.you == *uid, pi, localization),
None => info
.entity_name
.get(uid)
.cloned()
.expect("client didn't proved enough info"),
};
let message_format = |from: &common::uid::Uid, content: &Content, group: Option<&String>| {
let alias = name_format(from);
let name = if let Some(pi) = info.player_alias.get(from).cloned() && show_char_name {
pi.character.map(|c| c.name)
} else {
None
};
2023-04-11 22:51:07 +00:00
let message = localization.get_content(content);
match (group, name) {
(Some(group), None) => format!("({group}) [{alias}]: {message}"),
(None, None) => format!("[{alias}]: {message}"),
(Some(group), Some(name)) => format!("({group}) [{alias}] {name}: {message}"),
(None, Some(name)) => format!("[{alias}] {name}: {message}"),
}
};
let new_msg = match &msg.chat_type {
2023-04-11 22:51:07 +00:00
ChatType::Online(uid) => localization
.get_msg_ctx("hud-chat-online_msg", &i18n::fluent_args! {
"name" => name_format(uid),
})
.into_owned(),
2023-04-11 22:51:07 +00:00
ChatType::Offline(uid) => localization
.get_msg_ctx("hud-chat-offline_msg", &i18n::fluent_args! {
"name" => name_format(uid
),
})
.into_owned(),
2023-04-11 22:51:07 +00:00
ChatType::CommandError => localization.get_content(msg.content()),
ChatType::CommandInfo => localization.get_content(msg.content()),
ChatType::FactionMeta(_) => localization.get_content(msg.content()),
ChatType::GroupMeta(_) => localization.get_content(msg.content()),
ChatType::Tell(from, to) => {
let from_alias = name_format(from);
let to_alias = name_format(to);
// TODO: internationalise
if *from == info.you {
2023-04-11 22:51:07 +00:00
format!(
"To [{to_alias}]: {}",
localization.get_content(msg.content())
)
} else {
2023-04-11 22:51:07 +00:00
format!(
"From [{from_alias}]: {}",
localization.get_content(msg.content())
)
}
},
ChatType::Say(uid) => message_format(uid, msg.content(), None),
ChatType::Group(uid, s) => message_format(uid, msg.content(), Some(s)),
ChatType::Faction(uid, s) => message_format(uid, msg.content(), Some(s)),
ChatType::Region(uid) => message_format(uid, msg.content(), None),
ChatType::World(uid) => message_format(uid, msg.content(), None),
// NPCs can't talk. Should be filtered by hud/mod.rs for voxygen and
// should be filtered by server (due to not having a Pos) for chat-cli
ChatType::Npc(uid) => message_format(uid, msg.content(), None),
ChatType::NpcSay(uid) => message_format(uid, msg.content(), None),
ChatType::NpcTell(from, to) => {
let from_alias = name_format(from);
let to_alias = name_format(to);
// TODO: internationalise
if *from == info.you {
2023-04-11 22:51:07 +00:00
format!(
"To [{to_alias}]: {}",
localization.get_content(msg.content())
)
} else {
2023-04-11 22:51:07 +00:00
format!(
"From [{from_alias}]: {}",
localization.get_content(msg.content())
)
}
},
2023-04-11 22:51:07 +00:00
ChatType::Meta => localization.get_content(msg.content()),
ChatType::Kill(kill_source, victim) => {
let i18n_buff = |buff| match buff {
BuffKind::Burning => "hud-outcome-burning",
BuffKind::Bleeding => "hud-outcome-bleeding",
BuffKind::Cursed => "hud-outcome-curse",
BuffKind::Crippled => "hud-outcome-crippled",
BuffKind::Frozen => "hud-outcome-frozen",
BuffKind::Regeneration
| BuffKind::Saturation
| BuffKind::Potion
| BuffKind::CampfireHeal
| BuffKind::EnergyRegen
| BuffKind::IncreaseMaxEnergy
| BuffKind::IncreaseMaxHealth
| BuffKind::Invulnerability
| BuffKind::ProtectingWard
| BuffKind::Frenzied
2022-03-05 20:51:41 +00:00
| BuffKind::Hastened
2022-12-19 18:26:03 +00:00
| BuffKind::Fortitude
2023-05-19 03:07:44 +00:00
| BuffKind::Reckless
| BuffKind::Flame
| BuffKind::Frigid
2023-07-09 20:03:09 +00:00
| BuffKind::Lifesteal
2023-05-15 01:38:11 +00:00
// | BuffKind::SalamanderAspect
2023-05-20 17:25:49 +00:00
| BuffKind::ImminentCritical
2023-05-21 00:21:23 +00:00
| BuffKind::Fury
2023-05-27 21:02:34 +00:00
| BuffKind::Sunderer
2023-07-03 00:50:25 +00:00
| BuffKind::Defiance
| BuffKind::Bloodfeast
| BuffKind::Berserk => {
tracing::error!("Player was killed by a positive buff!");
"hud-outcome-mysterious"
},
BuffKind::Wet
| BuffKind::Ensnared
| BuffKind::Poisoned
| BuffKind::Parried
2023-03-29 23:11:59 +00:00
| BuffKind::PotionSickness
| BuffKind::Polymorphed(_) => {
tracing::error!("Player was killed by a debuff that doesn't do damage!");
"hud-outcome-mysterious"
},
};
match kill_source {
// Buff deaths
KillSource::Player(attacker, KillType::Buff(buff_kind)) => {
let i18n_buff = i18n_buff(*buff_kind);
2023-04-11 22:51:07 +00:00
let buff = localization.get_msg(i18n_buff);
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx("hud-chat-died_of_pvp_buff_msg", &i18n::fluent_args! {
"victim" => name_format(victim),
"died_of_buff" => buff,
"attacker" => name_format(attacker),
})
},
KillSource::NonPlayer(attacker_name, KillType::Buff(buff_kind)) => {
let i18n_buff = i18n_buff(*buff_kind);
2023-04-11 22:51:07 +00:00
let buff = localization.get_msg(i18n_buff);
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx("hud-chat-died_of_npc_buff_msg", &i18n::fluent_args! {
"victim" => name_format(victim),
"died_of_buff" => buff,
"attacker" => attacker_name,
})
},
KillSource::NonExistent(KillType::Buff(buff_kind)) => {
let i18n_buff = i18n_buff(*buff_kind);
2023-04-11 22:51:07 +00:00
let buff = localization.get_msg(i18n_buff);
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx(
"hud-chat-died_of_buff_nonexistent_msg",
&i18n::fluent_args! {
"victim" => name_format(victim),
"died_of_buff" => buff,
},
)
},
// PvP deaths
KillSource::Player(attacker, kill_type) => {
let key = match kill_type {
KillType::Melee => "hud-chat-pvp_melee_kill_msg",
KillType::Projectile => "hud-chat-pvp_ranged_kill_msg",
KillType::Explosion => "hud-chat-pvp_explosion_kill_msg",
KillType::Energy => "hud-chat-pvp_energy_kill_msg",
KillType::Other => "hud-chat-pvp_other_kill_msg",
&KillType::Buff(_) => unreachable!("handled above"),
};
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx(key, &i18n::fluent_args! {
"victim" => name_format(victim),
"attacker" => name_format(attacker),
})
},
// PvE deaths
KillSource::NonPlayer(attacker_name, kill_type) => {
let key = match kill_type {
KillType::Melee => "hud-chat-npc_melee_kill_msg",
KillType::Projectile => "hud-chat-npc_ranged_kill_msg",
KillType::Explosion => "hud-chat-npc_explosion_kill_msg",
KillType::Energy => "hud-chat-npc_energy_kill_msg",
KillType::Other => "hud-chat-npc_other_kill_msg",
&KillType::Buff(_) => unreachable!("handled above"),
};
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx(key, &i18n::fluent_args! {
"victim" => name_format(victim),
"attacker" => attacker_name,
})
},
// Other deaths
KillSource::Environment(environment) => {
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx("hud-chat-environment_kill_msg", &i18n::fluent_args! {
"name" => name_format(victim),
"environment" => environment,
})
},
KillSource::FallDamage => {
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx("hud-chat-fall_kill_msg", &i18n::fluent_args! {
"name" => name_format(victim),
})
},
KillSource::Suicide => {
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx("hud-chat-suicide_msg", &i18n::fluent_args! {
"name" => name_format(victim),
})
},
KillSource::NonExistent(_) | KillSource::Other => {
2023-04-11 22:51:07 +00:00
localization.get_msg_ctx("hud-chat-default_death_msg", &i18n::fluent_args! {
"name" => name_format(victim),
})
},
}
.into_owned()
},
};
(msg.chat_type, new_msg)
2022-09-12 12:07:11 +00:00
}
2023-04-11 22:51:07 +00:00
fn insert_alias(you: bool, info: PlayerInfo, localization: &Localization) -> String {
2022-09-12 12:07:11 +00:00
const YOU: &str = "hud-chat-you";
2023-01-28 01:45:05 +00:00
// Leave space for a mod badge icon.
const MOD_SPACING: &str = " ";
2022-09-12 12:07:11 +00:00
match (info.is_moderator, you) {
(false, false) => info.player_alias,
2023-04-11 22:51:07 +00:00
(false, true) => localization.get_msg(YOU).to_string(),
2023-01-28 01:45:05 +00:00
(true, false) => format!("{}{}", MOD_SPACING, info.player_alias),
2023-04-11 22:51:07 +00:00
(true, true) => format!("{}{}", MOD_SPACING, &localization.get_msg(YOU),),
2022-09-12 12:07:11 +00:00
}
}