aboutsummaryrefslogtreecommitdiff
path: root/base/src/locale.rs
blob: a1c5ef2788d1ffa0031b1f62e19266b31c85eeea (plain)
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
use std::{borrow::Cow, collections::HashMap};

#[derive(Debug, Clone, Copy)]
pub enum Language {
    English,
    German,
}

pub fn tr<'a>(lang: Language, key: &str, args: &[(&str, &str)]) -> Cow<'a, str> {
    let source_str = match lang {
        Language::English => include_str!("../../locale/en.ini"),
        Language::German => include_str!("../../locale/de.ini"),
    };
    let tr_map = source_str
        .lines()
        .filter_map(|line| {
            let (key, value) = line.split_once("=")?;
            Some((key.trim(), value.trim()))
        })
        .collect::<HashMap<&'static str, &'static str>>();

    match tr_map.get(key) {
        Some(value) => Cow::Borrowed(value),
        None => Cow::Owned(format!("TR[{key}]")),
    }
}