Automated translation of german into "Leichte Sprache"
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

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