Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptistemontan committed Jan 12, 2025
2 parents b14e1a2 + 0103aff commit 15eaf10
Show file tree
Hide file tree
Showing 23 changed files with 127 additions and 112 deletions.
2 changes: 1 addition & 1 deletion docs/book/src/declare/01_key_value.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ But there are additional rules you must follow in addition to those of the forma

## Keys

Key names must be [valid Rust identifiers](https://doc.rust-lang.org/reference/identifiers.html), with the exception of `-` that would be converted to `_`.
Key names must be [valid Rust identifiers](https://doc.rust-lang.org/reference/identifiers.html), with the exception of `-` that would be converted to `_`, and does not support [strict](https://doc.rust-lang.org/reference/keywords.html#strict-keywords) or [reserved](https://doc.rust-lang.org/reference/keywords.html#reserved-keywords) keywords.

## Same keys across files

Expand Down
4 changes: 2 additions & 2 deletions docs/book/src/declare/02_interpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ You can mix them both without a problem:
}
```

## Names
## Values Names.

Just like keys, names of variables/components can be anything as long as it is a valid Rust identifier, apart from `-` which will be converted to `_`.
Values names must follow the same rules as [keys](./01_key_value.md#keys).
6 changes: 4 additions & 2 deletions docs/book/src/declare/03_plurals.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ This can be solved by defining 2 plural forms:

Providing the count to the `t!` macro, this will result in:

```rust
```rust,ignore
let i18n = use_i18n();
t!(i18n, items, count = || 0) // -> "0 items"
t!(i18n, items, count = || 1) // -> "1 item"
t!(i18n, items, count = || 4) // -> "4 items"
```

> All `items_*` are merged into the single key `items`.
`{{ count }}` is a special variable when using plurals. Even if you don't interpolate it, you must supply it:

```json
Expand All @@ -48,7 +50,7 @@ This will still need you to supply the `count` variable: `t!(i18n, items, count

Why bother and not just do

```rust
```rust,ignore
if item_count == 1 {
t!(i18n, items_one)
} else {
Expand Down
6 changes: 3 additions & 3 deletions docs/book/src/declare/04_ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,21 @@ With ranges, `{{ count }}` is a special variable that refers to the count provid
}
```

```rust
```rust,ignore
t!(i18n, click_count, count = || 0);
```

Will result in `"You have not clicked yet"` and

```rust
```rust,ignore
t!(i18n, click_count, count = || 5);
```

Will result in `"You clicked 5 times"`.

Providing `count` will create an error:

```rust
```rust,ignore
t!(i18n, click_count, count = 12, count = || 5); // compilation error
```

Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/declare/05_subkeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You can declare subkeys by just giving a map to the key:
}
```

```rust
```rust,ignore
t!(i18n, subkeys.subkey_1); // -> "This is subkey_1"
t!(i18n, subkeys.nested_subkeys.nested_subkey_1) // -> "you can nest subkeys"
```
8 changes: 4 additions & 4 deletions docs/book/src/declare/06_foreign_keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Arguments can be anything that could be parsed as a normal key-value:
}
```

```rust
```rust,ignore
t!(i18n, string_arg); // -> "str"
t!(i18n, boolean_arg); // -> "true"
t!(i18n, number_arg); // -> "56"
Expand Down Expand Up @@ -78,7 +78,7 @@ You can supply the count as a foreign key in 2 ways, as a variable:

This will just rename the key.

```rust
```rust,ignore
t!(i18n, new_key, new_count = move || 1); // -> "one item"
t!(i18n, new_key, new_count = move || 2); // -> "2 items"
```
Expand All @@ -94,7 +94,7 @@ Or by an actual value:
}
```

```rust
```rust,ignore
t!(i18n, singular_key); // -> "one item"
t!(i18n, multiple_key); // -> "6 items"
```
Expand Down Expand Up @@ -123,7 +123,7 @@ You can use `Foreign keys` to construct a single key from multiple plurals/range
}
```

```rust
```rust,ignore
t!(i18n, key, boys_count = move || 0, girls_count = move || 0); // -> "0 boys and 0 girls"
t!(i18n, key, boys_count = move || 0, girls_count = move || 1); // -> "0 boys and 1 girl"
t!(i18n, key, boys_count = move || 1, girls_count = move || 0); // -> "1 boy and 0 girls"
Expand Down
12 changes: 6 additions & 6 deletions docs/book/src/declare/08_formatters.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ This makes the variable needed to be `impl leptos_i18n::formatting::NumberFormat
- f32 \*
- f64 \*

