Fixing translations

This commit is contained in:
Marco De Araujo 2025-12-22 14:41:08 -04:00
parent dbe196f388
commit 0dc51af51c
9 changed files with 41 additions and 136 deletions

View file

@ -3,7 +3,7 @@ use std::collections::HashMap;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use crate::i18n::t;
use crate::i18n::{t, t_with_args};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HeartbeatEntry {
@ -64,7 +64,13 @@ impl HeartbeatResponse {
for (key, percentage) in &self.uptime_list_raw {
let parts: Vec<&str> = key.split('_').collect();
if parts.len() != 2 {
return Err(anyhow::anyhow!("Formato invalido (colocar o t) {}", key));
let t_args: &mut HashMap<&str, std::string::String> =
&mut HashMap::<&str, String>::new();
t_args.insert("key", key.to_string());
return Err(anyhow::anyhow!(t_with_args(
"invalid-uptime-key-format",
t_args
)));
}
let monitor_id = parts[0]

View file

@ -2,4 +2,4 @@ pub mod loader;
pub mod translate;
pub use loader::init_locales;
pub use translate::t;
pub use translate::{t, t_with_args};

View file

@ -1,13 +1,27 @@
use crate::i18n::loader::LOCALES;
use fluent_templates::Loader;
use std::str::FromStr;
use fluent_templates::{Loader, fluent_bundle::FluentValue};
use once_cell::sync::Lazy;
use std::borrow::Cow;
use std::{collections::HashMap, str::FromStr};
use unic_langid::LanguageIdentifier;
fn get_sys_locale() -> LanguageIdentifier {
static SYSTEM_LOCALE: Lazy<LanguageIdentifier> = Lazy::new(|| {
let sys_lang = sys_locale::get_locale().unwrap_or_else(|| String::from("pt-BR"));
LanguageIdentifier::from_str(&sys_lang).expect("Invalid language")
}
});
pub fn t(key: &str) -> String {
LOCALES.lookup(&get_sys_locale(), key)
LOCALES.lookup(&*SYSTEM_LOCALE, key)
}
pub fn t_with_args(key: &str, args: &HashMap<&'static str, String>) -> String {
let mut map = HashMap::new();
let args_map: &HashMap<Cow<'static, _>, FluentValue<'_>>;
args_map = {
for (key, value) in args {
map.insert(Cow::Borrowed(*key), FluentValue::from(value.clone()));
};
&map
};
LOCALES.lookup_with_args(&*&SYSTEM_LOCALE, key, args_map)
}

View file

@ -5,3 +5,6 @@ metrics_preview = 📋 First 200 characters of metrics:
Response = Response
invalid-json-status-page = ❌ Error parssing status page JSON
invalid-json-heartbeat = ❌ Error parssing heartbeat JSON
invalid-uptime-key-format = Invalid format for uptime key. Expected format "monitorID_period". Received key: {key}
invalid-monitor-id = Invalid monitor ID: {id}
invalid-period-hours = Invalid period in hours: {hours}

View file

@ -5,3 +5,6 @@ missing_url = ❌ URL não fornecida. Use --url ou a variável de ambiente UPTIM
Response = Resposta
invalid-json-status-page = ❌ Falha ao parsear JSON do status page
invalid-json-heartbeat = ❌ Falha ao parsear JSON do heartbeat
invalid-uptime-key-format = Formato inválido na chave de uptime. Chave esperada no formato "monitorID_periodo". Chave recebida: {key}
invalid-monitor-id = ID de monitor inválido: {id}
invalid-period-hours = Período em horas inválido: {hours}

View file

@ -3,6 +3,7 @@ use crate::i18n::t;
use anyhow::{Context, Ok, Result};
pub fn parse_response(json_text: &str) -> Result<StatusPageResponse> {
let mut response: StatusPageResponse = serde_json::from_str(json_text)?; //.with_context(|| t("invalid-json-status-page"))?;
let mut response: StatusPageResponse =
serde_json::from_str(json_text).with_context(|| t("invalid-json-status-page"))?;
Ok(response)
}