From efb8caa5f8220aa75146fedca1110c6476466212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Fri, 21 Jun 2024 02:15:20 +0200 Subject: [PATCH] Add method `activate` to `leptos_fluent::Language` struct (#133) --- CHANGELOG.md | 8 +++ Cargo.lock | 4 +- book/src/basic-usage.md | 71 ++++++++++++++------------- examples/csr-complete/src/lib.rs | 39 ++++++++------- examples/csr-minimal/src/lib.rs | 2 +- examples/ssr-hydrate-actix/src/app.rs | 2 +- examples/ssr-hydrate-axum/src/app.rs | 2 +- leptos-fluent-macros/Cargo.toml | 5 +- leptos-fluent-macros/src/lib.rs | 2 +- leptos-fluent/Cargo.toml | 2 +- leptos-fluent/src/lib.rs | 28 ++++++++--- 11 files changed, 97 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4543f391..1346e8fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 2024-06-21 - [0.1.1] + +### Enhancements + +- Add method `activate` to `leptos_fluent::Language` struct to set a language + active. Use `lang.activate()` instead of `expect_i18n().language.set(lang)`. + ## 2024-06-20 - [0.1.0] ### Breaking changes @@ -234,6 +241,7 @@ version to `0.1` during installation. - Added all ISO-639-1 and ISO-639-2 languages. +[0.1.1]: https://github.com/mondeja/leptos-fluent/compare/v0.1.0...v0.1.1 [0.1.0]: https://github.com/mondeja/leptos-fluent/compare/v0.0.37...v0.1.0 [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 diff --git a/Cargo.lock b/Cargo.lock index c24cca41..6d639be0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1446,7 +1446,7 @@ dependencies = [ [[package]] name = "leptos-fluent" -version = "0.1.0" +version = "0.1.1" dependencies = [ "fluent-templates", "leptos", @@ -1478,7 +1478,7 @@ dependencies = [ [[package]] name = "leptos-fluent-macros" -version = "0.1.0" +version = "0.1.1" dependencies = [ "fluent-syntax", "fluent-templates", diff --git a/book/src/basic-usage.md b/book/src/basic-usage.md index d1d15fa7..03a28e3b 100644 --- a/book/src/basic-usage.md +++ b/book/src/basic-usage.md @@ -31,7 +31,7 @@ language-selected-is = El idioma seleccionado es { $lang }. // src/lib.rs use fluent_templates::static_loader; use leptos::*; -use leptos_fluent::{expect_i18n, leptos_fluent, move_tr, tr}; +use leptos_fluent::{expect_i18n, leptos_fluent, move_tr, Language}; static_loader! { pub static TRANSLATIONS = { @@ -58,36 +58,36 @@ fn LanguageSelector() -> impl IntoView { view! {

{move_tr!("select-a-language")}

- { - move || i18n.languages.iter().map(|lang| { - view! { -
- - -
- } - }).collect::>() - } + {move || { + i18n.languages.iter().map(|lang| render_language(lang)).collect::>() + }}

- { - move || { - tr!( - "language-selected-is", - { "lang" => i18n.language.get().name } - ) - } - } + {move_tr!( + "language-selected-is", + { "lang" => i18n.language.get().name } + )}

} } + +fn render_language(lang: &'static Language) -> impl IntoView { + // Passed as atrribute, `Language` is converted to their code, + // so ` + + + + } +} ``` ```rust @@ -190,16 +190,13 @@ The i18n context has the following fields: - [`translations`]: A signal to the vector of [fluent-templates] loaders that stores the translations. -[`expect_i18n`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/fn.expect_i18n.html -[`language`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html#structfield.language -[`languages`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html#structfield.languages -[`translations`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html#structfield.translations -[fluent-templates]: https://docs.rs/fluent-templates/latest/fluent_templates - -To update the language, use `set` method of `language` field: +To update the language, use `set` method of `language` field or just +[`lang.activate()`] (new in v0.1.1): ```rust -i18n.language.set(lang) +expect_i18n().language.set(lang) + +lang.activate() // New in v0.1.1 ``` To get the current active language, use `get` method of `language` field: @@ -225,3 +222,9 @@ lang.is_active() [`move_tr!`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/macro.move_tr.html [`leptos_fluent::I18n`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html [`leptos_fluent::Language`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.Language.html +[`lang.activate()`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.Language.html#method.activate +[`expect_i18n`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/fn.expect_i18n.html +[`language`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html#structfield.language +[`languages`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html#structfield.languages +[`translations`]: https://docs.rs/leptos-fluent/latest/leptos_fluent/struct.I18n.html#structfield.translations +[fluent-templates]: https://docs.rs/fluent-templates/latest/fluent_templates diff --git a/examples/csr-complete/src/lib.rs b/examples/csr-complete/src/lib.rs index dd3fb10b..2b3d30d6 100644 --- a/examples/csr-complete/src/lib.rs +++ b/examples/csr-complete/src/lib.rs @@ -1,6 +1,6 @@ use fluent_templates::static_loader; use leptos::*; -use leptos_fluent::{expect_i18n, leptos_fluent, move_tr}; +use leptos_fluent::{expect_i18n, leptos_fluent, move_tr, Language}; static_loader! { static TRANSLATIONS = { @@ -44,24 +44,7 @@ fn ChildComponent() -> impl IntoView {
{move || { - i18n.languages - .iter() - .map(|lang| { - view! { -
- - -
- } - }) - .collect::>() + i18n.languages.iter().map(|lang| render_language(lang)).collect::>() }}
@@ -81,3 +64,21 @@ fn ChildComponent() -> impl IntoView { } } + +fn render_language(lang: &'static Language) -> impl IntoView { + // Passed as atrribute, `Language` is converted to their code, + // so ` + + + + } +} diff --git a/examples/csr-minimal/src/lib.rs b/examples/csr-minimal/src/lib.rs index be006959..6618a88f 100644 --- a/examples/csr-minimal/src/lib.rs +++ b/examples/csr-minimal/src/lib.rs @@ -40,7 +40,7 @@ pub fn LanguageSelector() -> impl IntoView { name="language" value=lang checked=lang.is_active() - on:click=move |_| i18n.language.set(lang) + on:click=move |_| lang.activate() /> diff --git a/examples/ssr-hydrate-actix/src/app.rs b/examples/ssr-hydrate-actix/src/app.rs index 081b5b93..eddb5d60 100644 --- a/examples/ssr-hydrate-actix/src/app.rs +++ b/examples/ssr-hydrate-actix/src/app.rs @@ -71,7 +71,7 @@ fn HomePage() -> impl IntoView { name="language" value=lang checked=lang.is_active() - on:click=move |_| i18n.language.set(lang) + on:click=move |_| lang.activate() /> diff --git a/examples/ssr-hydrate-axum/src/app.rs b/examples/ssr-hydrate-axum/src/app.rs index 4b1a68d9..09cba875 100644 --- a/examples/ssr-hydrate-axum/src/app.rs +++ b/examples/ssr-hydrate-axum/src/app.rs @@ -79,7 +79,7 @@ fn HomePage() -> impl IntoView { name="language" value=lang checked=lang.is_active() - on:click=move |_| i18n.language.set(lang) + on:click=move |_| lang.activate() /> diff --git a/leptos-fluent-macros/Cargo.toml b/leptos-fluent-macros/Cargo.toml index 8ae7aebb..bea49165 100644 --- a/leptos-fluent-macros/Cargo.toml +++ b/leptos-fluent-macros/Cargo.toml @@ -2,7 +2,7 @@ name = "leptos-fluent-macros" description = "Macros for leptos-fluent" edition.workspace = true -version = "0.1.0" +version = "0.1.1" license = "MIT" documentation.workspace = true repository.workspace = true @@ -44,7 +44,8 @@ skip_feature_sets = [ ["actix", "csr"], ["actix", "hydrate"], ["axum", "csr"], - ["axum", "hydrate"] + ["axum", "hydrate"], + ["ssr"], ] always_include_features = ["json"] denylist = ["yaml", "json5"] diff --git a/leptos-fluent-macros/src/lib.rs b/leptos-fluent-macros/src/lib.rs index 53852542..83caa369 100644 --- a/leptos-fluent-macros/src/lib.rs +++ b/leptos-fluent-macros/src/lib.rs @@ -362,7 +362,7 @@ pub fn leptos_fluent( let hydrate_rerender_quote = quote! { ::leptos::create_effect(move |prev| { if prev.is_none() { - ::leptos_fluent::expect_i18n().language.set(l); + l.activate(); } }); }; diff --git a/leptos-fluent/Cargo.toml b/leptos-fluent/Cargo.toml index 7c4450e0..a58ee265 100644 --- a/leptos-fluent/Cargo.toml +++ b/leptos-fluent/Cargo.toml @@ -2,7 +2,7 @@ name = "leptos-fluent" description = "Fluent framework for internationalization of Leptos applications" edition.workspace = true -version = "0.1.0" +version = "0.1.1" license = "MIT" documentation.workspace = true repository.workspace = true diff --git a/leptos-fluent/src/lib.rs b/leptos-fluent/src/lib.rs index ea79ecdc..e1f697d7 100644 --- a/leptos-fluent/src/lib.rs +++ b/leptos-fluent/src/lib.rs @@ -241,7 +241,7 @@ use fluent_templates::{ }; use leptos::{ use_context, Attribute, IntoAttribute, Oco, RwSignal, Signal, SignalGet, - SignalWith, + SignalSet, SignalWith, }; pub use leptos_fluent_macros::leptos_fluent; @@ -287,10 +287,17 @@ pub struct Language { } impl Language { - /// Check if the language is the active language. + /// Get if the language is the active language. + #[inline(always)] pub fn is_active(&self) -> bool { self == expect_i18n().language.get() } + + /// Set the language as the active language. + #[inline(always)] + pub fn activate(&'static self) { + expect_i18n().language.set(self); + } } impl PartialEq for Language { @@ -329,6 +336,15 @@ impl FromStr for Language { macro_rules! impl_into_attr_for_language { () => { + /// Convert a language to an HTML attribute passing the language identifier. + /// + /// ```rust,ignore + /// // The following code: + /// + /// // is the same as + /// + /// ``` + #[inline(always)] fn into_attribute(self) -> Attribute { Attribute::String(Oco::Owned(self.id.to_string())) } @@ -350,8 +366,8 @@ impl IntoAttribute for &&'static Language { /// Internationalization context. /// -/// This context is used to provide the current language, the available languages -/// and all the translations. It is capable of doing what is needed to translate +/// Used to provide the current language, the available languages and all +/// the translations. It is capable of doing what is needed to translate /// and manage translations in a whole application. #[derive(Clone, Copy)] pub struct I18n { @@ -409,7 +425,7 @@ pub fn tr_impl(text_id: &str) -> String { None }); - found.unwrap_or("Unknown localization {text_id}".to_string()) + found.unwrap_or(format!("Unknown localization {text_id}")) } #[doc(hidden)] @@ -431,7 +447,7 @@ pub fn tr_with_args_impl( None }); - found.unwrap_or("Unknown localization {text_id}".to_string()) + found.unwrap_or(format!("Unknown localization {text_id}")) } /// Translate a text identifier to the current language.