Skip to content

Commit

Permalink
Add initial_language_from_server_function_to_cookie to `leptos_flue…
Browse files Browse the repository at this point in the history
…nt!` (#183)
  • Loading branch information
mondeja authored Jun 29, 2024
1 parent d208b06 commit 5739296
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 159 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
language from the browser language to a server function.
- `initial_language_from_url_param_to_server_function` to set the initial
language from URL parameter to a server function.
- `initial_language_from_server_function_to_cookie` to set the initial language
from a server function to a cookie.

## 2024-06-27 - [0.1.6]

Expand Down
12 changes: 12 additions & 0 deletions book/src/leptos_fluent.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,18 @@ pub async fn set_language_server_function(
}
```

### `initial_language_from_server_function_to_cookie`

Get the initial language from a [server function] and set it to a
[cookie].

```rust
leptos_fluent! {{
// ...
initial_language_from_server_function_to_cookie: true,
}}
```

[`fluent_templates::static_loader!`]: https://docs.rs/fluent-templates/latest/fluent_templates/macro.static_loader.html
[`once_cell:sync::Lazy`]: https://docs.rs/once_cell/latest/once_cell/sync/struct.Lazy.html
[`<html lang="...">` attribute]: https://developer.mozilla.org/es/docs/Web/HTML/Global_attributes/lang
Expand Down
1 change: 1 addition & 0 deletions book/src/strategies.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ are available:
| :------------------------------------- | :--------------------------------------------------- |
| [URL parameter] to [server function]\* | `initial_language_from_url_param_to_server_function` |
| [Cookie] to [server function]\* | `initial_language_from_cookie_to_server_function` |
| [Server function] to [cookie]\* | `initial_language_from_server_function_to_cookie` |

<sub style="position: relative; left: 110px"><sup>\*Unreleased</sup></sub>

Expand Down
3 changes: 2 additions & 1 deletion end2end/tests/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
- `initial_language_from_localstorage_to_server_function`
- `initial_language_from_cookie_to_server_function`
- `initial_language_from_navigator_to_server_function`
- `initial_language_from_url_param_to_server_function_quote`
- `initial_language_from_url_param_to_server_function`
- `initial_language_from_server_function_to_cookie`
113 changes: 73 additions & 40 deletions leptos-fluent-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ pub fn leptos_fluent(
set_language_to_cookie_exprpath,
initial_language_from_server_function,
initial_language_from_server_function_exprpath,
initial_language_from_server_function_to_cookie_bool,
initial_language_from_server_function_to_cookie_expr,
initial_language_from_server_function_to_cookie_exprpath,
set_language_to_server_function,
set_language_to_server_function_exprpath,
#[cfg(feature = "system")]
Expand Down Expand Up @@ -216,6 +219,34 @@ pub fn leptos_fluent(
::leptos_fluent::i18n().language.get()
};

let cookie_attrs = match cookie_attrs_str {
Some(ref lit) => match cookie_attrs_exprpath {
Some(path) => quote! { #path{#lit} },
None => quote! { #lit },
},
None => match cookie_attrs_expr {
Some(ref expr) => match cookie_attrs_exprpath {
Some(path) => quote! { #path{#expr} },
None => quote! { #expr },
},
None => quote! { "" },
},
};

let cookie_name = match cookie_name_str {
Some(ref lit) => match cookie_name_exprpath {
Some(path) => quote! { #path{#lit} },
None => quote! { #lit },
},
None => match cookie_name_expr {
Some(ref expr) => match cookie_name_exprpath {
Some(path) => quote! { #path{#expr} },
None => quote! { #expr },
},
None => quote! { "lf-lang" },
},
};

#[cfg(feature = "system")]
let data_file_key = match data_file_key_str {
Some(ref lit) => match data_file_key_exprpath {
Expand Down Expand Up @@ -447,6 +478,36 @@ pub fn leptos_fluent(
}

let initial_language_from_server_function_quote = {
let set_to_cookie_quote = {
let effect_quote = quote! {
::leptos_fluent::cookie::set(
#cookie_name,
&l.id.to_string(),
&#cookie_attrs,
);
};

let quote = match initial_language_from_server_function_to_cookie_bool {
Some(ref lit) => match lit.value {
true => effect_quote,
false => quote! {},
},
None => match initial_language_from_server_function_to_cookie_expr {
Some(ref expr) => quote! {
if #expr {
#effect_quote
}
},
None => quote! {},
},
};

match initial_language_from_server_function_to_cookie_exprpath {
Some(ref path) => quote! { #path{#quote} },
None => quote,
}
};

let effect_quote = quote! {
spawn_local(async move {
let lang_result = #initial_language_from_server_function().await;
Expand All @@ -456,6 +517,10 @@ pub fn leptos_fluent(
&l,
&LANGUAGES
);
if let Some(l) = lang {
#set_to_cookie_quote;
}

}
}
});
Expand Down Expand Up @@ -644,40 +709,6 @@ pub fn leptos_fluent(
}
};

#[cfg(any(
not(feature = "ssr"),
all(feature = "ssr", feature = "actix"),
all(feature = "ssr", feature = "axum")
))]
let cookie_name = match cookie_name_str {
Some(ref lit) => match cookie_name_exprpath {
Some(path) => quote! { #path{#lit} },
None => quote! { #lit },
},
None => match cookie_name_expr {
Some(ref expr) => match cookie_name_exprpath {
Some(path) => quote! { #path{#expr} },
None => quote! { #expr },
},
None => quote! { "lf-lang" },
},
};

#[cfg(not(feature = "ssr"))]
let cookie_attrs = match cookie_attrs_str {
Some(ref lit) => match cookie_attrs_exprpath {
Some(path) => quote! { #path{#lit} },
None => quote! { #lit },
},
None => match cookie_attrs_expr {
Some(ref expr) => match cookie_attrs_exprpath {
Some(path) => quote! { #path{#expr} },
None => quote! { #expr },
},
None => quote! { "" },
},
};

let initial_language_from_url_param_quote = {
#[cfg(feature = "hydrate")]
let hydrate_rerender_quote = quote! {
Expand Down Expand Up @@ -883,9 +914,7 @@ pub fn leptos_fluent(

match initial_language_from_url_param_exprpath {
Some(ref path) => {
quote! {
#path{#quote}
}
quote! { #path{#quote} }
}
None => quote,
}
Expand Down Expand Up @@ -1685,6 +1714,11 @@ pub fn leptos_fluent(
Some(_) => quote! { true },
None => quote! { false },
};
let initial_language_from_server_function_to_cookie_quote =
bool_param(
initial_language_from_server_function_to_cookie_bool,
initial_language_from_server_function_to_cookie_expr,
);
let set_language_to_server_function_quote =
match set_language_to_server_function {
Some(_) => quote! { true },
Expand Down Expand Up @@ -1761,6 +1795,7 @@ pub fn leptos_fluent(
initial_language_from_cookie_to_server_function: #initial_language_from_cookie_to_server_function_quote,
set_language_to_cookie: #set_language_to_cookie_quote,
initial_language_from_server_function: #initial_language_from_server_function_quote,
initial_language_from_server_function_to_cookie: #initial_language_from_server_function_to_cookie_quote,
set_language_to_server_function: #set_language_to_server_function_quote,
provide_meta_context: true,
#system_quote
Expand All @@ -1769,9 +1804,7 @@ pub fn leptos_fluent(
};

match provide_meta_context_exprpath {
Some(ref path) => quote! {
#path{#quote};
},
Some(ref path) => quote! { #path{#quote}; },
None => quote,
}
}
Expand Down
Loading

0 comments on commit 5739296

Please sign in to comment.