diff --git a/.gitignore b/.gitignore index ea8c4bf..bcd5559 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target + +.DS_Store diff --git a/BookForge.toml b/BookForge.toml index 3c2268e..0ac7cc8 100644 --- a/BookForge.toml +++ b/BookForge.toml @@ -1,4 +1,6 @@ database_path = "" +locale = "fr" +base_url = "" [listener] port = 8000 diff --git a/Cargo.lock b/Cargo.lock index 2ec2f93..ef09c66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,6 +99,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "arc-swap" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d03449bb8ca2cc2ef70869af31463d1ae5ccc8fa3e334b307203fbf815207e" +dependencies = [ + "rustversion", +] + [[package]] name = "arrayvec" version = "0.7.6" @@ -287,6 +296,12 @@ dependencies = [ "tracing", ] +[[package]] +name = "base62" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1adf9755786e27479693dedd3271691a92b5e242ab139cacb9fb8e7fb5381111" + [[package]] name = "base64" version = "0.22.1" @@ -322,6 +337,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.10.0" @@ -364,6 +385,7 @@ dependencies = [ "dirs", "log", "pretty_env_logger", + "rust-i18n", "sea-orm", "sea-orm-migration", "serde", @@ -371,7 +393,7 @@ dependencies = [ "snafu", "static-serve", "tokio", - "toml", + "toml 0.9.11+spec-1.1.0", "xdg", ] @@ -398,6 +420,16 @@ dependencies = [ "syn 2.0.114", ] +[[package]] +name = "bstr" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "bumpalo" version = "3.19.1" @@ -583,6 +615,25 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-queue" version = "0.3.12" @@ -1018,6 +1069,30 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" +[[package]] +name = "globset" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52dfc19153a48bde0cbd630453615c8151bce3a5adfac7a0aebfbf0a1e1f57e3" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "globwalk" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" +dependencies = [ + "bitflags 1.3.2", + "ignore", + "walkdir", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -1324,6 +1399,22 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "ignore" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3d782a365a015e0f5c04902246139249abf769125006fbe7649e2ee88169b4a" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata", + "same-file", + "walkdir", + "winapi-util", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -1384,6 +1475,15 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.14.0" @@ -1446,7 +1546,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616" dependencies = [ - "bitflags", + "bitflags 2.10.0", "libc", "redox_syscall 0.7.0", ] @@ -1551,6 +1651,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "normpath" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf23ab2b905654b4cb177e30b629937b3868311d4e1cba859f899c041046e69b" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "num-bigint" version = "0.4.6" @@ -1806,7 +1915,7 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983" dependencies = [ - "toml_edit", + "toml_edit 0.23.10+spec-1.0.0", ] [[package]] @@ -1930,7 +2039,7 @@ version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ - "bitflags", + "bitflags 2.10.0", ] [[package]] @@ -1939,7 +2048,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f3fe0889e69e2ae9e41f4d6c4c0181701d00e4697b356fb1f74173a5e0ee27" dependencies = [ - "bitflags", + "bitflags 2.10.0", ] [[package]] @@ -2060,6 +2169,60 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rust-i18n" +version = "3.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda2551fdfaf6cc5ee283adc15e157047b92ae6535cf80f6d4962d05717dc332" +dependencies = [ + "globwalk", + "once_cell", + "regex", + "rust-i18n-macro", + "rust-i18n-support", + "smallvec", +] + +[[package]] +name = "rust-i18n-macro" +version = "3.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22baf7d7f56656d23ebe24f6bb57a5d40d2bce2a5f1c503e692b5b2fa450f965" +dependencies = [ + "glob", + "once_cell", + "proc-macro2", + "quote", + "rust-i18n-support", + "serde", + "serde_json", + "serde_yaml", + "syn 2.0.114", +] + +[[package]] +name = "rust-i18n-support" +version = "3.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940ed4f52bba4c0152056d771e563b7133ad9607d4384af016a134b58d758f19" +dependencies = [ + "arc-swap", + "base62", + "globwalk", + "itertools 0.11.0", + "lazy_static", + "normpath", + "once_cell", + "proc-macro2", + "regex", + "serde", + "serde_json", + "serde_yaml", + "siphasher", + "toml 0.8.23", + "triomphe", +] + [[package]] name = "rust_decimal" version = "1.40.0" @@ -2103,6 +2266,15 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schemars" version = "0.9.0" @@ -2158,7 +2330,7 @@ dependencies = [ "chrono", "derive_more", "futures-util", - "itertools", + "itertools 0.14.0", "log", "ouroboros", "pgvector", @@ -2362,6 +2534,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + [[package]] name = "serde_spanned" version = "1.0.4" @@ -2414,6 +2595,19 @@ dependencies = [ "syn 2.0.114", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap 2.13.0", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha1" version = "0.10.6" @@ -2473,6 +2667,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" +[[package]] +name = "siphasher" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e" + [[package]] name = "slab" version = "0.4.11" @@ -2635,7 +2835,7 @@ checksum = "aa003f0038df784eb8fecbbac13affe3da23b45194bd57dba231c8f48199c526" dependencies = [ "atoi", "base64", - "bitflags", + "bitflags 2.10.0", "byteorder", "bytes", "chrono", @@ -2681,7 +2881,7 @@ checksum = "db58fcd5a53cf07c184b154801ff91347e4c30d17a3562a635ff028ad5deda46" dependencies = [ "atoi", "base64", - "bitflags", + "bitflags 2.10.0", "byteorder", "chrono", "crc", @@ -2985,6 +3185,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", + "toml_edit 0.22.27", +] + [[package]] name = "toml" version = "0.9.11+spec-1.1.0" @@ -2993,13 +3205,22 @@ checksum = "f3afc9a848309fe1aaffaed6e1546a7a14de1f935dc9d89d32afd9a44bab7c46" dependencies = [ "indexmap 2.13.0", "serde_core", - "serde_spanned", - "toml_datetime", + "serde_spanned 1.0.4", + "toml_datetime 0.7.5+spec-1.1.0", "toml_parser", "toml_writer", "winnow", ] +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + [[package]] name = "toml_datetime" version = "0.7.5+spec-1.1.0" @@ -3009,6 +3230,20 @@ dependencies = [ "serde_core", ] +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap 2.13.0", + "serde", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", + "toml_write", + "winnow", +] + [[package]] name = "toml_edit" version = "0.23.10+spec-1.0.0" @@ -3016,7 +3251,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84c8b9f757e028cee9fa244aea147aab2a9ec09d5325a9b01e0a49730c2b5269" dependencies = [ "indexmap 2.13.0", - "toml_datetime", + "toml_datetime 0.7.5+spec-1.1.0", "toml_parser", "winnow", ] @@ -3030,6 +3265,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "toml_writer" version = "1.0.6+spec-1.1.0" @@ -3111,6 +3352,17 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "triomphe" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd69c5aa8f924c7519d6372789a74eac5b94fb0f8fcf0d4a97eb0bfc3e785f39" +dependencies = [ + "arc-swap", + "serde", + "stable_deref_trait", +] + [[package]] name = "typenum" version = "1.19.0" @@ -3156,6 +3408,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "url" version = "2.5.8" @@ -3203,6 +3461,16 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index ba47a7d..83d8600 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ pretty_env_logger = "0.5.0" log = "0.4.29" serde_with = "3.16.1" csv = "1.4.0" +rust-i18n = "3.1.5" diff --git a/assets/images/favicon.png b/assets/images/favicon.png new file mode 100644 index 0000000..5dbbdbe Binary files /dev/null and b/assets/images/favicon.png differ diff --git a/locales/en.yml b/locales/en.yml new file mode 100644 index 0000000..5e258e0 --- /dev/null +++ b/locales/en.yml @@ -0,0 +1,92 @@ +_version: 1 +name: "BookForge" + +common: + download: Download + create: Create + edit: Edit + delete: Delete + next: Next + previous: Previous + actions: Actions + search: Search + reset: Reset + show: Show + no_result: No results + close: Close + confirmation: Confirmation + are_you_sure: Are you sure? + +nav: + toggle: Toggle navigation + books: Books + users: Users + +theme: + light: Light + dark: Dark + +user: + attributes: + name: Name + owner_books: Owned books + borrowed_books: Borrowed books + + index: + title_tag: Users list | BookForge + title: All Users + button: Add a user + + edit: + title_tag: Edit user | BookForge + title: Edit + button: Edit user + + new: + title_tag: New user | BookForge + title: New user + button: Create user + +book: + attributes: + title: Title + authors: Author(s) + description: Description + owner: Owner + current_holder: Current holder + comment: Comment + + index: + title_tag: Books list | BookForge + title: All Books + + new: + title_tag: New book | BookForge + title: New Book + button: Create book + button_short: Add book + + edit: + title_tag: Edit book | BookForge + title: Edit book + button: Edit book + + show: + title_tag: Details | BookForge + book_details: Book details + user_details: User details + more_informations: More information + +footer: + message: Made with love & Fuck fascists! + +error: + error_404: + title_tag: Error 404 | BookForge + title: Oops! This page does not exist + subtitle: 404 NOT FOUND + button: Back to home + + generic: + title_tag: Error | BookForge + title: Oops! An error occurred diff --git a/locales/fr.yml b/locales/fr.yml new file mode 100644 index 0000000..22b0675 --- /dev/null +++ b/locales/fr.yml @@ -0,0 +1,77 @@ +_version: 1 +name: "BookForge" +common: + download: Télécharger + create: Créer + edit: Modifier + delete: Supprimer + next: Suivant + previous: Précédent + actions: Actions + search: Filtrer + reset: Réini. + show: Voir + no_result: Aucun résultat + close: Fermer + confirmation: Confirmation + are_you_sure: Êtes-vous sûr ? +nav: + toggle: Basculer la navigation + books: Livres + users: Utilisateurs +theme: + light: Light + dark: Dark +user: + attributes: + name: Nom + owner_books: Livres possédés + borrowed_books: Livres empruntés + index: + title_tag: Liste des utilisateur.ice.s | BookForge + title: Tous les utilisateur.ice.s + button: Ajouter un.e utilisateur.ice + edit: + title_tag: Modifier l'utilisateur.ice | BookForge + title: Modifier + button: Modifier l'utilisateur.ice + new: + title_tag: Nouvel utilisateur.ice | BookForge + title: Nouvel utilisateur.ice + button: Créer l'utilisateur.ice +book: + attributes: + title: Titre + authors: Auteur.ice.(s) + description: Description + owner: Propriétaire + current_holder: Détenteur.ice actuel.le + comment: Commentaire + index: + title_tag: Liste des livres | BookForge + title: Tous les livres + new: + title_tag: Nouveau livre | BookForge + title: Nouveau livre + button: Créer le livre + button_short: Ajouter un livre + edit: + title_tag: Modifier le livre | BookForge + title: Modifier le livre + button: Modifier le livre + show: + title_tag: Details | BookForge + book_details: Détails du livre + user_details: Détails de l'utilisateur.ice + more_informations: Plus d'informations +footer: + message: Fait avec amour & Nique les fachos ! +error: + error_404: + title_tag: Erreur 404 | BookForge + title: Oups ! Cette page n'existe pas + subtitle: 404 NOT FOUND + button: Retour à l'accueil + generic: + title_tag: Erreur | BookForge + title: Oups ! Une erreur s'est produite diff --git a/src/lib.rs b/src/lib.rs index c024ef5..6c3de23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,10 +2,12 @@ use askama::Template; use askama_web::WebTemplate; use axum::{ Router, + extract::State, routing::{get, post}, }; use static_serve::embed_assets; +use crate::routes::router::Router as InternalRouter; use crate::state::AppState; mod migrations; @@ -13,7 +15,13 @@ mod models; mod routes; pub mod state; +#[macro_use] +extern crate rust_i18n; + +i18n!("locales", fallback = "en", minify_key = true); + pub fn build_app(state: AppState) -> Router { + rust_i18n::set_locale(&state.config.locale); embed_assets!("assets", compress = true); Router::new() @@ -38,8 +46,14 @@ pub fn build_app(state: AppState) -> Router { #[derive(Template, WebTemplate)] #[template(path = "404.html")] -struct NotFoundTemplate {} - -pub async fn error_handler() -> impl axum::response::IntoResponse { - NotFoundTemplate {} +struct NotFoundTemplate { + pub router: InternalRouter, +} + +pub async fn error_handler(State(state): State) -> impl axum::response::IntoResponse { + NotFoundTemplate { + router: InternalRouter { + base_path: state.config.base_path, + }, + } } diff --git a/src/routes/book.rs b/src/routes/book.rs index fe9d75d..8d4fa03 100644 --- a/src/routes/book.rs +++ b/src/routes/book.rs @@ -13,7 +13,7 @@ use serde::Deserialize; use serde_with::{NoneAsEmptyString, serde_as}; use snafu::prelude::*; -use crate::{models::book::Model as BookModel, state::error::CSVSnafu}; +use crate::{models::book::Model as BookModel, routes::router::Router, state::error::CSVSnafu}; use crate::{models::user::Model as UserModel, state::error::IOSnafu}; use crate::{ @@ -55,6 +55,7 @@ struct BookIndexTemplate { current_page: u64, total_page: u64, base_query: String, + router: Router, } pub async fn index( @@ -73,7 +74,7 @@ pub async fn index( .context(UserSnafu)?; // Get all Book filtered with query - let books_paginate = BookOperator::new(state) + let books_paginate = BookOperator::new(state.clone()) .all_paginate(page, Some(query.clone())) .await .context(BookSnafu)?; @@ -127,6 +128,9 @@ pub async fn index( current_page: books_paginate.current_page, total_page: books_paginate.total_page, base_query, + router: Router { + base_path: state.config.base_path, + }, }) } @@ -136,6 +140,7 @@ struct ShowBookTemplate { book: BookModel, owner: UserModel, current_holder: Option, + router: Router, } pub async fn show( @@ -168,6 +173,9 @@ pub async fn show( book, owner, current_holder, + router: Router { + base_path: state.config.base_path, + }, }) } @@ -200,14 +208,23 @@ pub async fn create( #[template(path = "books/new.html")] struct NewBookTemplate { users: Vec, + router: Router, } pub async fn new( State(state): State, ) -> Result { - let users = UserOperator::new(state).all().await.context(UserSnafu)?; + let users = UserOperator::new(state.clone()) + .all() + .await + .context(UserSnafu)?; - Ok(NewBookTemplate { users }) + Ok(NewBookTemplate { + users, + router: Router { + base_path: state.config.base_path, + }, + }) } #[derive(Template, WebTemplate)] @@ -215,6 +232,7 @@ pub async fn new( struct EditBookTemplate { users: Vec, book: BookModel, + router: Router, } pub async fn edit( @@ -225,12 +243,18 @@ pub async fn edit( .all() .await .context(UserSnafu)?; - let book = BookOperator::new(state) + let book = BookOperator::new(state.clone()) .find_by_id(id) .await .context(BookSnafu)?; - Ok(EditBookTemplate { users, book }) + Ok(EditBookTemplate { + users, + book, + router: Router { + base_path: state.config.base_path, + }, + }) } pub async fn update( @@ -273,13 +297,13 @@ pub async fn download_csv( let mut wtr = Writer::from_writer(vec![]); wtr.write_record(&[ - "id", + "ID", "Title", "Author(s)", - "description", - "owner_id", - "current_holder_id", - "comment", + "Description", + "Owner", + "Current Holder", + "Comment", ]) .context(CSVSnafu)?; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 011ff79..2bbba56 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,2 +1,3 @@ pub mod book; +pub mod router; pub mod user; diff --git a/src/routes/router.rs b/src/routes/router.rs new file mode 100644 index 0000000..52bcd0b --- /dev/null +++ b/src/routes/router.rs @@ -0,0 +1,54 @@ +#[derive(Clone)] +pub struct Router { + pub base_path: String, +} + +impl Router { + pub fn assets(&self, path: &str) -> String { + if self.base_path.is_empty() || self.base_path == "/" { + format!("/{}", path) + } else { + format!("{}/{}", self.base_path.trim_end_matches('/'), path) + } + } + + pub fn root_path(&self) -> String { + format!("{}/", &self.base_path) + } + + // BOOKS ROUTES + + pub fn new_book_path(&self) -> String { + format!("{}/books/new", &self.base_path) + } + + pub fn create_book_path(&self) -> String { + format!("{}/books", &self.base_path) + } + + pub fn update_book_path(&self, id: &i32) -> String { + format!("{}/books/{}", &self.base_path, id) + } + + pub fn download_csv_book_path(&self) -> String { + format!("{}/books/download_csv", &self.base_path) + } + + // USERS + + pub fn index_user_path(&self) -> String { + format!("{}/users", &self.base_path) + } + + pub fn new_user_path(&self) -> String { + format!("{}/users/new", &self.base_path) + } + + pub fn create_user_path(&self) -> String { + format!("{}/users", &self.base_path) + } + + pub fn update_user_path(&self, id: &i32) -> String { + format!("{}/users/{}", &self.base_path, id) + } +} diff --git a/src/routes/user.rs b/src/routes/user.rs index 3c81570..04eed56 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -16,6 +16,7 @@ use crate::{ book::BookOperator, user::{self, UserOperator}, }, + routes::router::Router, state::{ AppState, error::{AppStateError, BookSnafu, UserSnafu}, @@ -25,8 +26,9 @@ use crate::{ #[derive(Template, WebTemplate)] #[template(path = "users/index.html")] struct UsersIndexTemplate { - user_with_books_number: Vec, + users_with_books_number: Vec, query: IndexQuery, + router: Router, } pub struct UserWithBookNumber { @@ -84,8 +86,11 @@ pub async fn index( } Ok(UsersIndexTemplate { - user_with_books_number: result, + users_with_books_number: result, query, + router: Router { + base_path: state.config.base_path, + }, }) } @@ -135,24 +140,36 @@ pub async fn delete( #[template(path = "users/edit.html")] struct EditTemplate { user: user::Model, + router: Router, } pub async fn edit( State(state): State, Path(id): Path, ) -> Result { - let user = UserOperator::new(state) + let user = UserOperator::new(state.clone()) .find_by_id(id) .await .context(UserSnafu)?; - Ok(EditTemplate { user }) + Ok(EditTemplate { + user, + router: Router { + base_path: state.config.base_path, + }, + }) } #[derive(Template, WebTemplate)] #[template(path = "users/new.html")] -struct NewTemplate {} - -pub async fn new() -> impl axum::response::IntoResponse { - NewTemplate {} +struct NewTemplate { + router: Router, +} + +pub async fn new(State(state): State) -> impl axum::response::IntoResponse { + NewTemplate { + router: Router { + base_path: state.config.base_path, + }, + } } diff --git a/src/state/config.rs b/src/state/config.rs index 4f81560..f137f8a 100644 --- a/src/state/config.rs +++ b/src/state/config.rs @@ -32,6 +32,8 @@ pub enum ConfigError { pub struct AppConfig { #[serde(default = "AppConfig::default_sqlite_path")] pub database_path: Utf8PathBuf, + pub locale: String, + pub base_path: String, pub listener: Listener, } @@ -39,6 +41,8 @@ impl Default for AppConfig { fn default() -> Self { AppConfig { database_path: Self::default_sqlite_path(), + base_path: Self::default_base_path(), + locale: Self::default_locale(), listener: Listener::default(), } } @@ -92,6 +96,14 @@ impl AppConfig { Self::config_path().join("BookForge.toml") } + fn default_locale() -> String { + "en".to_string() + } + + fn default_base_path() -> String { + "".to_string() + } + pub fn default_sqlite_path() -> Utf8PathBuf { Self::config_path().join("db.sqlite") } diff --git a/src/state/error.rs b/src/state/error.rs index 59d8232..6a031b6 100644 --- a/src/state/error.rs +++ b/src/state/error.rs @@ -6,6 +6,7 @@ use snafu::prelude::*; use crate::{ models::{book::BookError, user::UserError}, + routes::router::Router, state::config::ConfigError, }; @@ -47,6 +48,7 @@ pub enum AppStateError { #[template(path = "error.html")] struct ErrorTemplate { state: AppStateErrorContext, + router: Router, } struct AppStateErrorContext { @@ -66,6 +68,9 @@ impl IntoResponse for AppStateError { let error_context = AppStateErrorContext::from(self); ErrorTemplate { state: error_context, + router: Router { + base_path: "".to_string(), + }, } .into_response() } diff --git a/templates/404.html b/templates/404.html index 854fcc8..6f35ef6 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,8 +1,13 @@ {% extends "base.html" %} + +{% block title %} + {{ t!("error.error_404.title_tag") }} +{% endblock %} + {% block main %}
-

Oops ! This page does not exist

-

404 NOT FOUND

- Back to home +

{{ t!("error.error_404.title") }}

+

{{ t!("error.error_404.subtitle") }}

+ {{ t!("error.error_404.button") }}
{% endblock %} diff --git a/templates/base.html b/templates/base.html index 58b2ef0..bc0ed7e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,13 +2,14 @@ - {% block title %}Book Forge{% endblock %} + {% block title %}{{ t!("name") }}{% endblock %} - - + + + - - + + {% block extra_head %}{% endblock extra_head %} @@ -25,7 +26,7 @@ {% endblock %} - - + + diff --git a/templates/books/edit.html b/templates/books/edit.html index 6964b99..f80be6a 100644 --- a/templates/books/edit.html +++ b/templates/books/edit.html @@ -2,23 +2,27 @@ {% import "components/typography.html" as typography %} {% import "components/cards.html" as cards %} +{% block title %} + {{ t!("book.edit.title_tag") }} +{% endblock %} + {% block main %} - {{ typography::heading("Editer") }} + {{ typography::heading(t!("book.edit.title")) }} {% call cards::card() %} -
+
- +
- +
- + {% match book.current_holder_id %} @@ -52,7 +56,7 @@
- + {% match book.description %} {% when Some with (description) %} @@ -62,7 +66,7 @@
- + {% match book.comment %} {% when Some with (comment) %} @@ -72,7 +76,7 @@
- +
{% endcall %} diff --git a/templates/books/new.html b/templates/books/new.html index ddd66ce..30deb91 100644 --- a/templates/books/new.html +++ b/templates/books/new.html @@ -3,28 +3,32 @@ {% import "components/cards.html" as cards %} {% import "components/inputs.html" as form_helpers %} +{% block title %} + {{ t!("book.new.title_tag") }} +{% endblock %} + {% block main %} - {{ typography::heading("New book") }} + {{ typography::heading(t!("book.new.title")) }} {% call cards::card() %} -
- {{ form_helpers::input("title", "Name", is_required = true, placeholder = "Ex: La Petite Dernière") }} - {{ form_helpers::input("authors", "Author(s)", is_required = true, placeholder = "Ex: Fatima Daas") }} + + {{ form_helpers::input("title", t!("book.attributes.title"), is_required = true, placeholder = "Ex: La Petite Dernière") }} + {{ form_helpers::input("authors", t!("book.attributes.authors"), is_required = true, placeholder = "Ex: Fatima Daas") }} - {% call(option) form_helpers::select("owner_id", "Owner", users, is_required = true) %} + {% call(option) form_helpers::select("owner_id", t!("book.attributes.owner"), users, is_required = true) %} {% endcall %} - {% call(option) form_helpers::select("current_holder_id", "Current Holder", users, is_required = false) %} + {% call(option) form_helpers::select("current_holder_id", t!("book.attributes.current_holder"), users, is_required = false) %} {% endcall %} - - {{ form_helpers::textarea("description", "Description", rows = 5, is_required = false, placeholder = "Ex: Je m’appelle Fatima Daas. Je suis la mazoziya, la petite dernière. Celle à laquelle on ne s’est pas préparé. Française d’origine algérienne.") }} - {{ form_helpers::textarea("comment", "Comment", rows = 3, is_required = false, placeholder = "Ex: I recommend it, it's great!") }} + {{ form_helpers::textarea("description", t!("book.attributes.description"), rows = 5, is_required = false, placeholder = "Ex: Je m’appelle Fatima Daas. Je suis la mazoziya, la petite dernière. Celle à laquelle on ne s’est pas préparé. Française d’origine algérienne.") }} + + {{ form_helpers::textarea("comment", t!("book.attributes.comment"), rows = 3, is_required = false, placeholder = "Ex: I recommend it, it's great!") }}
- +
{% endcall %} diff --git a/templates/books/show.html b/templates/books/show.html index aec5bda..61042f2 100644 --- a/templates/books/show.html +++ b/templates/books/show.html @@ -4,39 +4,43 @@ {% import "components/fields.html" as fields %} {% import "components/cards.html" as cards %} +{% block title %} + {{ t!("book.show.title_tag") }} +{% endblock %} + {% block main %} {{ typography::book_heading(book.title, book, show = false) }} {% call cards::card() %} -
-
Book details
- {{ fields::field("Name", book.title) }} - {{ fields::field("Authors", book.authors) }} +
+
{{ t!("book.show.book_details") }}
+ {{ fields::field(t!("book.attributes.title"), book.title) }} + {{ fields::field(t!("book.attributes.authors"), book.authors) }} {% match book.description %} {% when Some with (description) %} - {{ fields::field("Description", description) }} + {{ fields::field(t!("book.attributes.description"), description) }} {% when None %} - {{ fields::field("Description", "-") }} + {{ fields::field(t!("book.attributes.description"), "-") }} {% endmatch %} -
User Details
- {{ fields::field("Owner", owner.name) }} +
{{ t!("book.show.user_details") }}
+ {{ fields::field(t!("book.attributes.owner"), owner.name) }} {% match current_holder %} {% when Some with (current_holder) %} - {{ fields::field("Current Holder", current_holder.name) }} + {{ fields::field(t!("book.attributes.current_holder"), current_holder.name) }} {% when None %} - {{ fields::field("Current Holder", "-") }} + {{ fields::field(t!("book.attributes.current_holder"), "-") }} {% endmatch %} -
More Informations
+
{{ t!("book.show.more_informations") }}
{% match book.comment %} {% when Some with (comment) %} - {{ fields::field("Comment", comment) }} + {{ fields::field(t!("book.attributes.comment"), comment) }} {% when None %} - {{ fields::field("Comment", "-") }} + {{ fields::field(t!("book.attributes.comment"), "-") }} {% endmatch %}
{% endcall %} diff --git a/templates/components/dropdown.html b/templates/components/dropdown.html index 37dc290..7b0e55d 100644 --- a/templates/components/dropdown.html +++ b/templates/components/dropdown.html @@ -14,15 +14,15 @@ {% macro crud_dropdown_button(book, label, sub_path, show = true) %} @@ -32,16 +32,16 @@ {% endmacro %} diff --git a/templates/error.html b/templates/error.html index e84c368..09e125b 100644 --- a/templates/error.html +++ b/templates/error.html @@ -1,12 +1,17 @@ {% extends "base.html" %} + +{% block title %} + {{ t!("error.generic.title_tag") }} +{% endblock %} + {% block main %}
-

Oops ! An Error occured

+

{{ t!("error.generic.title") }}

{% for error in state.errors %}

{{ error }}

{% endfor %}
- Back to home + {{ t!("error.error_404.button") }}
{% endblock %} diff --git a/templates/index.html b/templates/index.html index 17a7ddb..2701d7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,10 +3,14 @@ {% import "components/dropdown.html" as dropdown %} {% import "components/cards.html" as cards %} +{% block title %} + {{ t!("book.index.title_tag") }} +{% endblock %} + {% block main %} - {% call typography::heading("All books") %} - - Download (csv) + {% call typography::heading(t!("book.index.title")) %} + + {{ t!("common.download") }} (csv) {% endcall %} @@ -14,7 +18,7 @@
- + {% match query.title %} {% when Some with (value) %} @@ -24,7 +28,7 @@
- + {% match query.authors %} {% when Some with (value) %} @@ -35,7 +39,7 @@
- + @@ -79,73 +83,84 @@
- +
{% endcall %} {% call cards::card() %} - - - - - - - - - - - - - {% for book_user in books_with_user %} - - - - - - - - - {% endfor %} - -
#NameAuthor(s)OwnerCurrent HolderActions
{{ book_user.book.id }}{{ book_user.book.title }}{{ book_user.book.authors }}{{ book_user.owner.name }} - {% match book_user.current_holder %} - {% when Some with (current_holder) %} - {{ current_holder.name }} - {% when None %} - - - {% endmatch %} - - {{ dropdown::crud_dropdown_button(book_user.book, "Actions", "books") }} -
- - {% if total_page > 1 %} -
- + + {{ t!("book.new.button_short") }} +
+ {% else %} + + + + + + + + + + + + + {% for book_user in books_with_user %} + + + + + + + + + {% endfor %} + +
#{{ t!("book.attributes.title") }}{{ t!("book.attributes.authors") }}{{ t!("book.attributes.owner") }}{{ t!("book.attributes.current_holder") }}{{ t!("common.actions") }}
{{ book_user.book.id }}{{ book_user.book.title }}{{ book_user.book.authors }}{{ book_user.owner.name }} + {% match book_user.current_holder %} + {% when Some with (current_holder) %} + {{ current_holder.name }} + {% when None %} + - + {% endmatch %} + + {{ dropdown::crud_dropdown_button(book_user.book, t!("common.actions"), "books") }} +
+ + {% if total_page > 1 %} +
+ +
+ {% endif %} + {% endif %} {% endcall %} {% endblock %} diff --git a/templates/layout/footer.html b/templates/layout/footer.html index 3ddc19e..3f29c8b 100644 --- a/templates/layout/footer.html +++ b/templates/layout/footer.html @@ -1,5 +1,5 @@
-

Made with Love & Fuck a Fascist !

+

{{ t!("footer.message") }}

-
\ No newline at end of file +
diff --git a/templates/layout/header.html b/templates/layout/header.html index 6471bb9..114aa1f 100644 --- a/templates/layout/header.html +++ b/templates/layout/header.html @@ -1,31 +1,31 @@