Skip to content

Commit ed0a9c3

Browse files
committed
Add locale support to 404 pages
1 parent 3bc1f9a commit ed0a9c3

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/main.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ use std::path::{Path, PathBuf};
4242
use rand::seq::SliceRandom;
4343

4444
use rocket::{
45-
http::Status,
45+
http::{RawStr, Status},
46+
request::{FromParam, Request},
4647
response::{NamedFile, Redirect},
4748
};
4849
use rocket_contrib::templates::Template;
@@ -89,7 +90,12 @@ fn get_title(page_name: &str) -> String {
8990

9091
#[get("/components/<_file..>", rank = 1)]
9192
fn components(_file: PathBuf) -> Template {
92-
not_found()
93+
not_found_locale(ENGLISH.into())
94+
}
95+
96+
#[get("/<locale>/components/<_file..>", rank = 11)]
97+
fn components_locale(locale: SupportedLocale, _file: PathBuf) -> Template {
98+
not_found_locale(locale.0)
9399
}
94100

95101
#[get("/logos/<file..>", rank = 1)]
@@ -204,7 +210,21 @@ fn redirect_locale(_locale: redirect::Locale, dest: redirect::Destination) -> Re
204210
}
205211

206212
#[catch(404)]
207-
fn not_found() -> Template {
213+
fn not_found(req: &Request) -> Template {
214+
let lang = if let Some(next) = req.uri().segments().next() {
215+
if let Ok(lang) = SupportedLocale::from_param(RawStr::from_str(next)) {
216+
lang.0
217+
} else {
218+
ENGLISH.into()
219+
}
220+
} else {
221+
ENGLISH.into()
222+
};
223+
224+
not_found_locale(lang)
225+
}
226+
227+
fn not_found_locale(lang: String) -> Template {
208228
let page = "404";
209229
let title = format!("{} - Rust programming language", page).to_string();
210230
let context = Context {
@@ -213,7 +233,7 @@ fn not_found() -> Template {
213233
parent: LAYOUT.to_string(),
214234
is_landing: false,
215235
data: (),
216-
lang: ENGLISH.to_string(),
236+
lang,
217237
baseurl: String::new(),
218238
pontoon_enabled: pontoon_enabled(),
219239
};
@@ -222,7 +242,7 @@ fn not_found() -> Template {
222242

223243
#[catch(500)]
224244
fn catch_error() -> Template {
225-
not_found()
245+
not_found_locale(ENGLISH.into())
226246
}
227247

228248
fn compile_sass(filename: &str) {
@@ -420,6 +440,7 @@ fn main() {
420440
team_locale,
421441
production_locale,
422442
subject_locale,
443+
components_locale,
423444
redirect,
424445
redirect_pdfs,
425446
redirect_bare_en_us,

0 commit comments

Comments
 (0)