Skip to content

Commit 95d28bb

Browse files
authored
feat(i18n): ✨ passed locale to build and request state (#43)
BREAKING CHANGE: build/request state now take locale as second parameter (request state takes request as third now)
1 parent 643e51e commit 95d28bb

File tree

11 files changed

+47
-20
lines changed

11 files changed

+47
-20
lines changed

examples/basic/src/templates/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
3737
.set_headers_fn(Rc::new(set_headers))
3838
}
3939

40-
pub async fn get_build_props(_path: String) -> RenderFnResultWithCause<String> {
40+
pub async fn get_build_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
4141
Ok(serde_json::to_string(&IndexPageProps {
4242
greeting: "Hello World!".to_string(),
4343
})?) // This `?` declares the default, that the server is the cause of the error

examples/i18n/src/templates/post.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
3737
}))
3838
}
3939

40-
pub async fn get_static_props(path: String) -> RenderFnResultWithCause<String> {
40+
pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
4141
// This is just an example
4242
let title = urlencoding::decode(&path).unwrap();
4343
let content = format!(

examples/showcase/src/templates/amalgamation.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ pub fn amalgamate_states(states: States) -> RenderFnResultWithCause<Option<Strin
4242
})?))
4343
}
4444

45-
pub async fn get_build_state(_path: String) -> RenderFnResultWithCause<String> {
45+
pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
4646
Ok(serde_json::to_string(&AmalagamationPageProps {
4747
message: "Hello from the build process!".to_string(),
4848
})?)
4949
}
5050

51-
pub async fn get_request_state(_path: String, _req: Request) -> RenderFnResultWithCause<String> {
51+
pub async fn get_request_state(
52+
_path: String,
53+
_locale: String,
54+
_req: Request,
55+
) -> RenderFnResultWithCause<String> {
5256
// Err(perseus::GenericErrorWithCause {
5357
// error: "this is a test error!".into(),
5458
// cause: perseus::ErrorCause::Client(None)

examples/showcase/src/templates/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
2727
}))
2828
}
2929

30-
pub async fn get_static_props(_path: String) -> RenderFnResultWithCause<String> {
30+
pub async fn get_static_props(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
3131
Ok(serde_json::to_string(&IndexPageProps {
3232
greeting: "Hello World!".to_string(),
3333
})?)

examples/showcase/src/templates/ip.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
3131
}))
3232
}
3333

34-
pub async fn get_request_state(_path: String, req: Request) -> RenderFnResultWithCause<String> {
34+
pub async fn get_request_state(
35+
_path: String,
36+
_locale: String,
37+
req: Request,
38+
) -> RenderFnResultWithCause<String> {
3539
// Err(perseus::GenericErrorWithCause {
3640
// error: "this is a test error!".into(),
3741
// cause: perseus::ErrorCause::Client(None)

examples/showcase/src/templates/post.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
3939
}))
4040
}
4141

