build html with the correct lang

This commit is contained in:
Mylloon 2024-12-22 22:20:44 +01:00
parent f725318f56
commit fed97a269b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
20 changed files with 68 additions and 24 deletions

View file

@ -29,6 +29,7 @@ fn build_securitytxt(config: Config) -> String {
pref_lang: config.fc.lang.unwrap_or_default(), pref_lang: config.fc.lang.unwrap_or_default(),
}, },
InfosPage::default(), InfosPage::default(),
None,
) )
} }
@ -56,6 +57,7 @@ fn build_humanstxt(config: Config) -> String {
name: config.fc.fullname.unwrap_or_default(), name: config.fc.fullname.unwrap_or_default(),
}, },
InfosPage::default(), InfosPage::default(),
None,
) )
} }
@ -95,5 +97,6 @@ fn build_webmanifest(config: Config) -> String {
url: get_url(config.fc), url: get_url(config.fc),
}, },
InfosPage::default(), InfosPage::default(),
None,
) )
} }

View file

@ -37,13 +37,13 @@ fn build_index(config: Config, lang: Lang) -> String {
let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}")); let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}"));
// Get about // Get about
let about = read_file_fallback( let (about, html_lang) = read_file_fallback(
FilePath { FilePath {
base: blog_dir, base: blog_dir,
path: "about.md".to_owned(), path: "about.md".to_owned(),
}, },
MType::Generic, MType::Generic,
lang, &lang,
); );
// Sort from newest to oldest // Sort from newest to oldest
@ -69,6 +69,7 @@ fn build_index(config: Config, lang: Lang) -> String {
)), )),
kw: Some(make_kw(&["blog", "blogging"])), kw: Some(make_kw(&["blog", "blogging"])),
}, },
Some(html_lang),
) )
} }
@ -107,6 +108,7 @@ fn build_post(file: &str, config: Config) -> String {
toc, toc,
}, },
infos, infos,
None,
) )
} }

View file

@ -84,13 +84,13 @@ fn build_page(config: Config, lang: Lang) -> String {
let ext = ".md"; let ext = ".md";
// Get about // Get about
let about = read_file_fallback( let (about, html_lang) = read_file_fallback(
FilePath { FilePath {
base: contacts_dir.clone(), base: contacts_dir.clone(),
path: "about.md".to_owned(), path: "about.md".to_owned(),
}, },
MType::Generic, MType::Generic,
lang, &lang,
); );
let mut socials = read(&FilePath { let mut socials = read(&FilePath {
@ -139,5 +139,6 @@ fn build_page(config: Config, lang: Lang) -> String {
"linktree", "linktree",
])), ])),
}, },
Some(html_lang),
) )
} }

View file

@ -87,5 +87,6 @@ async fn build_page(config: Config) -> String {
"code", "code",
])), ])),
}, },
None,
) )
} }

View file

@ -107,5 +107,6 @@ fn build_page(info: &web::Query<PathRequest>, config: Config) -> String {
"digital garden", "digital garden",
])), ])),
}, },
None,
) )
} }

View file

@ -36,13 +36,13 @@ struct StyleAvatar {
#[cached(time = 60)] #[cached(time = 60)]
fn build_page(config: Config, lang: Lang) -> String { fn build_page(config: Config, lang: Lang) -> String {
let mut file = read_file_fallback( let (mut file, html_lang) = read_file_fallback(
FilePath { FilePath {
base: config.locations.data_dir.clone(), base: config.locations.data_dir.clone(),
path: "index.md".to_owned(), path: "index.md".to_owned(),
}, },
MType::Index, MType::Index,
lang, &lang,
); );
// Default values // Default values
@ -101,5 +101,6 @@ fn build_page(config: Config, lang: Lang) -> String {
desc: Some("Page principale".into()), desc: Some("Page principale".into()),
kw: Some(make_kw(&["index", "étudiant", "accueil"])), kw: Some(make_kw(&["index", "étudiant", "accueil"])),
}, },
Some(html_lang),
) )
} }

View file

@ -32,5 +32,6 @@ fn build_page(config: Config) -> String {
desc: Some("Une page perdu du web".into()), desc: Some("Une page perdu du web".into()),
..InfosPage::default() ..InfosPage::default()
}, },
None,
) )
} }

