Files
Oxicloud/src/domain/services/i18n_service.rs
T

69 lines
1.8 KiB
Rust
Raw Normal View History

2025-03-17 21:28:08 +01:00
use thiserror::Error;
/// Error types for i18n service operations
#[derive(Debug, Error)]
pub enum I18nError {
#[error("Translation key not found: {0}")]
KeyNotFound(String),
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
#[error("Invalid locale: {0}")]
InvalidLocale(String),
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
#[error("Error loading translations: {0}")]
LoadError(String),
}
/// Result type for i18n service operations
pub type I18nResult<T> = Result<T, I18nError>;
/// Supported locales
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
2025-03-17 21:28:08 +01:00
pub enum Locale {
#[default]
2025-03-17 21:28:08 +01:00
English,
Spanish,
French,
German,
Portuguese,
2025-03-17 21:28:08 +01:00
}
impl Locale {
/// Convert locale to code string
pub fn as_str(&self) -> &'static str {
match self {
Locale::English => "en",
Locale::Spanish => "es",
Locale::French => "fr",
Locale::German => "de",
Locale::Portuguese => "pt",
2025-03-17 21:28:08 +01:00
}
}
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Create from locale code string
pub fn from_code(code: &str) -> Option<Self> {
2025-03-17 21:28:08 +01:00
match code.to_lowercase().as_str() {
"en" => Some(Locale::English),
"es" => Some(Locale::Spanish),
"fr" => Some(Locale::French),
"de" => Some(Locale::German),
"pt" => Some(Locale::Portuguese),
2025-03-17 21:28:08 +01:00
_ => None,
}
}
}
/// Interface for i18n service (primary port)
pub trait I18nService: Send + Sync + 'static {
/// Get a translation for a key and locale
async fn translate(&self, key: &str, locale: Locale) -> I18nResult<String>;
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Load translations for a locale
async fn load_translations(&self, locale: Locale) -> I18nResult<()>;
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Get available locales
async fn available_locales(&self) -> Vec<Locale>;
2026-02-14 01:29:34 +01:00
2025-03-17 21:28:08 +01:00
/// Check if a locale is supported
async fn is_supported(&self, locale: Locale) -> bool;
2026-02-14 01:29:34 +01:00
}