Files
Oxicloud/src/application/dtos/i18n_dto.rs
T

92 lines
2.5 KiB
Rust
Raw Normal View History

2025-03-17 21:28:08 +01:00
use crate::domain::services::i18n_service::Locale;
2026-02-14 01:29:34 +01:00
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
2025-03-17 21:28:08 +01:00
/// DTO for locale information
#[derive(Debug, Serialize, Deserialize, ToSchema)]
2025-03-17 21:28:08 +01:00
pub struct LocaleDto {
/// Locale code (e.g., "en", "es")
pub code: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Locale name in its own language (e.g., "English", "Español")
pub name: String,
}
impl From<Locale> for LocaleDto {
fn from(locale: Locale) -> Self {
2026-06-03 13:18:05 +02:00
Self::from(&locale)
}
}
impl From<&Locale> for LocaleDto {
fn from(locale: &Locale) -> Self {
let code = locale.as_str().to_string();
let name = display_name_for(&code)
.map(str::to_string)
.unwrap_or_else(|| code.clone());
Self { code, name }
}
}
2026-02-14 01:29:34 +01:00
2026-06-03 13:18:05 +02:00
/// Endonym lookup for the locales shipped under `static/locales/`. New
/// locales added in PR-A's `LocaleRegistry::discover` should be added
/// here too; an unknown code falls back to itself, which is safe but
/// looks rough in a language switcher.
fn display_name_for(code: &str) -> Option<&'static str> {
match code {
"en" => Some("English"),
"es" => Some("Español"),
"fr" => Some("Français"),
"de" => Some("Deutsch"),
"pt" => Some("Português"),
"it" => Some("Italiano"),
"nl" => Some("Nederlands"),
"pl" => Some("Polski"),
"ru" => Some("Русский"),
"ja" => Some("日本語"),
"ko" => Some("한국어"),
"zh" => Some("中文"),
"zh-tw" => Some("繁體中文"),
"ar" => Some("العربية"),
"fa" => Some("فارسی"),
"hi" => Some("हिन्दी"),
_ => None,
2025-03-17 21:28:08 +01:00
}
}
/// DTO for translation request
#[derive(Debug, Deserialize, ToSchema)]
2025-03-17 21:28:08 +01:00
pub struct TranslationRequestDto {
/// The translation key
pub key: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// The locale code (optional, defaults to "en")
pub locale: Option<String>,
}
/// DTO for translation response
#[derive(Debug, Serialize, ToSchema)]
2025-03-17 21:28:08 +01:00
pub struct TranslationResponseDto {
/// The translation key
pub key: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// The locale code used for translation
pub locale: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// The translated text
pub text: String,
}
/// DTO for translation error
#[derive(Debug, Serialize, ToSchema)]
2025-03-17 21:28:08 +01:00
pub struct TranslationErrorDto {
/// The translation key that was not found
pub key: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// The locale code used for translation
pub locale: String,
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// The error message
pub error: String,
2026-02-14 01:29:34 +01:00
}