diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-28 00:48:52 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-28 00:48:52 +0200 |
commit | 80d28b764c95891551e28c395783f5ff9d065743 (patch) | |
tree | f25898b1c939a939c63236ca4e8e843e81069947 /ui/src/locale.rs | |
parent | 335ba978dbaf203f3603a815147fd75dbf205723 (diff) | |
download | jellything-80d28b764c95891551e28c395783f5ff9d065743.tar jellything-80d28b764c95891551e28c395783f5ff9d065743.tar.bz2 jellything-80d28b764c95891551e28c395783f5ff9d065743.tar.zst |
start with splitting server
Diffstat (limited to 'ui/src/locale.rs')
-rw-r--r-- | ui/src/locale.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/ui/src/locale.rs b/ui/src/locale.rs new file mode 100644 index 0000000..0179c66 --- /dev/null +++ b/ui/src/locale.rs @@ -0,0 +1,82 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2025 metamuffin <metamuffin.org> +*/ +use markup::{Render, RenderAttributeValue}; +use std::{borrow::Cow, collections::HashMap, sync::LazyLock}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Language { + English, + German, +} + +static LANG_TABLES: LazyLock<HashMap<Language, HashMap<&'static str, &'static str>>> = + LazyLock::new(|| { + let mut k = HashMap::new(); + for (lang, source) in [ + (Language::English, include_str!("../../locale/en.ini")), + (Language::German, include_str!("../../locale/de.ini")), + ] { + let tr_map = source + .lines() + .filter_map(|line| { + let (key, value) = line.split_once("=")?; + Some((key.trim(), value.trim())) + }) + .collect::<HashMap<&'static str, &'static str>>(); + k.insert(lang, tr_map); + } + k + }); + +pub fn tr(lang: Language, key: &str) -> Cow<'static, str> { + let tr_map = LANG_TABLES.get(&lang).unwrap(); + match tr_map.get(key) { + Some(value) => Cow::Borrowed(value), + None => Cow::Owned(format!("TR[{key}]")), + } +} + +pub struct TrString<'a>(Cow<'a, str>); +impl Render for TrString<'_> { + fn render(&self, writer: &mut impl std::fmt::Write) -> std::fmt::Result { + self.0.render(writer) + } +} +impl RenderAttributeValue for TrString<'_> { + fn is_none(&self) -> bool { + false + } + fn is_true(&self) -> bool { + false + } + fn is_false(&self) -> bool { + false + } +} + +pub fn escape(str: &str) -> String { + let mut o = String::with_capacity(str.len()); + let mut last = 0; + for (index, byte) in str.bytes().enumerate() { + if let Some(esc) = match byte { + b'<' => Some("<"), + b'>' => Some(">"), + b'&' => Some("&"), + b'"' => Some("""), + _ => None, + } { + o += &str[last..index]; + o += esc; + last = index + 1; + } + } + o += &str[last..]; + o +} + +pub fn trs<'a>(lang: &Language, key: &str) -> TrString<'a> { + TrString(tr(*lang, key)) +} |