42-
pub async fn get_static_props(path: String) -> RenderFnResultWithCause<String> {
42+
pub async fn get_static_props(path: String, _locale: String) -> RenderFnResultWithCause<String> {
4343
// This path is illegal, and can't be rendered
4444
if path == "post/tests" {
4545
return Err(GenericErrorWithCause {

examples/showcase/src/templates/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
3333
.build_paths_fn(Rc::new(get_build_paths))
3434
}
3535

36-
pub async fn get_build_state(path: String) -> RenderFnResultWithCause<String> {
36+
pub async fn get_build_state(path: String, _locale: String) -> RenderFnResultWithCause<String> {
3737
// This path is illegal, and can't be rendered
3838
if path == "timeisr/tests" {
3939
return Err(GenericErrorWithCause {

examples/showcase/src/templates/time_root.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn get_template<G: GenericNode>() -> Template<G> {
3131
.build_state_fn(Rc::new(get_build_state))
3232
}
3333

34-
pub async fn get_build_state(_path: String) -> RenderFnResultWithCause<String> {
34+
pub async fn get_build_state(_path: String, _locale: String) -> RenderFnResultWithCause<String> {
3535
Ok(serde_json::to_string(&TimePageProps {
3636
time: format!("{:?}", std::time::SystemTime::now()),
3737
})?)

packages/perseus/src/build.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub async fn build_template(
8585
if template.uses_build_state() && template.revalidates() {
8686
// We pass in the path to get a state (including the template path for consistency with the incremental logic)
8787
let initial_state = template
88-
.get_build_state(full_path_without_locale.clone())
88+
.get_build_state(full_path_without_locale.clone(), translator.get_locale())
8989
.await?;
9090
// Write that intial state to a static JSON file
9191
mutable_store
@@ -112,7 +112,7 @@ pub async fn build_template(
112112
} else if template.uses_build_state() {
113113
// We pass in the path to get a state (including the template path for consistency with the incremental logic)
114114
let initial_state = template
115-
.get_build_state(full_path_without_locale.clone())
115+
.get_build_state(full_path_without_locale.clone(), translator.get_locale())
116116
.await?;
117117
// Write that intial state to a static JSON file
118118
immutable_store

packages/perseus/src/serve.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ async fn render_request_state(
9696
req: Request,
9797
) -> Result<(String, String, Option<String>), ServerError> {
9898
// Generate the initial state (this may generate an error, but there's no file that can't exist)
99-
let state = Some(template.get_request_state(path.to_string(), req).await?);
99+
let state = Some(
100+
template
101+
.get_request_state(path.to_string(), translator.get_locale(), req)
102+
.await?,
103+
);
100104
// Use that to render the static HTML
101105
let html = sycamore::render_to_string(|| {
102106
template.render_for_template(state.clone(), Rc::clone(&translator), true)
@@ -177,7 +181,10 @@ async fn revalidate(
177181
// We need to regenerate and cache this page for future usage (until the next revalidation)
178182
let state = Some(
179183
template
180-
.get_build_state(format!("{}/{}", template.get_path(), path))
184+
.get_build_state(
185+
format!("{}/{}", template.get_path(), path),
186+
translator.get_locale(),
187+
)
181188
.await?,
182189
);
183190
let html = sycamore::render_to_string(|| {
@@ -297,7 +304,11 @@ pub async fn get_page_for_template(
297304
// All this uses the mutable store because this will be done at runtime
298305
None => {
299306
// We need to generate and cache this page for future usage
300-
let state = Some(template.get_build_state(path.to_string()).await?);
307+
let state = Some(
308+
template
309+
.get_build_state(path.to_string(), locale.to_string())
310+
.await?,
311+
);
301312
let html_val = sycamore::render_to_string(|| {
302313
template.render_for_template(state.clone(), Rc::clone(&translator), true)
303314
});

packages/perseus/src/template.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,14 @@ make_async_trait!(GetBuildPathsFnType, RenderFnResult<Vec<String>>);
120120
make_async_trait!(
121121
GetBuildStateFnType,
122122
RenderFnResultWithCause<String>,
123-
path: String
123+
path: String,
124+
locale: String
124125
);
125126
make_async_trait!(
126127
GetRequestStateFnType,
127128
RenderFnResultWithCause<String>,
128129
path: String,
130+
locale: String,
129131
req: Request
130132
);
131133
make_async_trait!(ShouldRevalidateFnType, RenderFnResultWithCause<bool>);
@@ -279,10 +281,15 @@ impl<G: GenericNode> Template<G> {
279281
}
280282
}
281283
/// Gets the initial state for a template. This needs to be passed the full path of the template, which may be one of those generated by
282-
/// `.get_build_paths()`.
283-
pub async fn get_build_state(&self, path: String) -> Result<String, ServerError> {
284+
/// `.get_build_paths()`. This also needs the locale being rendered to so that more compelx applications like custom documentation
285+
/// systems can be enabled.
286+
pub async fn get_build_state(
287+
&self,
288+
path: String,
289+
locale: String,
290+
) -> Result<String, ServerError> {
284291
if let Some(get_build_state) = &self.get_build_state {
285-
let res = get_build_state.call(path).await;
292+
let res = get_build_state.call(path, locale).await;
286293
match res {
287294
Ok(res) => Ok(res),
288295
Err(GenericErrorWithCause { error, cause }) => Err(ServerError::RenderFnFailed {
@@ -302,14 +309,15 @@ impl<G: GenericNode> Template<G> {
302309
}
303310
/// Gets the request-time state for a template. This is equivalent to SSR, and will not be performed at build-time. Unlike
304311
/// `.get_build_paths()` though, this will be passed information about the request that triggered the render. Errors here can be caused
305-
/// by either the server or the client, so the user must specify an [`ErrorCause`].
312+
/// by either the server or the client, so the user must specify an [`ErrorCause`]. This is also passed the locale being rendered to.
306313
pub async fn get_request_state(
307314
&self,
308315
path: String,
316+
locale: String,
309317
req: Request,
310318
) -> Result<String, ServerError> {
311319
if let Some(get_request_state) = &self.get_request_state {
312-
let res = get_request_state.call(path, req).await;
320+
let res = get_request_state.call(path, locale, req).await;
313321
match res {
314322
Ok(res) => Ok(res),
315323
Err(GenericErrorWithCause { error, cause }) => Err(ServerError::RenderFnFailed {

0 commit comments

Comments
 (0)