Created
July 12, 2024 19:41
-
-
Save JadedBlueEyes/5c6f7b2686e07cf621978ce907f85d4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::str::FromStr; | |
use mf1::{load_locales, t_l_string as t}; | |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] | |
#[allow(non_camel_case_types)] | |
pub enum Locale { | |
en, | |
es, | |
} | |
impl Locale { | |
fn get_strings(self) -> &'static Mf1Keys { | |
match self { | |
Locale::en => &en, | |
Locale::es => &es, | |
} | |
} | |
fn as_str(self) -> &'static str { | |
match self { | |
Locale::en => "en", | |
Locale::es => "es", | |
} | |
} | |
} | |
impl std::str::FromStr for Locale { | |
type Err = (); | |
fn from_str(s: &str) -> Result<Self, Self::Err> { | |
match s.trim() { | |
"en" => Ok(Locale::en), | |
"es" => Ok(Locale::es), | |
_ => Err(()), | |
} | |
} | |
} | |
impl Default for Locale { | |
fn default() -> Self { | |
Locale::en | |
} | |
} | |
#[doc(hidden)] | |
pub mod builders { | |
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] | |
pub struct EmptyValue; | |
#[allow(non_camel_case_types, non_snake_case)] | |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] | |
pub struct interpolated<'a> { | |
arg_var: &'a str, | |
} | |
} | |
struct DynKey<'a, T>(&'a for<'x, 'y> fn( | |
&'x mut dyn mf1::Formatable<'y>, | |
&T, | |
) -> Result<(), Box<dyn std::error::Error>>, T); | |
impl<'a, T> mf1::BuildStr for DynKey<'a, T> { | |
#[inline] | |
fn build_string(self) -> std::borrow::Cow<'static, str> { | |
std::borrow::Cow::Owned(format!("{}", self)) | |
} | |
} | |
impl<'a, T> std::fmt::Display for DynKey<'a, T> { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
match (self.0)(f, &self.1) { | |
Ok(_) => Ok(()), | |
Err(e) => Err(*e.downcast().unwrap()), | |
} | |
} | |
} | |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] | |
#[allow(non_camel_case_types, non_snake_case)] | |
pub struct Mf1Keys { | |
pub message: &'static str, | |
pub message_2: &'static str, | |
pub interpolated: &'static for<'x, 'y> fn( | |
&'x mut dyn mf1::Formatable<'y>, | |
&str, | |
) -> Result<(), Box<dyn std::error::Error>>, | |
} | |
trait InterpolationArgs { | |
type interpolated<'a>; | |
} | |
impl InterpolationArgs for Mf1Keys { | |
type interpolated<'a> = builders::interpolated<'a>; | |
} | |
#[allow(non_upper_case_globals)] | |
const en: Mf1Keys = Mf1Keys { | |
message: "This is a message!", | |
message_2: "This is a second message!", | |
interpolated: | |
&(|fmt: &mut dyn mf1::Formatable, var: &str| -> Result<(), _> { | |
fmt.write_str("This has been interpolated with ")?; | |
fmt.write_str(var)?; | |
Ok(()) | |
} as _ | |
), | |
}; | |
#[allow(non_upper_case_globals)] | |
const es: Mf1Keys = Mf1Keys { | |
message: "¡Este es un mensaje!", | |
message_2: en.message_2, | |
interpolated: en.interpolated, | |
}; | |
fn main() { | |
let args: Vec<String> = std::env::args().collect(); | |
let l = args | |
.get(1) | |
.map(|l| Locale::from_str(l).unwrap()) | |
.unwrap_or_default(); | |
let la = l.as_str(); | |
dbg!(l.get_strings()); | |
println!("{}", { | |
let(var,) = (la,); | |
#[allow(unused)] | |
use mf1::BuildStr; | |
let _strings = l.get_strings(); | |
let _key = _strings.interpolated; | |
let a = Mf1Keys::interpo; | |
let _key = _key.arg_var(&var); | |
#[deny(deprecated)] | |
_key.build_string() | |
}); | |
println!("{}", t!(l, message)); | |
println!("{}", t!(l, message_2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment