diff options
author | metamuffin <metamuffin@disroot.org> | 2025-06-06 18:48:29 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-06-06 18:48:32 +0200 |
commit | 4a1b46745368af34a16eda0b99a795189b9e03bf (patch) | |
tree | adcd42f6c84ce88ecb1b713c1e7b9ccb1956cf76 | |
parent | 90bc0f0a870bb071da3390cf299f999e0073d12f (diff) | |
download | hurrycurry-4a1b46745368af34a16eda0b99a795189b9e03bf.tar hurrycurry-4a1b46745368af34a16eda0b99a795189b9e03bf.tar.bz2 hurrycurry-4a1b46745368af34a16eda0b99a795189b9e03bf.tar.zst |
translate messages in server log
-rw-r--r-- | server/src/message.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/server/src/message.rs b/server/src/message.rs index 069c2b0b..5a15c472 100644 --- a/server/src/message.rs +++ b/server/src/message.rs @@ -18,7 +18,12 @@ use anyhow::{anyhow, Result}; use hurrycurry_protocol::Message; -use std::{collections::HashMap, fmt::Debug, ops::Index, sync::LazyLock}; +use std::{ + collections::HashMap, + fmt::{Debug, Display}, + ops::Index, + sync::LazyLock, +}; #[macro_export] macro_rules! trm { @@ -72,6 +77,24 @@ impl Debug for TrError { } } } +impl Display for TrError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TrError::Tr { id, params } => { + if params.is_empty() { + write!(f, "{}", tr(id)) + } else { + let mut s = tr(id).to_string(); + for (i, p) in params.iter().enumerate() { + s = s.replace(&format!("{{{i}}}"), &format!("{p:?}")); + } + write!(f, "{s}") + } + } + TrError::Plain(s) => write!(f, "{s}"), + } + } +} #[macro_export] macro_rules! tre { @@ -126,6 +149,6 @@ impl Strings { } static TR: LazyLock<Strings> = LazyLock::new(|| Strings::load().unwrap()); -pub fn tr<'a>(s: &'static str) -> &'a str { +pub fn tr(s: &'static str) -> &'static str { &TR[s] } |