Allowed localisation content to be recursive

This commit is contained in:
Joshua Barretto 2023-04-11 23:18:34 +01:00
parent c69307557d
commit 3484e156d1
3 changed files with 32 additions and 12 deletions

View File

@ -196,7 +196,7 @@ pub enum Content {
/// deterministic (but pseudorandom) localised output /// deterministic (but pseudorandom) localised output
seed: u16, seed: u16,
/// i18n arguments /// i18n arguments
args: HashMap<String, String>, args: HashMap<String, Content>,
}, },
} }
@ -219,16 +219,16 @@ impl Content {
} }
} }
pub fn localized_with_args<'a, S: ToString>( pub fn localized_with_args<'a, A: Into<Content>>(
key: impl ToString, key: impl ToString,
args: impl IntoIterator<Item = (&'a str, S)>, args: impl IntoIterator<Item = (&'a str, A)>,
) -> Self { ) -> Self {
Self::Localized { Self::Localized {
key: key.to_string(), key: key.to_string(),
seed: rand::random(), seed: rand::random(),
args: args args: args
.into_iter() .into_iter()
.map(|(k, v)| (k.to_string(), v.to_string())) .map(|(k, v)| (k.to_string(), v.into()))
.collect(), .collect(),
} }
} }
@ -242,7 +242,7 @@ impl Content {
pub fn localize<F>(&self, i18n_variation: F) -> String pub fn localize<F>(&self, i18n_variation: F) -> String
where where
F: Fn(&str, u16, &HashMap<String, String>) -> String, F: Fn(&str, u16, &HashMap<String, Content>) -> String,
{ {
match self { match self {
Content::Plain(text) => text.to_string(), Content::Plain(text) => text.to_string(),

View File

@ -8,13 +8,32 @@ use hashbrown::HashMap;
use i18n::Localization; use i18n::Localization;
pub fn make_localizer( pub fn make_localizer(
localisation: &Localization, localization: &Localization,
) -> impl Fn(&str, u16, &HashMap<String, String>) -> String + Copy + '_ { ) -> impl Fn(&str, u16, &HashMap<String, Content>) -> String + Copy + '_ {
move |key: &str, seed: u16, args: &HashMap<String, String>| { fn get(
localisation localization: &Localization,
.get_variation_ctx(key, seed, &args.iter().collect()) key: &str,
seed: u16,
args: &HashMap<String, Content>,
) -> String {
localization
.get_variation_ctx(
key,
seed,
&args
.iter()
.map(|(k, v)| {
(
k,
v.localize(move |key, seed, args| get(localization, key, seed, args)),
)
})
.collect(),
)
.into_owned() .into_owned()
} }
move |key: &str, seed: u16, args: &HashMap<String, Content>| get(localization, key, seed, args)
} }
pub fn localize_chat_message( pub fn localize_chat_message(

View File

@ -384,7 +384,8 @@ impl SessionState {
.new_message(ChatType::CommandError.into_msg(match time { .new_message(ChatType::CommandError.into_msg(match time {
0 => Content::localized("hud-chat-goodbye"), 0 => Content::localized("hud-chat-goodbye"),
_ => Content::localized_with_args("hud-chat-connection_lost", [( _ => Content::localized_with_args("hud-chat-connection_lost", [(
"time", time, "time",
time.to_string(),
)]), )]),
})); }));
}, },
@ -987,7 +988,7 @@ impl PlayState for SessionState {
self.hud.new_message(ChatType::Meta.into_msg( self.hud.new_message(ChatType::Meta.into_msg(
Content::localized_with_args( Content::localized_with_args(
"hud-trade-invite_sent", "hud-trade-invite_sent",
[("playername", &name)], [("playername", name.as_str())],
), ),
)); ));
client.send_invite(uid, InviteKind::Trade) client.send_invite(uid, InviteKind::Trade)