Skip to content

Commit 6bbd3cb

Browse files
committed
feat(i18n): created switch_locale function on reactor
Also cleaned up some minor import errors.
1 parent 9420cb7 commit 6bbd3cb

File tree

8 files changed

+33
-19
lines changed

8 files changed

+33
-19
lines changed

examples/core/i18n/src/templates/about.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ use sycamore::prelude::*;
44
fn about_page<G: Html>(cx: Scope) -> View<G> {
55
view! { cx,
66
p { (t!(cx, "about")) }
7-
p {
8-
(
9-
if !G::IS_BROWSER {
10-
"This is running on the server."
11-
} else {
12-
"This is running on the client."
13-
}
14-
)
15-
}
7+
button(on:click = move |_| {
8+
#[cfg(client)]
9+
Reactor::<G>::from_cx(cx).switch_locale("fr-FR");
10+
}) { "Switch to French" }
1611
}
1712
}
1813

packages/perseus-macro/src/entrypoint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ pub fn main_impl(input: MainFn, server_fn: Path) -> TokenStream {
181181

182182
// The browser-specific `main` function
183183
#[cfg(client)]
184-
pub fn main() -> ::perseus::ClientReturn {
185-
::perseus::run_client(__perseus_simple_main);
184+
pub fn main() -> ::perseus::client::ClientReturn {
185+
::perseus::client::run_client(__perseus_simple_main);
186186
Ok(())
187187
}
188188

packages/perseus/src/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::reactor::Reactor;
2-
use crate::{checkpoint, plugins::PluginAction, template::BrowserNodeType};
32
use crate::{i18n::TranslationsManager, init::PerseusAppBase, stores::MutableStore};
3+
use crate::{plugins::PluginAction, template::BrowserNodeType, utils::checkpoint};
44
use sycamore::prelude::create_scope;
55
#[cfg(feature = "hydrate")]
66
use sycamore::utils::hydrate::with_hydration_context;

packages/perseus/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ pub mod template;
5252
/// General utilities that may be useful while building Perseus apps.
5353
pub mod utils;
5454

55+
/// Utilities for initializing the Perseus client.
5556
#[cfg(all(feature = "client-helpers", any(client, doc)))]
56-
mod client;
57+
pub mod client;
5758
mod init;
5859
mod page_data;
5960
/// Utilities for working with typed paths.

packages/perseus/src/reactor/initial_load.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::collections::HashMap;
22

33
use crate::{
4-
checkpoint,
54
error_views::ServerErrorData,
65
errors::*,
76
i18n::detect_locale,
87
path::PathMaybeWithLocale,
98
router::{match_route, FullRouteInfo, FullRouteVerdict, RouterLoadState},
109
state::TemplateState,
11-
utils::get_path_prefix_client,
10+
utils::{checkpoint, get_path_prefix_client},
1211
};
1312
use serde_json::Value;
1413
use sycamore::{

packages/perseus/src/reactor/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,27 @@ impl<G: Html> Reactor<G> {
253253
pub fn get_translator(&self) -> Translator {
254254
self.try_get_translator().expect("translator not available")
255255
}
256+
/// Switches the current locale to the given locale. This will navigate to
257+
/// the current page in the given locale.
258+
///
259+
/// If a new page is being loaded, or if an error view is loaded, this will
260+
/// simply have no effect whatsoever (to avoid users trying to switch
261+
/// locales during a navigation and inadvertently causing a panic).
262+
///
263+
/// # Panics
264+
///
265+
/// This will panic if the given locale is not supported: use this only with
266+
/// hardcoded locale values! This will also panic if used in an error
267+
/// view without a translator.
268+
#[cfg(client)]
269+
pub fn switch_locale(&self, new_locale: &str) {
270+
let path = self.router_state.get_path();
271+
if let Some(path) = path {
272+
let curr_locale = self.get_translator().get_locale();
273+
let new_path = path.replace(&curr_locale, new_locale);
274+
sycamore_router::navigate(&new_path);
275+
}
276+
}
256277
}
257278

258279
#[cfg(engine)]

packages/perseus/src/reactor/start.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use super::Reactor;
22
use crate::{
3-
checkpoint,
43
error_views::ErrorPosition,
54
errors::ClientError,
65
reactor::InitialView,
76
router::{PageDisposer, PerseusRoute, RouteVerdict, RouterLoadState},
87
template::BrowserNodeType,
9-
utils::{render_or_hydrate, replace_head},
8+
utils::{checkpoint, render_or_hydrate, replace_head},
109
};
1110
use sycamore::prelude::{create_effect, create_signal, on_mount, view, ReadSignal, Scope, View};
1211
use sycamore_futures::spawn_local_scoped;

packages/perseus/src/reactor/subsequent_load.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ use sycamore::{
66
};
77

88
use crate::{
9-
checkpoint,
109
errors::{AssetType, ClientError, ClientInvariantError},
1110
i18n::detect_locale,
1211
page_data::PageDataPartial,
1312
path::PathMaybeWithLocale,
1413
router::{FullRouteInfo, FullRouteVerdict, RouteVerdict, RouterLoadState},
1514
state::{PssContains, TemplateState},
16-
utils::{fetch, get_path_prefix_client, replace_head},
15+
utils::{checkpoint, fetch, get_path_prefix_client, replace_head},
1716
};
1817

1918
use super::Reactor;

0 commit comments

Comments
 (0)