translate login and not_found to browser language
This commit is contained in:
parent
c6c56a5ea2
commit
0b9afb1fa3
@ -4,6 +4,9 @@ invite-user = Benutzer einladen
|
|||||||
list-users = Liste der Benutzer
|
list-users = Liste der Benutzer
|
||||||
welcome-user = Herzlich willkommen {$username}
|
welcome-user = Herzlich willkommen {$username}
|
||||||
logout = Abmelden
|
logout = Abmelden
|
||||||
|
login = Login
|
||||||
|
|
||||||
|
not-found = Dieser Link existiert nicht, oder wurde gelöscht.
|
||||||
|
|
||||||
edit-link-headline = Zu editierender Link: {$linktitle}
|
edit-link-headline = Zu editierender Link: {$linktitle}
|
||||||
edit-link = Link Editieren
|
edit-link = Link Editieren
|
||||||
|
@ -2,8 +2,11 @@ list-links = List of existing links
|
|||||||
add-link = Add a new link
|
add-link = Add a new link
|
||||||
invite-user = Invite a new user
|
invite-user = Invite a new user
|
||||||
list-users = List of existing users
|
list-users = List of existing users
|
||||||
welcome-user = Wellcome {$username}
|
welcome-user = Welcome {$username}
|
||||||
logout = Logout
|
logout = Logout
|
||||||
|
login = Login
|
||||||
|
|
||||||
|
not-found = This Link has not been found or has been deleted
|
||||||
|
|
||||||
edit-link-headline = Edit link: {$linktitle}
|
edit-link-headline = Edit link: {$linktitle}
|
||||||
edit-link = Edit link
|
edit-link = Edit link
|
||||||
|
44
src/views.rs
44
src/views.rs
@ -3,9 +3,14 @@ use std::time::SystemTime;
|
|||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::header::{CacheControl, CacheDirective, ContentType, Expires},
|
http::header::{CacheControl, CacheDirective, ContentType, Expires},
|
||||||
web, HttpResponse,
|
web, HttpRequest, HttpResponse,
|
||||||
};
|
};
|
||||||
use argonautica::Verifier;
|
use argonautica::Verifier;
|
||||||
|
use fluent_langneg::{
|
||||||
|
convert_vec_str_to_langids_lossy, negotiate_languages, parse_accepted_languages,
|
||||||
|
NegotiationStrategy,
|
||||||
|
};
|
||||||
|
use fluent_templates::LanguageIdentifier;
|
||||||
use image::{DynamicImage, ImageOutputFormat, Luma};
|
use image::{DynamicImage, ImageOutputFormat, Luma};
|
||||||
use qrcode::{render::svg, QrCode};
|
use qrcode::{render::svg, QrCode};
|
||||||
use tera::{Context, Tera};
|
use tera::{Context, Tera};
|
||||||
@ -27,6 +32,35 @@ fn redirect_builder(target: &str) -> HttpResponse {
|
|||||||
.body(format!("Redirect to {}", target))
|
.body(format!("Redirect to {}", target))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn detect_language(request: &HttpRequest) -> Result<String, ServerError> {
|
||||||
|
let requested = parse_accepted_languages(
|
||||||
|
request
|
||||||
|
.headers()
|
||||||
|
.get(actix_web::http::header::ACCEPT_LANGUAGE)
|
||||||
|
.ok_or_else(|| ServerError::User("Failed to get Accept_Language".to_owned()))?
|
||||||
|
.to_str()
|
||||||
|
.map_err(|_| {
|
||||||
|
ServerError::User("Failed to convert Accept_language to str".to_owned())
|
||||||
|
})?,
|
||||||
|
);
|
||||||
|
let available = convert_vec_str_to_langids_lossy(&["de", "en"]);
|
||||||
|
let default: LanguageIdentifier = "en"
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| ServerError::User("Failed to parse a langid.".to_owned()))?;
|
||||||
|
|
||||||
|
let supported = negotiate_languages(
|
||||||
|
&requested,
|
||||||
|
&available,
|
||||||
|
Some(&default),
|
||||||
|
NegotiationStrategy::Filtering,
|
||||||
|
);
|
||||||
|
let languagecode = supported
|
||||||
|
.get(0)
|
||||||
|
.map_or("en".to_string(), std::string::ToString::to_string);
|
||||||
|
println!("Detected the language: {}", &languagecode);
|
||||||
|
Ok(languagecode)
|
||||||
|
}
|
||||||
|
|
||||||
/// Show the list of all available links if a user is authenticated
|
/// Show the list of all available links if a user is authenticated
|
||||||
pub(crate) async fn index(
|
pub(crate) async fn index(
|
||||||
tera: web::Data<Tera>,
|
tera: web::Data<Tera>,
|
||||||
@ -251,9 +285,14 @@ pub(crate) async fn toggle_admin(
|
|||||||
pub(crate) async fn login(
|
pub(crate) async fn login(
|
||||||
tera: web::Data<Tera>,
|
tera: web::Data<Tera>,
|
||||||
id: Identity,
|
id: Identity,
|
||||||
|
config: web::Data<crate::ServerConfig>,
|
||||||
|
req: HttpRequest,
|
||||||
) -> Result<HttpResponse, ServerError> {
|
) -> Result<HttpResponse, ServerError> {
|
||||||
|
let language_code = detect_language(&req)?;
|
||||||
|
slog_info!(config.log, "Detected languagecode: {}", &language_code);
|
||||||
let mut data = Context::new();
|
let mut data = Context::new();
|
||||||
data.insert("title", "Login");
|
data.insert("title", "Login");
|
||||||
|
data.insert("language", &language_code);
|
||||||
|
|
||||||
if let Some(_id) = id.identity() {
|
if let Some(_id) = id.identity() {
|
||||||
return Ok(redirect_builder("/admin/index/"));
|
return Ok(redirect_builder("/admin/index/"));
|
||||||
@ -304,6 +343,7 @@ pub(crate) async fn redirect(
|
|||||||
tera: web::Data<Tera>,
|
tera: web::Data<Tera>,
|
||||||
config: web::Data<crate::ServerConfig>,
|
config: web::Data<crate::ServerConfig>,
|
||||||
data: web::Path<String>,
|
data: web::Path<String>,
|
||||||
|
req: HttpRequest,
|
||||||
) -> Result<HttpResponse, ServerError> {
|
) -> Result<HttpResponse, ServerError> {
|
||||||
slog_info!(config.log, "Redirecting to {:?}", data);
|
slog_info!(config.log, "Redirecting to {:?}", data);
|
||||||
let link = queries::get_link_simple(&data.0, &config).await;
|
let link = queries::get_link_simple(&data.0, &config).await;
|
||||||
@ -323,6 +363,8 @@ pub(crate) async fn redirect(
|
|||||||
);
|
);
|
||||||
let mut data = Context::new();
|
let mut data = Context::new();
|
||||||
data.insert("title", "Wurde gel\u{f6}scht");
|
data.insert("title", "Wurde gel\u{f6}scht");
|
||||||
|
let language = detect_language(&req)?;
|
||||||
|
data.insert("language", &language);
|
||||||
let rendered = tera.render("not_found.html", &data)?;
|
let rendered = tera.render("not_found.html", &data)?;
|
||||||
Ok(HttpResponse::NotFound().body(rendered))
|
Ok(HttpResponse::NotFound().body(rendered))
|
||||||
}
|
}
|
||||||
|
@ -4,14 +4,16 @@
|
|||||||
<div class="center">
|
<div class="center">
|
||||||
<form action="" method="POST">
|
<form action="" method="POST">
|
||||||
<div>
|
<div>
|
||||||
<label for="username">Benutzername:</label>
|
<label for="username">{{ fluent(key="username", lang=language)
|
||||||
|
}}:</label>
|
||||||
<input type="text" name="username">
|
<input type="text" name="username">
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="password">Passwort:</label>
|
<label for="password">{{ fluent(key="password", lang=language)
|
||||||
|
}}:</label>
|
||||||
<input type="password" name="password">
|
<input type="password" name="password">
|
||||||
</div>
|
</div>
|
||||||
<input type="submit" value="Login">
|
<input type="submit" value='{{ fluent(key="login", lang=language) }}'>
|
||||||
</form>
|
</form>
|
||||||
<h2> </h2>
|
<h2> </h2>
|
||||||
</div>
|
</div>
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="center">
|
<div class="center">
|
||||||
<h3>This Link has not been found or has been deleted</h3>
|
<h3>{{ fluent(key="not-found", lang=language) }}</h3>
|
||||||
<h2> </h2>
|
<h2> </h2>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
Loading…
Reference in New Issue
Block a user