i18n on the web - deepseek translations
This commit is contained in:
@@ -5,7 +5,11 @@ use axum::{
|
||||
response::{Html, IntoResponse, Redirect, Response},
|
||||
};
|
||||
|
||||
use crate::{AppState, services::authenticated_request};
|
||||
use crate::{
|
||||
AppState,
|
||||
{i18n::Locale, tr},
|
||||
services::authenticated_request,
|
||||
};
|
||||
|
||||
use super::{
|
||||
loader::{LoadError, load_page},
|
||||
@@ -17,7 +21,7 @@ pub(crate) async fn new_logic_page(
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
) -> Response {
|
||||
render_loaded(load_page(state, &headers, CreateLogicForm::default(), None).await)
|
||||
render_loaded(&headers, load_page(state, &headers, CreateLogicForm::default(), None).await)
|
||||
}
|
||||
|
||||
pub(crate) async fn create_logic(
|
||||
@@ -27,7 +31,7 @@ pub(crate) async fn create_logic(
|
||||
) -> Response {
|
||||
let submitted = form.clone();
|
||||
if let Err(error) = load_page(state.clone(), &headers, submitted.clone(), None).await {
|
||||
return render_loaded(Err(error));
|
||||
return render_loaded(&headers, Err(error));
|
||||
}
|
||||
if headers
|
||||
.get("sec-fetch-site")
|
||||
@@ -35,10 +39,13 @@ pub(crate) async fn create_logic(
|
||||
{
|
||||
return (StatusCode::FORBIDDEN, "Cross-site form submission rejected").into_response();
|
||||
}
|
||||
let request = match form.into_request() {
|
||||
let request = match form.into_request(Locale::from_headers(&headers)) {
|
||||
Ok(request) => request,
|
||||
Err(message) => {
|
||||
return render_loaded(load_page(state, &headers, submitted, Some(message)).await);
|
||||
return render_loaded(
|
||||
&headers,
|
||||
load_page(state, &headers, submitted, Some(message)).await,
|
||||
);
|
||||
}
|
||||
};
|
||||
let mut scripts = state.scripts;
|
||||
@@ -48,30 +55,43 @@ pub(crate) async fn create_logic(
|
||||
};
|
||||
match scripts.post_table_script(request).await {
|
||||
Ok(response) => Html(ui::render_success(
|
||||
Locale::from_headers(&headers),
|
||||
response.get_ref().id,
|
||||
&response.get_ref().warnings,
|
||||
))
|
||||
.into_response(),
|
||||
Err(error) => (
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
Html(ui::render_submission_error(error.message())),
|
||||
Html(ui::render_submission_error(
|
||||
Locale::from_headers(&headers),
|
||||
error.message(),
|
||||
)),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
fn render_loaded(result: Result<super::state::AddLogicPageState, LoadError>) -> Response {
|
||||
fn render_loaded(headers: &HeaderMap, result: Result<super::state::AddLogicPageState, LoadError>) -> Response {
|
||||
match result {
|
||||
Ok(page) => Html(ui::render_page(&page)).into_response(),
|
||||
Err(LoadError::Unauthenticated) => Redirect::to("/login").into_response(),
|
||||
Err(LoadError::Forbidden) => (
|
||||
StatusCode::FORBIDDEN,
|
||||
Html(ui::render_submission_error("Script-management permission is required.")),
|
||||
Html(ui::render_submission_error(
|
||||
Locale::from_headers(&headers),
|
||||
&tr!(
|
||||
Locale::from_headers(&headers),
|
||||
"add-logic-err-permission"
|
||||
),
|
||||
)),
|
||||
)
|
||||
.into_response(),
|
||||
Err(LoadError::Backend(message)) => (
|
||||
StatusCode::BAD_GATEWAY,
|
||||
Html(ui::render_submission_error(&message)),
|
||||
Html(ui::render_submission_error(
|
||||
Locale::from_headers(&headers),
|
||||
&message,
|
||||
)),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::definitions::table_script::PostTableScriptRequest;
|
||||
use crate::{i18n::Locale, tr};
|
||||
|
||||
#[derive(Clone, Debug, Default, serde::Deserialize)]
|
||||
pub(crate) struct CreateLogicForm {
|
||||
@@ -28,20 +29,20 @@ pub(crate) struct TableOption {
|
||||
}
|
||||
|
||||
impl CreateLogicForm {
|
||||
pub(crate) fn into_request(self) -> Result<PostTableScriptRequest, String> {
|
||||
pub(crate) fn into_request(self, locale: Locale) -> Result<PostTableScriptRequest, String> {
|
||||
if self.table_definition_id <= 0 {
|
||||
return Err("Select a target table.".to_string());
|
||||
return Err(tr!(locale, "add-logic-err-select-table"));
|
||||
}
|
||||
let target_column = self.target_column.trim().to_string();
|
||||
if target_column.is_empty() {
|
||||
return Err("Enter the target column.".to_string());
|
||||
return Err(tr!(locale, "add-logic-err-target-column"));
|
||||
}
|
||||
let script = self.script.trim().to_string();
|
||||
if script.is_empty() {
|
||||
return Err("Enter a Steel expression.".to_string());
|
||||
return Err(tr!(locale, "add-logic-err-script"));
|
||||
}
|
||||
if !script.starts_with('(') {
|
||||
return Err("The Steel expression must start with `(`.".to_string());
|
||||
return Err(tr!(locale, "add-logic-err-script-paren"));
|
||||
}
|
||||
Ok(PostTableScriptRequest {
|
||||
table_definition_id: self.table_definition_id,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use askama::Template;
|
||||
|
||||
use crate::{i18n::Locale, tr};
|
||||
use crate::ui::{Alert, Nav, render};
|
||||
|
||||
use super::state::AddLogicPageState;
|
||||
@@ -20,15 +21,28 @@ pub(crate) fn render_page(page: &AddLogicPageState) -> String {
|
||||
}
|
||||
|
||||
/// POST /admin/logic — the #submission-status swaps.
|
||||
pub(crate) fn render_submission_error(message: &str) -> String {
|
||||
render(&Alert::error("Could not save the logic", message))
|
||||
pub(crate) fn render_submission_error(locale: Locale, message: &str) -> String {
|
||||
render(&Alert::error(
|
||||
locale,
|
||||
&tr!(locale, "add-logic-error-title"),
|
||||
message,
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn render_success(id: i64, warnings: &str) -> String {
|
||||
pub(crate) fn render_success(locale: Locale, id: i64, warnings: &str) -> String {
|
||||
let message = if warnings.trim().is_empty() {
|
||||
format!("Script #{id} was saved.")
|
||||
tr!(locale, "add-logic-saved", "id" => id.to_string())
|
||||
} else {
|
||||
format!("Script #{id} was saved. Warnings: {warnings}")
|
||||
tr!(
|
||||
locale,
|
||||
"add-logic-saved-warnings",
|
||||
"id" => id.to_string(),
|
||||
"warnings" => warnings.to_string(),
|
||||
)
|
||||
};
|
||||
render(&Alert::success("Logic saved", &message))
|
||||
render(&Alert::success(
|
||||
locale,
|
||||
&tr!(locale, "add-logic-success-title"),
|
||||
&message,
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user