From 4a1b46745368af34a16eda0b99a795189b9e03bf Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 6 Jun 2025 18:48:29 +0200 Subject: translate messages in server log --- server/src/message.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'server/src') 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 = LazyLock::new(|| Strings::load().unwrap()); -pub fn tr<'a>(s: &'static str) -> &'a str { +pub fn tr(s: &'static str) -> &'static str { &TR[s] } -- cgit v1.2.3-70-g09d2