Skip to content

Commit

Permalink
Merge pull request #154 from Baptistemontan/update_t_string
Browse files Browse the repository at this point in the history
update t_string
  • Loading branch information
Baptistemontan authored Oct 29, 2024
2 parents 2d57dfa + b98d2ce commit ea8e755
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
9 changes: 7 additions & 2 deletions docs/book/src/usage/06_td_string_macro.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# The `td_string!` Macro

The `td_string!` macro is to use interpolations outside the context of rendering views, it let you give a different kind of values and return a `Cow<'static, str>`.
The `td_string!` macro is to use interpolations outside the context of rendering views, it let you give a different kind of values and return a `&'static str` or a `String` depending on the value of the key.
If the value is a plain string or a boolean it returns a `&'static str`, if it's an interpolations or a number it returns a `String`.

This requires the `interpolate_display` feature to be enabled.
This requires the `interpolate_display` feature to be enabled to work with interpolations.

It enable you to do this:

Expand Down Expand Up @@ -87,3 +88,7 @@ assert_eq!(format!("before {t} after"), "before You clicked 10 times after");
let t_str = t.to_string(); // can call `to_string` as the value impl `Display`
assert_eq!(t_str, "You clicked 10 times");
```

# `t_string`, `t_display`, `tu_string` and `tu_display`

They also exist, `td_string` was used here for easier demonstration. Remember that `t_string` access a signal reactively.
41 changes: 29 additions & 12 deletions leptos_i18n/src/macro_helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use std::{borrow::Cow, fmt::Display, future::Future, marker::PhantomData};
use std::{fmt::Display, future::Future, marker::PhantomData};

pub mod formatting;
mod interpol_args;
Expand All @@ -13,29 +13,33 @@ pub use scope::*;

#[doc(hidden)]
pub trait Literal: Sized + Display + IntoView + Copy {
fn into_str(self) -> Cow<'static, str>;
type AsStr;
fn into_str(self) -> Self::AsStr;
}

impl Literal for &'static str {
fn into_str(self) -> Cow<'static, str> {
Cow::Borrowed(self)
type AsStr = Self;
fn into_str(self) -> Self::AsStr {
self
}
}

impl Literal for bool {
fn into_str(self) -> Cow<'static, str> {
type AsStr = &'static str;
fn into_str(self) -> Self::AsStr {
match self {
true => Cow::Borrowed("true"),
false => Cow::Borrowed("false"),
true => "true",
false => "false",
}
}
}

macro_rules! impl_build_lit_nums {
($t:ty) => {
impl Literal for $t {
fn into_str(self) -> Cow<'static, str> {
Cow::Owned(self.to_string())
type AsStr = String;
fn into_str(self) -> Self::AsStr {
self.to_string()
}
}
};
Expand All @@ -47,6 +51,19 @@ macro_rules! impl_build_lit_nums {

impl_build_lit_nums!(u64, i64, f64);

#[doc(hidden)]
#[diagnostic::on_unimplemented(
message = "Interpolated values can't be used inside t_string/t_display without the \"interpolate_display\" feature enabled."
)]
pub trait InterpolationStringBuilder
where
Self: Sized,
{
fn check(self) -> Self {
self
}
}

#[doc(hidden)]
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand All @@ -73,7 +90,7 @@ impl<T: Literal> LitWrapper<T> {
self.0
}

pub fn build_string(self) -> Cow<'static, str> {
pub fn build_string(self) -> T::AsStr {
Literal::into_str(self.0)
}

Expand Down Expand Up @@ -107,7 +124,7 @@ impl<T: Literal, F: Future<Output = LitWrapper<T>>> LitWrapperFut<F> {
self.0.await.into_view()
}

pub async fn build_string(self) -> Cow<'static, str> {
pub async fn build_string(self) -> T::AsStr {
self.0.await.build_string()
}

Expand Down Expand Up @@ -137,7 +154,7 @@ impl<T: Literal> LitWrapperFut<LitWrapper<T>> {
self.0.into_view()
}

pub async fn build_string(self) -> Cow<'static, str> {
pub async fn build_string(self) -> T::AsStr {
self.0.build_string()
}

Expand Down
24 changes: 16 additions & 8 deletions leptos_i18n_macro/src/load_locales/interpolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,8 @@ impl Interpolation {
}

#[inline]
pub async fn build_string(self) -> std::borrow::Cow<'static, str> {
let display_struct = self.build_display().await;
std::borrow::Cow::Owned(display_struct.to_string())
pub async fn build_string(self) -> String {
self.build_display().await.to_string()
}
}
} else if cfg!(all(feature = "dynamic_load", feature = "ssr")) {
Expand All @@ -372,9 +371,8 @@ impl Interpolation {
}

#[inline]
pub async fn build_string(self) -> std::borrow::Cow<'static, str> {
let display_struct = self.build_display();
std::borrow::Cow::Owned(display_struct.to_string())
pub async fn build_string(self) -> String {
self.build_display().to_string()
}
}
} else {
Expand All @@ -386,8 +384,8 @@ impl Interpolation {
}

#[inline]
pub fn build_string(self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.build_display().to_string())
pub fn build_string(self) -> String {
self.build_display().to_string()
}
}
};
Expand Down Expand Up @@ -454,6 +452,14 @@ impl Interpolation {

let into_views = fields.iter().filter_map(Field::as_into_view_generic);

let string_builder_trait_impl = if cfg!(feature = "interpolate_display") {
quote! {
impl l_i18n_crate::__private::InterpolationStringBuilder for #dummy_ident {}
}
} else {
quote!()
};

quote! {
impl #dummy_ident {
pub const fn new(#locale_field: #enum_ident) -> Self {
Expand All @@ -468,6 +474,8 @@ impl Interpolation {

#display_builder_fn
}

#string_builder_trait_impl
}
}

Expand Down
8 changes: 8 additions & 0 deletions leptos_i18n_macro/src/t_macro/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub fn t_macro_inner(
let (#(#keys,)*) = (#(#values,)*);
};

let get_key = if matches!(output_type, OutputType::String | OutputType::Display) {
quote! {
leptos_i18n::__private::InterpolationStringBuilder::check(#get_key)
}
} else {
get_key
};

let inner = quote! {
{
let _builder = #get_key.#builder_fn();
Expand Down

0 comments on commit ea8e755

Please sign in to comment.