Skip to content

Commit

Permalink
Add cookie_attrs parameter to leptos_fluent! macro (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Jun 16, 2024
1 parent 6b604ff commit 0006e0e
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 36 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CHANGELOG

## Unreleased - [0.0.37]
## 2024-06-16 - [0.0.37]

### Bug fixes

Expand All @@ -11,6 +11,11 @@

- Notify invalid Fluent message identifiers checking translations.

### New features

- Add `cookie_attrs` parameter to `leptos_fluent!` macro to set cookie
attributes.

## 2024-06-15 - [0.0.36]

### Bug fixes
Expand Down Expand Up @@ -209,7 +214,7 @@

- Added all ISO-639-1 and ISO-639-2 languages.

[0.0.37]: https://github.com/mondeja/leptos-fluent/compare/v0.0.36...master
[0.0.37]: https://github.com/mondeja/leptos-fluent/compare/v0.0.36...v0.0.37
[0.0.36]: https://github.com/mondeja/leptos-fluent/compare/v0.0.35...v0.0.36
[0.0.35]: https://github.com/mondeja/leptos-fluent/compare/v0.0.34...v0.0.35
[0.0.34]: https://github.com/mondeja/leptos-fluent/compare/v0.0.33...v0.0.34
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add the following to your `Cargo.toml` file:

```toml
[dependencies]
leptos-fluent = "0.0.36"
leptos-fluent = "0.0.37"
fluent-templates = "0.9"

[features]
Expand Down Expand Up @@ -117,6 +117,11 @@ fn App() -> impl IntoView {
// Get the initial language from `navigator.languages` if not
// found in the local storage. By default, it is `false`.
initial_language_from_navigator: true,
// Attributes to set for the language cookie. By default is `""`.
cookie_attrs: "SameSite=Strict; Secure; path=/; max-age=600",
// Update the language on cookie when using the method `I18n.set_language`.
// By default, it is `false`.
set_language_to_cookie: true,

// Server side options
// -------------------
Expand All @@ -131,9 +136,6 @@ fn App() -> impl IntoView {
cookie_name: "lang",
// Get the initial language from cookie. By default, it is `false`.
initial_language_from_cookie: true,
// Update the language on cookie when using the method `I18n.set_language`.
// By default, it is `false`.
set_language_to_cookie: true,
// URL parameter name to use discovering the initial language
// of the user. By default is `"lang"`.
url_param: "lang",
Expand Down
1 change: 1 addition & 0 deletions end2end/tests/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- `initial_language_from_navigator`
- `initial_language_from_accept_language_header`
- `set_language_to_cookie`
- `cookie_attrs`
4 changes: 2 additions & 2 deletions end2end/tests/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ async fn test_cookie() {
assert_eq!(element_text("p"), "Select a language:");
unmount!();

cookie::set(COOKIE_NAME, "es");
cookie::set(COOKIE_NAME, "es", "");
mount!(App);
assert!(es().checked());
assert_eq!(element_text("p"), "Selecciona un idioma:");
unmount!();

cookie::set(COOKIE_NAME, "en");
cookie::set(COOKIE_NAME, "en", "");
mount!(App);
assert!(en().checked());
assert_eq!(element_text("p"), "Select a language:");
Expand Down
4 changes: 4 additions & 0 deletions examples/csr-complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub fn App() -> impl IntoView {
locales: "./locales",
check_translations: "./src/**/*.rs",
sync_html_tag_lang: true,
cookie_name: "lang",
cookie_attrs: "SameSite=Strict; Secure; path=/; max-age=600",
set_language_to_cookie: true,
initial_language_from_cookie: true,
url_param: "lang",
initial_language_from_url_param: true,
initial_language_from_url_param_to_localstorage: true,
Expand Down
1 change: 1 addition & 0 deletions examples/ssr-hydrate-actix/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn App() -> impl IntoView {
locales: "./locales",
check_translations: "./src/**/*.rs",
cookie_name: "lang",
cookie_attrs: "SameSite=Strict; Secure; path=/; max-age=600",
initial_language_from_cookie: true,
set_language_to_cookie: true,
url_param: "lang",
Expand Down
1 change: 1 addition & 0 deletions examples/ssr-hydrate-axum/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn App() -> impl IntoView {
locales: "./locales",
check_translations: "./src/**/*.rs",
cookie_name: "lang",
cookie_attrs: "SameSite=Strict; Secure; path=/; max-age=600",
initial_language_from_cookie: true,
set_language_to_cookie: true,
url_param: "lang",
Expand Down
2 changes: 1 addition & 1 deletion leptos-fluent-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "leptos-fluent-macros"
description = "Macros for leptos-fluent"
edition.workspace = true
version = "0.0.36"
version = "0.0.37"
license = "MIT"
documentation.workspace = true
repository.workspace = true
Expand Down
15 changes: 15 additions & 0 deletions leptos-fluent-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ use quote::quote;
/// runtime. It will only take effect on server-side.
/// - **`cookie_name`** (_`"lf-lang"`_): The cookie name to manage language in a cookie. Can be a literal string or an
/// expression that will be evaluated at runtime. It will take effect on client-side and server side.
/// - **`cookie_attrs`** (_`""`_): The [attributes][cookie-attributes] to set in the cookie. Can be a literal string or an expression
/// that will be evaluated at runtime. For example, `"SameSite=Strict; Secure; path=/; max-age=600"`.
/// It will take effect on client-side.
/// - **`initial_language_from_cookie`** (_`false`_): Load the initial language of the user from a cookie.
/// Can be a literal boolean or an expression that will be evaluated at runtime. It will take effect on client-side
/// and server side.
Expand All @@ -159,6 +162,7 @@ use quote::quote;
/// [local storage]: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
/// [`navigator.languages`]: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/languages
/// [`leptos::create_effect`]: https://docs.rs/leptos/latest/leptos/fn.create_effect.html
/// [cookie-attributes]: https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#write_a_new_cookie
#[proc_macro]
pub fn leptos_fluent(
input: proc_macro::TokenStream,
Expand Down Expand Up @@ -190,6 +194,8 @@ pub fn leptos_fluent(
initial_language_from_accept_language_header_expr,
cookie_name_str,
cookie_name_expr,
cookie_attrs_str,
cookie_attrs_expr,
initial_language_from_cookie_bool,
initial_language_from_cookie_expr,
set_language_to_cookie_bool,
Expand Down Expand Up @@ -647,11 +653,20 @@ pub fn leptos_fluent(

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

let effect_quote = quote! {
::leptos::create_effect(move |_| {
::leptos_fluent::cookie::set(
#cookie_name,
&::leptos_fluent::expect_i18n().language.get().id.to_string(),
&#cookie_attrs,
);
});
};
Expand Down
15 changes: 15 additions & 0 deletions leptos-fluent-macros/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub(crate) struct I18nLoader {
Option<syn::Expr>,
pub(crate) cookie_name_str: Option<syn::LitStr>,
pub(crate) cookie_name_expr: Option<syn::Expr>,
pub(crate) cookie_attrs_str: Option<syn::LitStr>,
pub(crate) cookie_attrs_expr: Option<syn::Expr>,
pub(crate) initial_language_from_cookie_bool: Option<syn::LitBool>,
pub(crate) initial_language_from_cookie_expr: Option<syn::Expr>,
pub(crate) set_language_to_cookie_bool: Option<syn::LitBool>,
Expand Down Expand Up @@ -158,6 +160,8 @@ impl Parse for I18nLoader {
> = None;
let mut cookie_name_str: Option<syn::LitStr> = None;
let mut cookie_name_expr: Option<syn::Expr> = None;
let mut cookie_attrs_str: Option<syn::LitStr> = None;
let mut cookie_attrs_expr: Option<syn::Expr> = None;
let mut initial_language_from_cookie_bool: Option<syn::LitBool> = None;
let mut initial_language_from_cookie_expr: Option<syn::Expr> = None;
let mut set_language_to_cookie_bool: Option<syn::LitBool> = None;
Expand Down Expand Up @@ -284,6 +288,15 @@ impl Parse for I18nLoader {
) {
return Err(err);
}
} else if k == "cookie_attrs" {
if let Some(err) = parse_litstr_or_expr_param(
&fields,
&mut cookie_attrs_str,
&mut cookie_attrs_expr,
"cookie_attrs",
) {
return Err(err);
}
} else if k == "initial_language_from_cookie" {
if let Some(err) = parse_litbool_or_expr_param(
&fields,
Expand Down Expand Up @@ -493,6 +506,8 @@ impl Parse for I18nLoader {
initial_language_from_accept_language_header_expr,
cookie_name_str,
cookie_name_expr,
cookie_attrs_str,
cookie_attrs_expr,
initial_language_from_cookie_bool,
initial_language_from_cookie_expr,
set_language_to_cookie_bool,
Expand Down
2 changes: 1 addition & 1 deletion leptos-fluent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "leptos-fluent"
description = "Fluent framework for internationalization of Leptos applications"
edition.workspace = true
version = "0.0.36"
version = "0.0.37"
license = "MIT"
documentation.workspace = true
repository.workspace = true
Expand Down
10 changes: 6 additions & 4 deletions leptos-fluent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add the following to your `Cargo.toml` file:

```toml
[dependencies]
leptos-fluent = "0.0.36"
leptos-fluent = "0.0.37"
fluent-templates = "0.9"

[features]
Expand Down Expand Up @@ -117,6 +117,11 @@ fn App() -> impl IntoView {
// Get the initial language from `navigator.languages` if not
// found in the local storage. By default, it is `false`.
initial_language_from_navigator: true,
// Attributes to set for the language cookie. By default is `""`.
cookie_attrs: "SameSite=Strict; Secure; path=/; max-age=600",
// Update the language on cookie when using the method `I18n.set_language`.
// By default, it is `false`.
set_language_to_cookie: true,

// Server side options
// -------------------
Expand All @@ -131,9 +136,6 @@ fn App() -> impl IntoView {
cookie_name: "lang",
// Get the initial language from cookie. By default, it is `false`.
initial_language_from_cookie: true,
// Update the language on cookie when using the method `I18n.set_language`.
// By default, it is `false`.
set_language_to_cookie: true,
// URL parameter name to use discovering the initial language
// of the user. By default is `"lang"`.
url_param: "lang",
Expand Down
37 changes: 21 additions & 16 deletions leptos-fluent/src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,41 @@ pub fn get(name: &str) -> Option<String> {
}
}

pub fn set(name: &str, value: &str) {
#[cfg(not(feature = "ssr"))]
fn set_cookie(new_value: &str) {
use wasm_bindgen::JsCast;
leptos::document()
.dyn_into::<web_sys::HtmlDocument>()
.unwrap()
.set_cookie(new_value)
.unwrap();
}

pub fn set(name: &str, value: &str, attrs: &str) {
#[cfg(not(feature = "ssr"))]
{
use wasm_bindgen::JsCast;
leptos::document()
.dyn_into::<web_sys::HtmlDocument>()
.unwrap()
.set_cookie(&format!("{}={}", name, value))
.unwrap();
let mut new_value = format!("{}={}", name, value);
if !attrs.is_empty() {
new_value.push_str("; ");
new_value.push_str(attrs);
}
set_cookie(&new_value);
}

#[cfg(feature = "ssr")]
{
_ = name;
_ = value;
_ = attrs;
}
}

pub fn delete(name: &str) {
#[cfg(not(feature = "ssr"))]
{
use wasm_bindgen::JsCast;
leptos::document()
.dyn_into::<web_sys::HtmlDocument>()
.unwrap()
.set_cookie(&format!(
"{}=; expires=Thu, 01 Jan 1970 00:00:00 GMT",
name
))
.unwrap();
let new_value =
format!("{}=; expires=Thu, 01 Jan 1970 00:00:00 GMT", name);
set_cookie(&new_value);
}

#[cfg(feature = "ssr")]
Expand Down
10 changes: 6 additions & 4 deletions leptos-fluent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//!
//! ```toml
//! [dependencies]
//! leptos-fluent = "0.0.36"
//! leptos-fluent = "0.0.37"
//! fluent-templates = "0.9"
//!
//! [features]
Expand Down Expand Up @@ -112,6 +112,11 @@
//! // Get the initial language from `navigator.languages` if not
//! // found in the local storage. By default, it is `false`.
//! initial_language_from_navigator: true,
//! // Attributes to set for the language cookie. By default is `""`.
//! cookie_attrs: "SameSite=Strict; Secure; path=/; max-age=600",
//! // Update the language on cookie when using the method `I18n.set_language`.
//! // By default, it is `false`.
//! set_language_to_cookie: true,
//!
//! // Server side options
//! // -------------------
Expand All @@ -126,9 +131,6 @@
//! cookie_name: "lang",
//! // Get the initial language from cookie. By default, it is `false`.
//! initial_language_from_cookie: true,
//! // Update the language on cookie when using the method `I18n.set_language`.
//! // By default, it is `false`.
//! set_language_to_cookie: true,
//! // URL parameter name to use discovering the initial language
//! // of the user. By default is `"lang"`.
//! url_param: "lang",
Expand Down

0 comments on commit 0006e0e

Please sign in to comment.