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(),
},
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,
)
}

View file

@ -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,
)
}

View file

@ -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),
)
}

View file

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

View file

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

View file

@ -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),
)
}

View file

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

View file

@ -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),
)
}

View file

@ -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,
)
}

View file

@ -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<T> {
page_kw: Option<String>,
/// Author's name
page_author: Option<String>,
/// Language used
lang: String,
/// Data needed to render the page
data: T,
}
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 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,
})
}

View file

@ -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<File> {
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<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
@ -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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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