View file

@ -39,13 +39,13 @@ fn build_page(config: Config, lang: Lang) -> String {
let ext = ".md"; let ext = ".md";
// Get about // Get about
let about = read_file_fallback( let (about, html_lang) = read_file_fallback(
FilePath { FilePath {
base: projects_dir, base: projects_dir,
path: "about.md".to_owned(), path: "about.md".to_owned(),
}, },
MType::Generic, MType::Generic,
lang, &lang,
); );
// Get apps // Get apps
@ -115,5 +115,6 @@ fn build_page(config: Config, lang: Lang) -> String {
"code", "code",
])), ])),
}, },
Some(html_lang),
) )
} }

View file

@ -22,5 +22,6 @@ fn build_page(config: Config) -> String {
desc: Some("Coin reculé de l'internet".into()), desc: Some("Coin reculé de l'internet".into()),
kw: Some(make_kw(&["web3", "blockchain", "nft", "ai"])), kw: Some(make_kw(&["web3", "blockchain", "nft", "ai"])),
}, },
None,
) )
} }

View file

@ -1,5 +1,7 @@
use ramhorns::{Content, Ramhorns}; use ramhorns::{Content, Ramhorns};
use crate::utils::misc::Lang;
/// Structure used in the config variable of the app /// Structure used in the config variable of the app
#[derive(Clone, Debug, Hash, PartialEq, Eq)] #[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Template { pub struct Template {
@ -51,12 +53,20 @@ struct DataPage<T> {
page_kw: Option<String>, page_kw: Option<String>,
/// Author's name /// Author's name
page_author: Option<String>, page_author: Option<String>,
/// Language used
lang: String,
/// Data needed to render the page /// Data needed to render the page
data: T, data: T,
} }
impl Template { impl Template {
pub fn render<C: Content>(&self, template: &str, data: C, info: InfosPage) -> String { pub fn render<C: Content>(
&self,
template: &str,
data: C,
info: InfosPage,
lang: Option<String>,
) -> String {
let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap(); let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap();
let tplt = templates.from_file(template).unwrap(); let tplt = templates.from_file(template).unwrap();
@ -67,6 +77,7 @@ impl Template {
page_desc: info.desc, page_desc: info.desc,
page_kw: info.kw, page_kw: info.kw,
page_author: self.name.clone(), page_author: self.name.clone(),
lang: lang.unwrap_or(Lang::default()),
data, data,
}) })
} }

View file

@ -59,12 +59,18 @@ impl Responder for Html {
} }
/// Read a file localized, fallback to default file if localized file isn't found /// 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<File> { pub fn read_file_fallback(
read_file(filename.clone(), expected_file, Some(lang)).or(read_file( filename: FilePath,
filename, expected_file: MType,
expected_file, lang: &Lang,
None, ) -> (Option<File>, 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 /// Read a file
@ -143,6 +149,21 @@ pub enum Lang {
English, 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 /// Get the browser language
pub fn lang(headers: &HeaderMap) -> Lang { pub fn lang(headers: &HeaderMap) -> Lang {
headers headers

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
</head> </head>

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link <link

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link <link

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link rel="stylesheet" href="/css/contact.css" /> <link rel="stylesheet" href="/css/contact.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link rel="stylesheet" href="/css/contrib.css" /> <link rel="stylesheet" href="/css/contrib.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link rel="stylesheet" href="/css/cours.css" /> <link rel="stylesheet" href="/css/cours.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link rel="stylesheet" href="/css/index.css" /> <link rel="stylesheet" href="/css/index.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr"> <html lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
{{>head.html}} {{>head.html}}
<link rel="stylesheet" href="/css/portfolio.css" /> <link rel="stylesheet" href="/css/portfolio.css" />

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html class="index" lang="fr"> <html class="index" lang="{{lang}}">
<head dir="ltr"> <head dir="ltr">
<title>{{page_title}}{{#page_title}} - {{/page_title}}{{app_name}}</title> <title>{{page_title}}{{#page_title}} - {{/page_title}}{{app_name}}</title>
<meta charset="UTF-8" /> <meta charset="UTF-8" />