Skip to content

Commit

Permalink
Add method activate to leptos_fluent::Language struct (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Jun 21, 2024
1 parent 388f29b commit efb8caa
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 68 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
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.

71 changes: 37 additions & 34 deletions book/src/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -58,36 +58,36 @@ fn LanguageSelector() -> impl IntoView {
view! {
<p>{move_tr!("select-a-language")}</p>
<fieldset>
{
move || i18n.languages.iter().map(|lang| {
view! {
<div>
<input
type="radio"
id=lang
name="language"
value=lang
checked=lang.is_active()
on:click=move |_| i18n.language.set(lang)
/>
<label for=lang>{lang.name}</label>
</div>
}
}).collect::<Vec<_>>()
}
{move || {
i18n.languages.iter().map(|lang| render_language(lang)).collect::<Vec<_>>()
}}
</fieldset>
<p>
{
move || {
tr!(
"language-selected-is",
{ "lang" => i18n.language.get().name }
)
}
}
{move_tr!(
"language-selected-is",
{ "lang" => i18n.language.get().name }
)}
</p>
}
}

fn render_language(lang: &'static Language) -> impl IntoView {
// Passed as atrribute, `Language` is converted to their code,
// so `<input id=lang` becomes `<input id=lang.id.to_string()`
view! {
<div>
<input
id=lang
name="language"
value=lang
checked=lang.is_active()
on:click=move |_| expect_i18n().language.set(lang)
type="radio"
/>
<label for=lang>{lang.name}</label>
</div>
}
}
```

```rust
Expand Down Expand Up @@ -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:
Expand All @@ -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
39 changes: 20 additions & 19 deletions examples/csr-complete/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -44,24 +44,7 @@ fn ChildComponent() -> impl IntoView {
<fieldset>

{move || {
i18n.languages
.iter()
.map(|lang| {
view! {
<div>
<input
type="radio"
id=lang
name="language"
value=lang
checked=lang.is_active()
on:click=move |_| i18n.language.set(lang)
/>
<label for=lang>{lang.name}</label>
</div>
}
})
.collect::<Vec<_>>()
i18n.languages.iter().map(|lang| render_language(lang)).collect::<Vec<_>>()
}}

</fieldset>
Expand All @@ -81,3 +64,21 @@ fn ChildComponent() -> impl IntoView {
</ul>
}
}

fn render_language(lang: &'static Language) -> impl IntoView {
// Passed as atrribute, `Language` is converted to their code,
// so `<input id=lang` becomes `<input id=lang.id.to_string()`
view! {
<div>
<input
type="radio"
id=lang
name="language"
value=lang
checked=lang.is_active()
on:click=move |_| lang.activate()
/>
<label for=lang>{lang.name}</label>
</div>
}
}
2 changes: 1 addition & 1 deletion examples/csr-minimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
/>
<label for=lang>{lang.name}</label>
</div>
Expand Down
2 changes: 1 addition & 1 deletion examples/ssr-hydrate-actix/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
/>
<label for=lang>{lang.name}</label>
</div>
Expand Down
2 changes: 1 addition & 1 deletion examples/ssr-hydrate-axum/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
/>
<label for=lang>{lang.name}</label>
</div>
Expand Down
5 changes: 3 additions & 2 deletions 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.1.0"
version = "0.1.1"
license = "MIT"
documentation.workspace = true
repository.workspace = true
Expand Down Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion leptos-fluent-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
};
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.1.0"
version = "0.1.1"
license = "MIT"
documentation.workspace = true
repository.workspace = true
Expand Down
28 changes: 22 additions & 6 deletions leptos-fluent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
/// <input id={lang} ... >
/// // is the same as
/// <input id={lang.id.to_string()} ... >
/// ```
#[inline(always)]
fn into_attribute(self) -> Attribute {
Attribute::String(Oco::Owned(self.id.to_string()))
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)]
Expand All @@ -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.
Expand Down

0 comments on commit efb8caa

Please sign in to comment.