>* Is implemented for convenience, but uses [`FixedDecimal::try_from_f64`](https://docs.rs/fixed_decimal/latest/fixed_decimal/struct.FixedDecimal.html#method.try_from_f64) with the floating precision; you may want to use your own.
> \* Is implemented for convenience, but uses [`FixedDecimal::try_from_f64`](https://docs.rs/fixed_decimal/latest/fixed_decimal/struct.FixedDecimal.html#method.try_from_f64) with the floating precision; you may want to use your own.
The formatter itself doesn’t provide formatting options such as maximum significant digits, but those can be customized through `FixedDecimal` before being passed to the formatter.

Expand All @@ -69,7 +69,7 @@ There are no arguments for this formatter at the moment.

### Example

```rust
```rust,ignore
use crate::i18n::*;
let i18n = use_i18n();
Expand Down Expand Up @@ -111,7 +111,7 @@ There is one argument at the moment for the date formatter: `date_length`, which

### Example

```rust
```rust,ignore
use crate::i18n::*;
use leptos_i18n::reexports::icu::calendar::Date;
Expand Down Expand Up @@ -154,7 +154,7 @@ There is one argument at the moment for the time formatter: `time_length`, which

### Example

```rust
```rust,ignore
use crate::i18n::*;
use leptos_i18n::reexports::icu::calendar::Date;
Expand Down Expand Up @@ -192,7 +192,7 @@ There are two arguments at the moment for the datetime formatter: `date_length`

### Example

```rust
```rust,ignore
use crate::i18n::*;
use leptos_i18n::reexports::icu::calendar::DateTime;
Expand Down Expand Up @@ -247,7 +247,7 @@ See [`Intl.ListFormat`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Ref

### Example

```rust
```rust,ignore
use crate::i18n::*;
let i18n = use_i18n();
Expand Down
3 changes: 3 additions & 0 deletions docs/book/src/infos/01_locale_resol.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ Here is the list of detection methods, sorted in priorities:
1. As a last resort, the default locale is used.

In SSR, it is always the server that resolves what locale to use; the client does not try to compute a locale when loading; the only locale changes that can happen are by explicitly setting it in the context.

_note_: URL pathname locale has a behavior that can be unexpected, it only resolve when the `I18nRoute` component start rendering, so if anything relied on the resolved locale before it,
it may have used a different locale than what it should. You can learn more on the [caveat section of the router chapter](../usage/07_router.md#caveat).
20 changes: 10 additions & 10 deletions docs/book/src/reduce_size/01_datagen.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ First, generate the information; you can use [`icu_datagen`](https://docs.rs/icu

Then you need to load those informations; this is as simple as

```rust
```rust,ignore
include!(concat!(env!("OUT_DIR"), "/baked_data/mod.rs"));
pub struct MyDataProvider;
Expand All @@ -31,7 +31,7 @@ This is explained in the `icu_datagen` doc.

You now just need to tell `leptos_i18n` what provider to use. For that, you first need to implement `IcuDataProvider` for your provider. You can do it manually as it is straightforward, but the lib comes with a derive macro:

```rust
```rust,ignore
include!(concat!(env!("OUT_DIR"), "/baked_data/mod.rs"));
#[derive(leptos_i18n::custom_provider::IcuDataProvider)]
Expand All @@ -42,7 +42,7 @@ impl_data_provider!(MyDataProvider);
And then pass it to the `set_icu_data_provider` function when the program starts,
so for CSR apps in the main function:

```rust
```rust,ignore
fn main() {
leptos_i18n::custom_provider::set_icu_data_provider(MyDataProvider);
console_error_panic_hook::set_once();
Expand All @@ -52,7 +52,7 @@ fn main() {

and for SSR apps in both on hydrate and on server startup:

```rust
```rust,ignore
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn hydrate() {
leptos_i18n::custom_provider::set_icu_data_provider(MyDataProvider);
Expand All @@ -61,7 +61,7 @@ pub fn hydrate() {
}
```

```rust
```rust,ignore
// example for actix
#[actix_web::main]
async fn main() -> std::io::Result<()> {
Expand All @@ -74,7 +74,7 @@ async fn main() -> std::io::Result<()> {

The doc for ICU4X datagen can be quite intimidating, but it is actually quite straightforward. Your build.rs can look like this:

```rust
```rust,ignore
use icu_datagen::baked_exporter::*;
use icu_datagen::prelude::*;
use std::path::PathBuf;
Expand Down Expand Up @@ -113,7 +113,7 @@ when all information is already in the translations.
leptos_i18n_build = "0.5.0"
```

```rust
```rust,ignore
use leptos_i18n_build::TranslationsInfos;
use std::path::PathBuf;
Expand All @@ -137,15 +137,15 @@ If your code uses plurals, it will build with information for plurals. If it use

If you use more data somehow, like for example using `t*_format!` with a formatter not used in the translations, there are functions to either supply additional options or keys:

```rust
```rust,ignore
use leptos_i18n_build::Options;
translations_infos.generate_data_with_options(mod_directory, [Options::FormatDateTime]).unwrap();
```

This will inject the ICU `DataKey`s needed for the `date`, `time`, and `datetime` formatters.

```rust
```rust,ignore
use leptos_i18n_build::Options;
translations_infos.generate_data_with_data_keys(
Expand All @@ -158,7 +158,7 @@ This will inject the keys for cardinal and ordinal plurals.

If you need both, `Options` can be turned into the needed keys:

```rust
```rust,ignore
use leptos_i18n_build::Options;
let mut keys = icu_datagen::keys(&["plurals/cardinal@1", "plurals/ordinal@1"])
Expand Down
6 changes: 3 additions & 3 deletions docs/book/src/reduce_size/02_dynamic_load.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ For obvious reasons, with the `"dynamic_load"` accessing a value is now async, `

You can turn them into some kind of `Signal<Option<String>>` using leptos `AsyncDerived`:

```rust
```rust,ignore
let i18n = use_i18n();
let translation = AsyncDerived::new(move || t_string!(i18n, key)); // .get() will return an `Option<&'static str>`
```

Feel free to make yourself a macro to wrap them:

```rust
```rust,ignore
macro_rules! t_string_async {
($($tt:tt),*) => {
leptos::prelude::AsyncDerived::new(move || leptos_i18n::t_string!($($tt),*))
Expand All @@ -50,7 +50,7 @@ but for the API to be the same on the client and the server they return the valu
If you use a backend that needs to manually register server functions,
you can use the `ServerFn` associated type on the `Locale` trait implemented by the generated `Locale` enum:

```rust
```rust,ignore
use i18n::Locale;
use leptos_i18n::Locale as LocaleTrait;
Expand Down
8 changes: 4 additions & 4 deletions docs/book/src/usage/01_load.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Loading all those translations is the role of the `load_locales!` macro. Just call this macro anywhere in your codebase, and it will generate the code needed to use your translations.

```rust
```rust,ignore
// lib.rs/main.rs
leptos_i18n::load_locales!();
Expand All @@ -24,7 +24,7 @@ locales = ["en", "fr"]

Generate this enum:

```rust
```rust,ignore
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Default)]
#[allow(non_camel_case_types)]
pub enum Locale {
Expand Down Expand Up @@ -58,7 +58,7 @@ It contains an associated constant for each locale, where every field is populat

This will generate this struct:

```rust
```rust,ignore
pub struct I18nKeys {
pub hello_world: &'static str,
}
Expand All @@ -76,6 +76,6 @@ assert_eq!(i18n::I18nKeys::fr.hello_world, "Bonjour le Monde!");

This way of accessing the values is possible, but it's not practical and most importantly not reactive. We will cover the `t!` macro later, which lets you access the values based on the context:

```rust
```rust,ignore
t!(i18n, hello_world)
```
12 changes: 6 additions & 6 deletions docs/book/src/usage/02_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The context is a wrapper around a `RwSignal` of the current locale. Every getter
The `load_locales!` macro generates the `I18nContextProvider` component in the `i18n` module,
you can use this component to make the context accessible to all child components.

```rust
```rust,ignore
use crate::i18n::*;
use leptos::prelude::*;
Expand All @@ -28,7 +28,7 @@ pub fn App() -> impl IntoView {

Once provided, you can access it with the `use_i18n` function, also generated in the `i18n` module.

```rust
```rust,ignore
use crate::i18n::*;
use leptos::prelude::*;
Expand All @@ -47,7 +47,7 @@ pub fn Foo() -> impl IntoView {

With the context, you can access the current locale with the `get_locale` method:

```rust
```rust,ignore
use crate::i18n::*;
use leptos::prelude::*;
Expand Down Expand Up @@ -81,7 +81,7 @@ A non-reactive counterpart to `get_locale` exists: `get_locale_untracked`.

With the context, you can change the current locale with the `set_locale` method. For example, this component will switch between `en` and `fr` with a button:

```rust
```rust,ignore
use crate::i18n::*;
use leptos::prelude::*;
Expand Down Expand Up @@ -128,7 +128,7 @@ The `I18nContextProvider` component accepts multiple props, all optional (except

If you use the `islands` feature from Leptos, the `I18nContextProvider` loses two props: `cookie_options` and `ssr_lang_header_getter`, because they are not serializable. If you need them, you can use the `init_context_with_options` function and provide the context yourself:

```rust
```rust,ignore
use leptos_i18n::init_i18n_context_with_options;
use leptos_i18n::context::{CookieOptions, UseLocalesOptions};
use leptos_meta::Html;
Expand Down Expand Up @@ -174,7 +174,7 @@ You could do it yourself by tracking the locale and setting the attribute yourse

The `I18nContext` implements `Directive` from Leptos to set the "lang" attribute, so you can just do

```rust
```rust,ignore
let i18n = use_i18n();
view! {
Expand Down
Loading

0 comments on commit 15eaf10

Please sign in to comment.