Skip to content

Commit

Permalink
Merge pull request #169 from Baptistemontan/routing_crate
Browse files Browse the repository at this point in the history
Split routing utils in their own crate
  • Loading branch information
Baptistemontan authored Dec 11, 2024
2 parents 959d365 + 72b274f commit c6500c8
Show file tree
Hide file tree
Showing 46 changed files with 274 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- master
env:
CARGO_TERM_COLOR: always
CARGO_LEPTOS_VERSION: 0.2.21
CARGO_LEPTOS_VERSION: 0.2.22
jobs:
lib_test:
name: Test leptos_i18n package
Expand Down
22 changes: 21 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"tests/json",
"tests/common",
"tests/namespaces",
"leptos_i18n_router",
]
exclude = ["examples", "tests"]

Expand All @@ -19,5 +20,24 @@ version = "0.5.0-rc1"
leptos_i18n_macro = { path = "./leptos_i18n_macro", default-features = false, version = "=0.5.0-rc1" }
leptos_i18n_parser = { path = "./leptos_i18n_parser", default-features = false, version = "=0.5.0-rc1" }
leptos_i18n = { path = "./leptos_i18n", version = "0.5.0-rc1" }
leptos_i18n_router = { path = "./leptos_i18n_router", version = "0.5.0-rc1" }

# leptos
leptos = { version = "0.7.0" }
leptos_router = { version = "0.7.0" }
leptos_meta = { version = "0.7.0" }

# icu
icu_locid = { version = "1.5" }
icu_provider = { version = "1.5" }
fixed_decimal = { version = "0.5" }
icu_datagen = { version = "1.5" }
icu_plurals = { version = "1.5", default-features = false }
icu_datetime = { version = "1.5", default-features = false }
icu_calendar = { version = "1.5", default-features = false }
icu_list = { version = "1.5", default-features = false }
icu_decimal = { version = "1.5", default-features = false }
icu_locid_transform = { version = "1.5", default-features = false }

# internal use
tests_common = { path = "./tests/common", version = "0.1.0" }
leptos = { version = "0.7.0-rc1" }
2 changes: 1 addition & 1 deletion docs/book/src/06_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This feature must be enabled when building the client in csr mode

Set a cookie to remember the last chosen locale.

#### `experimental-islands`
#### `islands`

