From fed97a269b83bc5c5464a49d48871daee3c9046b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 22 Dec 2024 22:20:44 +0100 Subject: [PATCH] build html with the correct lang --- src/routes/agreements.rs | 3 +++ src/routes/blog.rs | 6 ++++-- src/routes/contact.rs | 5 +++-- src/routes/contrib.rs | 1 + src/routes/cours.rs | 1 + src/routes/index.rs | 5 +++-- src/routes/not_found.rs | 1 + src/routes/portfolio.rs | 5 +++-- src/routes/web3.rs | 1 + src/template.rs | 13 ++++++++++++- src/utils/misc.rs | 33 +++++++++++++++++++++++++++------ templates/404.html | 2 +- templates/blog/index.html | 2 +- templates/blog/post.html | 2 +- templates/contact/index.html | 2 +- templates/contrib.html | 2 +- templates/cours.html | 2 +- templates/index.html | 2 +- templates/portfolio/index.html | 2 +- templates/web3.html | 2 +- 20 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index 3f584a2..867dc87 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -29,6 +29,7 @@ fn build_securitytxt(config: Config) -> String { pref_lang: config.fc.lang.unwrap_or_default(), }, InfosPage::default(), + None, ) } @@ -56,6 +57,7 @@ fn build_humanstxt(config: Config) -> String { name: config.fc.fullname.unwrap_or_default(), }, InfosPage::default(), + None, ) } @@ -95,5 +97,6 @@ fn build_webmanifest(config: Config) -> String { url: get_url(config.fc), }, InfosPage::default(), + None, ) } diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 6676794..0d58799 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -37,13 +37,13 @@ fn build_index(config: Config, lang: Lang) -> String { let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}")); // Get about - let about = read_file_fallback( + let (about, html_lang) = read_file_fallback( FilePath { base: blog_dir, path: "about.md".to_owned(), }, MType::Generic, - lang, + &lang, ); // Sort from newest to oldest @@ -69,6 +69,7 @@ fn build_index(config: Config, lang: Lang) -> String { )), kw: Some(make_kw(&["blog", "blogging"])), }, + Some(html_lang), ) } @@ -107,6 +108,7 @@ fn build_post(file: &str, config: Config) -> String { toc, }, infos, + None, ) } diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 494bf0b..0fca8b1 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -84,13 +84,13 @@ fn build_page(config: Config, lang: Lang) -> String { let ext = ".md"; // Get about - let about = read_file_fallback( + let (about, html_lang) = read_file_fallback( FilePath { base: contacts_dir.clone(), path: "about.md".to_owned(), }, MType::Generic, - lang, + &lang, ); let mut socials = read(&FilePath { @@ -139,5 +139,6 @@ fn build_page(config: Config, lang: Lang) -> String { "linktree", ])), }, + Some(html_lang), ) } diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index ddda4ba..d7b03ff 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -87,5 +87,6 @@ async fn build_page(config: Config) -> String { "code", ])), }, + None, ) } diff --git a/src/routes/cours.rs b/src/routes/cours.rs index ad3a711..6c64bdc 100644 --- a/src/routes/cours.rs +++ b/src/routes/cours.rs @@ -107,5 +107,6 @@ fn build_page(info: &web::Query, config: Config) -> String { "digital garden", ])), }, + None, ) } diff --git a/src/routes/index.rs b/src/routes/index.rs index da16348..cf574e8 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -36,13 +36,13 @@ struct StyleAvatar { #[cached(time = 60)] fn build_page(config: Config, lang: Lang) -> String { - let mut file = read_file_fallback( + let (mut file, html_lang) = read_file_fallback( FilePath { base: config.locations.data_dir.clone(), path: "index.md".to_owned(), }, MType::Index, - lang, + &lang, ); // Default values @@ -101,5 +101,6 @@ fn build_page(config: Config, lang: Lang) -> String { desc: Some("Page principale".into()), kw: Some(make_kw(&["index", "étudiant", "accueil"])), }, + Some(html_lang), ) } diff --git a/src/routes/not_found.rs b/src/routes/not_found.rs index 276bf78..8d97a66 100644 --- a/src/routes/not_found.rs +++ b/src/routes/not_found.rs @@ -32,5 +32,6 @@ fn build_page(config: Config) -> String { desc: Some("Une page perdu du web".into()), ..InfosPage::default() }, + None, ) } diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 9950eb5..b6dc58a 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -39,13 +39,13 @@ fn build_page(config: Config, lang: Lang) -> String { let ext = ".md"; // Get about - let about = read_file_fallback( + let (about, html_lang) = read_file_fallback( FilePath { base: projects_dir, path: "about.md".to_owned(), }, MType::Generic, - lang, + &lang, ); // Get apps @@ -115,5 +115,6 @@ fn build_page(config: Config, lang: Lang) -> String { "code", ])), }, + Some(html_lang), ) } diff --git a/src/routes/web3.rs b/src/routes/web3.rs index 1c00d6e..7f74544 100644 --- a/src/routes/web3.rs +++ b/src/routes/web3.rs @@ -22,5 +22,6 @@ fn build_page(config: Config) -> String { desc: Some("Coin reculé de l'internet".into()), kw: Some(make_kw(&["web3", "blockchain", "nft", "ai"])), }, + None, ) } diff --git a/src/template.rs b/src/template.rs index 205752d..5c813d1 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,5 +1,7 @@ use ramhorns::{Content, Ramhorns}; +use crate::utils::misc::Lang; + /// Structure used in the config variable of the app #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Template { @@ -51,12 +53,20 @@ struct DataPage { page_kw: Option, /// Author's name page_author: Option, + /// Language used + lang: String, /// Data needed to render the page data: T, } impl Template { - pub fn render(&self, template: &str, data: C, info: InfosPage) -> String { + pub fn render( + &self, + template: &str, + data: C, + info: InfosPage, + lang: Option, + ) -> String { let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap(); let tplt = templates.from_file(template).unwrap(); @@ -67,6 +77,7 @@ impl Template { page_desc: info.desc, page_kw: info.kw, page_author: self.name.clone(), + lang: lang.unwrap_or(Lang::default()), data, }) } diff --git a/src/utils/misc.rs b/src/utils/misc.rs index e267d7b..ebce6e8 100644 --- a/src/utils/misc.rs +++ b/src/utils/misc.rs @@ -59,12 +59,18 @@ impl Responder for Html { } /// Read a file localized, fallback to default file if localized file isn't found -pub fn read_file_fallback(filename: FilePath, expected_file: MType, lang: Lang) -> Option { - read_file(filename.clone(), expected_file, Some(lang)).or(read_file( - filename, - expected_file, - None, - )) +pub fn read_file_fallback( + filename: FilePath, + expected_file: MType, + lang: &Lang, +) -> (Option, String) { + match read_file(filename.clone(), expected_file, Some(lang.clone())) { + None => ( + read_file(filename, expected_file, None), + Lang::English.to_string(), + ), + data => (data, lang.to_string()), + } } /// Read a file @@ -143,6 +149,21 @@ pub enum Lang { English, } +impl Lang { + pub fn default() -> String { + Lang::French.to_string() + } +} + +impl std::fmt::Display for Lang { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Lang::French => write!(f, "fr"), + Lang::English => write!(f, "en"), + } + } +} + /// Get the browser language pub fn lang(headers: &HeaderMap) -> Lang { headers diff --git a/templates/404.html b/templates/404.html index ba07cf2..a22b350 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,5 +1,5 @@ - + {{>head.html}} diff --git a/templates/blog/index.html b/templates/blog/index.html index 50291fb..313932b 100644 --- a/templates/blog/index.html +++ b/templates/blog/index.html @@ -1,5 +1,5 @@ - + {{>head.html}} - + {{>head.html}} - + {{>head.html}} diff --git a/templates/contrib.html b/templates/contrib.html index a3f3f8c..0422a61 100644 --- a/templates/contrib.html +++ b/templates/contrib.html @@ -1,5 +1,5 @@ - + {{>head.html}} diff --git a/templates/cours.html b/templates/cours.html index 3b128c5..7616fd7 100644 --- a/templates/cours.html +++ b/templates/cours.html @@ -1,5 +1,5 @@ - + {{>head.html}} diff --git a/templates/index.html b/templates/index.html index 63b99f2..ecdc2b3 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,5 @@ - + {{>head.html}} diff --git a/templates/portfolio/index.html b/templates/portfolio/index.html index 8e095b6..1d02730 100644 --- a/templates/portfolio/index.html +++ b/templates/portfolio/index.html @@ -1,5 +1,5 @@ - + {{>head.html}} diff --git a/templates/web3.html b/templates/web3.html index c753149..86b44d9 100644 --- a/templates/web3.html +++ b/templates/web3.html @@ -1,5 +1,5 @@ - + {{page_title}}{{#page_title}} - {{/page_title}}{{app_name}}