1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct Translation {
    messages: HashMap<String, String>,
}

impl Translation {
    pub fn new() -> Self {
        Self {
            messages: HashMap::new(),
        }
    }

    pub fn from_hashmap(messages: HashMap<String, String>) -> Self {
        Self { messages }
    }

    fn get_first(&self) -> &str {
        self.messages
            .values()
            .next()
            .map(|s| s.as_str())
            .unwrap_or("")
    }

    pub fn get(&self, key: &str) -> &str {
        self.messages
            .get(key)
            .map(|s| s.as_str())
            .unwrap_or(self.get_first())
    }
}

#[deprecated]
#[macro_export]
macro_rules! trad {
    ($($k:expr => $v:expr),* $(,)?) => {{
        Translation::from_hashmap(core::convert::From::from([$(($k.to_string(), $v.to_string()),)*]))
    }};
}

#[macro_export]
macro_rules! tr {
    ($($k:expr => $v:expr),* $(,)?) => {{
        Translation::from_hashmap(core::convert::From::from([$(($k.to_string(), $v.to_string()),)*]))
    }};
}
pub use tr;
pub use trad;