Monotone Arbeit nervt!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.9 KiB

2 years ago
  1. use actix_web::HttpRequest;
  2. use askama::Template;
  3. use crate::config::Config;
  4. #[derive(Template)]
  5. #[template(path = "index.html")]
  6. pub struct TplIndex<'a> {
  7. pub lang: &'a str,
  8. pub csrf_token: &'a str,
  9. pub sncf_admin_token: Option<String>,
  10. }
  11. #[derive(Template)]
  12. #[template(path = "error.html")]
  13. pub struct TplError<'a> {
  14. pub lang: &'a str,
  15. pub error_msg: &'a str,
  16. }
  17. #[derive(Template)]
  18. #[template(path = "link.html")]
  19. pub struct TplLink<'a> {
  20. pub lang: &'a str,
  21. pub admin_token: &'a str,
  22. pub csrf_token: &'a str,
  23. pub config: &'a Config,
  24. }
  25. pub fn get_lang(req: &HttpRequest) -> String {
  26. // getting language from client header
  27. // taking the two first characters of the Accept-Language header,
  28. // in lowercase, then parsing it.
  29. // if it fails, returns "en"
  30. if let Some(la) = req.uri().query() {
  31. return la[5..].to_string();
  32. } else {
  33. if let Some(l) = req.headers().get("Accept-Language") {
  34. if let Ok(s) = l.to_str() {
  35. return s.to_lowercase()[..2].to_string();
  36. }
  37. }
  38. }
  39. String::from("en")
  40. }
  41. mod filters {
  42. use crate::config::LOC;
  43. pub fn tr(key: &str, lang: &str) -> askama::Result<String> {
  44. let translation = LOC.get(key).ok_or_else(|| {
  45. eprintln!("tr filter: couldn't find the key {}", key);
  46. askama::Error::from(std::fmt::Error)
  47. })?;
  48. Ok(String::from(
  49. translation
  50. .get(lang)
  51. .unwrap_or(translation.get("en").ok_or_else(|| {
  52. eprintln!("tr filter: couldn't find the lang {} in key {}", lang, key);
  53. askama::Error::from(std::fmt::Error)
  54. })?)
  55. .as_str()
  56. .ok_or_else(|| {
  57. eprintln!("tr filter: lang {} in key {} is not str", lang, key);
  58. askama::Error::from(std::fmt::Error)
  59. })?,
  60. ))
  61. }
  62. }