This feature is, as it's name says, experimental.
This make this lib somewhat usable when using `islands` with Leptos.
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/usage/02_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The `I18nContextProvider` component accept multiple props, all optionnal (except

## Note on island

If you use the `experimental-islands` feature from Leptos the `I18nContextProvider` loose 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:
If you use the `islands` feature from Leptos the `I18nContextProvider` loose 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
use leptos_i18n::init_i18n_context_with_options;
Expand Down
45 changes: 13 additions & 32 deletions docs/book/src/usage/07_router.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# `I18nRoute`

The `i18n` module generated from the `load_locales!()` macro export the `I18nRoute` component,
You can use the `leptos_i18n_router` crate that export the `I18nRoute` component,
this component act exactly like a `leptos_router::Route` and take the same args, except for the path.

What it does is manage a prefix on the URL such that

```rust
use crate::i18n::I18nRoute;
use crate::i18n::Locale;
use leptos_i18n_router::I118nRoute;
use leptos::prelude::*;
use leptos_router::*;

view! {
<Router>
<Routes fallback=||"Page not found">
<I18nRoute view=Outlet>
<I18nRoute<Locale, _, _> view=Outlet>
<Route path=path!("") view=Home />
<Route path=path!("counter") view=Counter />
</I18nRoute>
</I18nRoute<Locale, _, _>>
</Routes>
</Router>
}
Expand Down Expand Up @@ -68,11 +69,11 @@ and the history will look like you directly navigated from `"/fr/counter"` to `"
You can use inside the `i18nRoute` the `i18n_path!` to create localized path segments:

```rust
use leptos_i18n::i18n_path;
use leptos_i18n_router::i18n_path;

<I18nRoute view=Outlet>
<I18nRoute<Locale, _, _> view=Outlet>
<Route path=i18n_path!(Locale, |locale| td_string(locale, segment_path_name)) view={/* */} />
</I18nRoute>
</I18nRoute<Locale, _, _>>
```

If you have `segment_path_name = "search"` for english, and `segment_path_name = "rechercher"` for french, the `I18nRoute` will produce 3 paths:
Expand All @@ -93,9 +94,9 @@ view! {
<Menu />
<Router>
<Routes fallback=||"Page not found">
<I18nRoute view=Outlet>
<I18nRoute<Locale, _, _> view=Outlet>
<Route path=path!("") view=Home />
</I18nRoute>
</I18nRoute<Locale, _, _>>
</Routes>
</Router>
</I18nContextProvider>
Expand All @@ -105,41 +106,21 @@ view! {
And the `Menu` component use localization, you could be suprised to see that sometimes there is a mismatch beetween the locale used by the `Menu` and the one inside the router.
This is due to the locale being read from the URL only when the `i18nRoute` is rendered. So the context may be initialized with another locale, and then hit the router that update it.

You have multiple solutions, you can either use the `Menu` component inside the `i18nRoute`:
One solution would be to use the `Menu` component inside the `i18nRoute`:

```rust
view! {
<I18nContextProvider>
<Router>
<Routes fallback=||"Page not found">
<I18nRoute view=|| view! {
<I18nRoute<Locale, _, _> view=|| view! {
<Menu />
<Outlet />
}>
<Route path=path!("") view=Home />
</I18nRoute>
</I18nRoute<Locale, _, _>>
</Routes>
</Router>
</I18nContextProvider>
}
```

Or you can use the `parse_locale_from_path` option on the `I18nContextProvider`:

```rust
view! {
<I18nContextProvider parse_locale_from_path="">
<Menu />
<Router>
<Routes fallback=||"Page not found">
<I18nRoute view=Outlet>
<Route path=path!("") view=Home />
</I18nRoute>
</Routes>
</Router>
</I18nContextProvider>
}
```

This option force the context to initialize itself with the locale from the URL. It is not enabled by default because the only time it is neededis this particular case.
It expect the base_path argument you would pass to the `I18nRoute`.
4 changes: 2 additions & 2 deletions examples/csr/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/counter_icu_datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", default-features = false, features = [
"json_files",
"csr",
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/counter_plurals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr", "plurals"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/counter_ranges/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/interpolation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/namespaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
7 changes: 4 additions & 3 deletions examples/csr/routing_csr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
leptos_i18n_router = { path = "../../../leptos_i18n_router" }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
wasm-bindgen = { version = "0.2" }

leptos_router = { version = "0.7.0-rc1" }
leptos_router = { version = "0.7.0" }

[package.metadata.leptos-i18n]
default = "en"
Expand Down
5 changes: 3 additions & 2 deletions examples/csr/routing_csr/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::i18n::*;
use leptos::prelude::*;
use leptos_i18n_router::I18nRoute;
use leptos_router::{components::*, path};

#[component]
Expand All @@ -11,10 +12,10 @@ pub fn App() -> impl IntoView {
<I18nContextProvider>
<Router>
<Routes fallback=|| "This page could not be found.">
<I18nRoute view=|| view! { <Outlet /> }>
<I18nRoute<Locale, _, _> view=|| view! { <Outlet /> }>
<Route path=path!("/") view=Home />
<Route path=path!("/counter") view=Counter />
</I18nRoute>
</I18nRoute<Locale, _, _>>
</Routes>
<br/>
<SwitchLang />
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/subcontext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/subkeys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", features = ["csr"] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1" }
Expand Down
4 changes: 2 additions & 2 deletions examples/csr/yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = { version = "0.7.0-rc1", features = ["csr"] }
leptos_meta = { version = "0.7.0-rc1" }
leptos = { version = "0.7.0", features = ["csr"] }
leptos_meta = { version = "0.7.0" }
leptos_i18n = { path = "../../../leptos_i18n", default-features = false, features = [
"csr",
"cookie",
Expand Down
9 changes: 5 additions & 4 deletions examples/dynamic_load/axum_island/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
axum = { version = "0.7", optional = true }
leptos = { version = "0.7.0-beta", features = ["experimental-islands"] }
leptos = { version = "0.7.0-beta", features = ["islands"] }
leptos_meta = { version = "0.7.0-beta" }
leptos_axum = { version = "0.7.0-beta", optional = true }
leptos_i18n = { path = "../../../leptos_i18n", features = [
"track_locale_files",
"experimental-islands",
"islands",
"dynamic_load",
] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
# wasm-bindgen = { version = "0.2", optional = true }
simple_logger = "4"
tokio = { version = "1.35", features = ["rt-multi-thread"], optional = true }
log = "0.4"
tower = { version = "0.5.1", optional = true }
tower-http = { version = "0.6.1", features = ["fs"], optional = true }
wasm-bindgen = "=0.2.96"

[features]
default = ["hydrate", "ssr"]
hydrate = [
"dep:console_error_panic_hook",
"dep:wasm-bindgen",
# "dep:wasm-bindgen",
"leptos/hydrate",
"leptos_i18n/hydrate",
]
Expand Down
11 changes: 6 additions & 5 deletions examples/dynamic_load/hello_world_actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
actix-files = { version = "0.6", optional = true }
actix-web = { version = "4.4", optional = true, features = ["macros"] }
leptos = { version = "0.7.0-rc1" }
leptos_meta = { version = "0.7.0-rc1" }
leptos_actix = { version = "0.7.0-rc1", optional = true }
leptos = { version = "0.7.0" }
leptos_meta = { version = "0.7.0" }
leptos_actix = { version = "0.7.0", optional = true }
leptos_i18n = { path = "../../../leptos_i18n", features = [
"track_locale_files",
"dynamic_load",
] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
# wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen = "=0.2.96"

[features]
default = ["hydrate", "ssr"]
hydrate = [
"dep:console_error_panic_hook",
"dep:wasm-bindgen",
# "dep:wasm-bindgen",
"leptos/hydrate",
"leptos_i18n/hydrate",
]
Expand Down
9 changes: 5 additions & 4 deletions examples/dynamic_load/hello_world_axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
axum = { version = "0.7", optional = true }
leptos = { version = "0.7.0-beta" }
leptos_meta = { version = "0.7.0-rc1" }
leptos_axum = { version = "0.7.0-rc1", optional = true }
leptos_meta = { version = "0.7.0" }
leptos_axum = { version = "0.7.0", optional = true }
leptos_i18n = { path = "../../../leptos_i18n", features = [
"track_locale_files",
"dynamic_load",
] }
serde = { version = "1", features = ["derive"] }
console_error_panic_hook = { version = "0.1", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
# wasm-bindgen = { version = "0.2", optional = true }
simple_logger = "4"
tokio = { version = "1.35", features = ["rt-multi-thread"], optional = true }
log = "0.4"
tower = { version = "0.5.1", optional = true }
tower-http = { version = "0.6.1", features = ["fs"], optional = true }
wasm-bindgen = "=0.2.96"

[features]
default = ["hydrate", "ssr"]
hydrate = [
"dep:console_error_panic_hook",
"dep:wasm-bindgen",
# "dep:wasm-bindgen",
"leptos/hydrate",
"leptos_i18n/hydrate",
]
Expand Down
Loading

0 comments on commit c6500c8

Please sign in to comment.