Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: cleanup and docs

+59 -82
+2 -2
examples/i18n/src/main.rs
··· 2 2 mod routes; 3 3 4 4 use maudit::{BuildOptions, BuildOutput, content_sources, coronate, routes}; 5 - use routes::{About, Article, Contact, Index, Mixed}; 5 + use routes::{About, Article, Contact, Index}; 6 6 7 7 fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> { 8 8 coronate( 9 - routes![Index, About, Contact, Article, Mixed], 9 + routes![Index, About, Contact, Article], 10 10 content_sources![], 11 11 BuildOptions::default(), 12 12 )
+10 -17
examples/i18n/src/routes/contact.rs
··· 1 - use crate::layout::layout; 2 - use maud::html; 3 1 use maudit::route::prelude::*; 4 2 5 3 #[route( ··· 9 7 pub struct Contact; 10 8 11 9 impl Route for Contact { 12 - fn render(&self, _ctx: &mut PageContext) -> impl Into<RenderResult> { 13 - layout(html! { 14 - h1 { "Contact" } 15 - p { "This route demonstrates different locale syntaxes:" } 16 - p { "en uses prefix syntax, sv uses prefix syntax, de uses path syntax" } 17 - p { "Results: /en/contact, /sv/contact, /de/kontakt" } 18 - nav { 19 - ul { 20 - li { a href="/contact" { "Default" } } 21 - li { a href="/en/contact" { "English" } } 22 - li { a href="/sv/contact" { "Swedish" } } 23 - li { a href="/de/kontakt" { "German" } } 24 - } 25 - } 26 - }) 10 + fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> { 11 + match &ctx.variant { 12 + Some(language) => match language.as_str() { 13 + "en" => "Contact us.", 14 + "sv" => "Kontakta oss.", 15 + "de" => "Kontaktieren Sie uns.", 16 + _ => unreachable!(), 17 + }, 18 + _ => "Contact us.", 19 + } 27 20 } 28 21 }
+8 -3
examples/i18n/src/routes/index.rs
··· 10 10 layout(html! { 11 11 h1 { "i18n Example" } 12 12 p { "This route only exists as variants - no base path!" } 13 + p { "The current variant is: " (if let Some(variant) = &_ctx.variant { 14 + variant 15 + } else { 16 + "none" 17 + }) } 13 18 nav { 14 19 ul { 15 - li { a href="/en" { "English" } } 16 - li { a href="/sv" { "Swedish" } } 17 - li { a href="/de" { "German" } } 20 + li { a href="/en/" { "English" } } 21 + li { a href="/sv/" { "Swedish" } } 22 + li { a href="/de/" { "German" } } 18 23 } 19 24 } 20 25 })
-43
examples/i18n/src/routes/mixed.rs
··· 1 - use crate::layout::layout; 2 - use maud::html; 3 - use maudit::route::prelude::*; 4 - 5 - #[derive(Params, Clone)] 6 - pub struct MixedParams { 7 - pub id: String, 8 - } 9 - 10 - // Base route is static (/products) 11 - // But variants have dynamic parameters (/en/products/[id]) 12 - #[route(locales(en = "/en/products/[id]", sv = "/sv/produkter/[id]"))] 13 - pub struct Mixed; 14 - 15 - impl Route<MixedParams> for Mixed { 16 - fn pages(&self, _ctx: &mut DynamicRouteContext) -> Pages<MixedParams> { 17 - vec![ 18 - Page::from_params(MixedParams { 19 - id: "laptop".to_string(), 20 - }), 21 - Page::from_params(MixedParams { 22 - id: "phone".to_string(), 23 - }), 24 - ] 25 - } 26 - 27 - fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> { 28 - let params = ctx.params::<MixedParams>(); 29 - 30 - layout(html! { 31 - h1 { "Product: " (params.id) } 32 - p { "This route has a static base path but dynamic variants!" } 33 - nav { 34 - ul { 35 - li { a href="/en/products/laptop" { "English - Laptop" } } 36 - li { a href="/en/products/phone" { "English - Phone" } } 37 - li { a href="/sv/produkter/laptop" { "Swedish - Laptop" } } 38 - li { a href="/sv/produkter/phone" { "Swedish - Phone" } } 39 - } 40 - } 41 - }) 42 - } 43 - }
-2
examples/i18n/src/routes/mod.rs
··· 2 2 mod articles; 3 3 mod contact; 4 4 mod index; 5 - mod mixed; 6 5 7 6 pub use about::About; 8 7 pub use articles::Article; 9 8 pub use contact::Contact; 10 9 pub use index::Index; 11 - pub use mixed::Mixed;
-15
examples/i18n/src/routes/wrong_order.rs.test
··· 1 - use crate::layout::layout; 2 - use maud::html; 3 - use maudit::route::prelude::*; 4 - 5 - // This should produce a compile error because path comes after locales 6 - #[route(locales(en = "/en"), "/about")] 7 - pub struct WrongOrder; 8 - 9 - impl Route for WrongOrder { 10 - fn render(&self, _ctx: &mut PageContext) -> impl Into<RenderResult> { 11 - layout(html! { 12 - h1 { "This should not compile!" } 13 - }) 14 - } 15 - }
+39
website/content/docs/routing.md
··· 159 159 } 160 160 } 161 161 ``` 162 + 163 + ## Internationalization (i18n) 164 + 165 + Maudit includes the ability to generate *variants* of pages based on locales. For instance, you may have a `/about` page and want to create a `/fr/about` or `/a-propos` page with a localized slug. 166 + 167 + While you could do this by duplicating your `/about` page, creating a new struct, re-implementing Route etc etc, it would be quite time consuming if your website support more languages and probably lead to a lot of duplicated code, as your Swedish about page probably uses a lot of the same layout as your Danish one. 168 + 169 + To create these variants, specify the named `locales` attribute on your route, after the path: 170 + 171 + ```rs 172 + use maudit::route::prelude::*; 173 + 174 + #[route( 175 + "/contact", 176 + locales(sv(prefix = "/sv"), de(path = "/de/kontakt")) 177 + )] 178 + pub struct Contact; 179 + 180 + impl Route for Contact { 181 + fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> { 182 + match &ctx.variant { 183 + Some(language) => match language.as_str() { 184 + "sv" => "Kontakta oss.", 185 + "de" => "Kontaktieren Sie uns.", 186 + _ => unreachable!(), 187 + }, 188 + _ => "Contact us.", 189 + } 190 + } 191 + } 192 + ``` 193 + 194 + For this example, Maudit will generate three pages: 195 + 196 + - `/contact` 197 + - `/sv/contact` 198 + - `/de/kontakt` 199 + 200 + Calling `render()` three times with a different `ctx.variant` each time.