Created
July 12, 2024 20:26
-
-
Save JadedBlueEyes/216deddac28d9b7c5aa58012e8dc2f00 to your computer and use it in GitHub Desktop.
Expanded JadedBlueEyes/messageformat basic example
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, __var> { | |
formatter: &'a for<'x, 'y> fn( | |
&'x mut dyn mf1::Formatable<'y>, | |
&str, | |
) -> Result<(), Box<dyn std::error::Error>>, | |
arg_var: __var, | |
} | |
impl<'a> interpolated<'a, EmptyValue> { | |
pub const fn new( | |
formatter: &'a for<'x, 'y> fn( | |
&'x mut dyn mf1::Formatable<'y>, | |
&str, | |
) -> Result<(), Box<dyn std::error::Error>>, | |
) -> Self { | |
Self { | |
formatter, | |
arg_var: EmptyValue, | |
} | |
} | |
} | |
impl<'a> interpolated<'a, EmptyValue> { | |
pub fn arg_var(self, arg_var: &str) -> interpolated<'a, &str> { | |
let interpolated { formatter, .. } = self; | |
interpolated { formatter, arg_var } | |
} | |
} | |
impl<'a> mf1::BuildStr for interpolated<'a, &str> { | |
#[inline] | |
fn build_string(self) -> std::borrow::Cow<'static, str> { | |
std::borrow::Cow::Owned(format!("{}", self)) | |
} | |
} | |
impl<'a> std::fmt::Display for interpolated<'a, &str> { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
match (self.formatter)(f, self.arg_var) { | |
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: builders::interpolated<'static, builders::EmptyValue>, | |
} | |
#[allow(non_upper_case_globals)] | |
const en: Mf1Keys = Mf1Keys { | |
message: "This is a message!", | |
message_2: "This is a second message!", | |
interpolated: builders::interpolated::new( | |
&(|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!("{}", t!(l, interpolated, var = la)); | |
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