mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Allowed localisation content to be recursive
This commit is contained in:
parent
c69307557d
commit
3484e156d1
@ -196,7 +196,7 @@ pub enum Content {
|
||||
/// deterministic (but pseudorandom) localised output
|
||||
seed: u16,
|
||||
/// 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,
|
||||
args: impl IntoIterator<Item = (&'a str, S)>,
|
||||
args: impl IntoIterator<Item = (&'a str, A)>,
|
||||
) -> Self {
|
||||
Self::Localized {
|
||||
key: key.to_string(),
|
||||
seed: rand::random(),
|
||||
args: args
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
||||
.map(|(k, v)| (k.to_string(), v.into()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
@ -242,7 +242,7 @@ impl Content {
|
||||
|
||||
pub fn localize<F>(&self, i18n_variation: F) -> String
|
||||
where
|
||||
F: Fn(&str, u16, &HashMap<String, String>) -> String,
|
||||
F: Fn(&str, u16, &HashMap<String, Content>) -> String,
|
||||
{
|
||||
match self {
|
||||
Content::Plain(text) => text.to_string(),
|
||||
|
@ -8,13 +8,32 @@ use hashbrown::HashMap;
|
||||
use i18n::Localization;
|
||||
|
||||
pub fn make_localizer(
|
||||
localisation: &Localization,
|
||||
) -> impl Fn(&str, u16, &HashMap<String, String>) -> String + Copy + '_ {
|
||||
move |key: &str, seed: u16, args: &HashMap<String, String>| {
|
||||
localisation
|
||||
.get_variation_ctx(key, seed, &args.iter().collect())
|
||||
localization: &Localization,
|
||||
) -> impl Fn(&str, u16, &HashMap<String, Content>) -> String + Copy + '_ {
|
||||
fn get(
|
||||
localization: &Localization,
|
||||
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()
|
||||
}
|
||||
|
||||
move |key: &str, seed: u16, args: &HashMap<String, Content>| get(localization, key, seed, args)
|
||||
}
|
||||
|
||||
pub fn localize_chat_message(
|
||||
|
@ -384,7 +384,8 @@ impl SessionState {
|
||||
.new_message(ChatType::CommandError.into_msg(match time {
|
||||
0 => Content::localized("hud-chat-goodbye"),
|
||||
_ => 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(
|
||||
Content::localized_with_args(
|
||||
"hud-trade-invite_sent",
|
||||
[("playername", &name)],
|
||||
[("playername", name.as_str())],
|
||||
),
|
||||
));
|
||||
client.send_invite(uid, InviteKind::Trade)
|
||||
|
Loading…
Reference in New Issue
Block a user