♻️ [global] Incorpora identificador de idioma

This commit is contained in:
Manuel Cillero 2022-11-10 22:02:12 +01:00
parent c7bf549434
commit e143ee57ac
5 changed files with 29 additions and 30 deletions

View file

@ -6,8 +6,6 @@ pub use actix_web_static_files::ResourceFiles;
mod banner;
pub mod locale;
#[cfg(feature = "database")]
pub mod db;

View file

@ -23,7 +23,7 @@ impl Application {
LazyStatic::force(&global::TRACING);
// Valida el identificador de idioma.
LazyStatic::force(&super::locale::LANGID);
LazyStatic::force(&global::LANGID);
#[cfg(feature = "database")]
// Conecta con la base de datos.

View file

@ -1,22 +0,0 @@
use crate::{global, trace, LazyStatic};
use unic_langid::LanguageIdentifier;
/// Almacena el Identificador de Idioma Unicode
/// ([Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier))
/// de la aplicación, obtenido de `SETTINGS.app.language`.
pub static LANGID: LazyStatic<LanguageIdentifier> =
LazyStatic::new(|| match global::SETTINGS.app.language.parse() {
Ok(language) => language,
Err(_) => {
trace::warn!(
"{}, {} \"{}\"! {}, {}",
"Failed to parse language",
"unrecognized Unicode Language Identifier",
global::SETTINGS.app.language,
"Using \"en-US\"",
"check the settings file",
);
"en-US".parse().unwrap()
}
});

View file

@ -1,10 +1,12 @@
use crate::{pub_config, LazyStatic};
use crate::{pub_config, trace, LazyStatic};
use serde::Deserialize;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::EnvFilter;
use unic_langid::LanguageIdentifier;
// CONFIGURACIÓN ***********************************************************************************
#[derive(Debug, Deserialize)]
@ -189,3 +191,24 @@ pub(crate) static TRACING: LazyStatic<WorkerGuard> = LazyStatic::new(|| {
guard
});
// LOCALIZACIÓN ************************************************************************************
/// Almacena el Identificador de Idioma Unicode
/// ([Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier))
/// de la aplicación, obtenido de `SETTINGS.app.language`.
pub static LANGID: LazyStatic<LanguageIdentifier> =
LazyStatic::new(|| match SETTINGS.app.language.parse() {
Ok(language) => language,
Err(_) => {
trace::warn!(
"{}, {} \"{}\"! {}, {}",
"Failed to parse language",
"unrecognized Unicode Language Identifier",
SETTINGS.app.language,
"Using \"en-US\"",
"check the settings file",
);
"en-US".parse().unwrap()
}
});

View file

@ -102,8 +102,8 @@ pub use fluent_templates::{static_loader as static_locale, Loader as Locale};
/// Permite integrar fácilmente localización en temas, módulos y componentes.
macro_rules! pub_locale {
( $dir_locales:literal $(, $core_locales:literal)? ) => {
use $crate::global;
use $crate::locale::*;
use $crate::app::locale::LANGID;
static_locale! {
static LOCALES = {
@ -118,7 +118,7 @@ macro_rules! pub_locale {
#[allow(dead_code)]
fn l(key: &str) -> String {
LOCALES.lookup(&LANGID, key).unwrap_or(key.to_string())
LOCALES.lookup(&global::LANGID, key).unwrap_or(key.to_string())
}
#[allow(dead_code)]
@ -126,7 +126,7 @@ macro_rules! pub_locale {
key: &str,
args: &std::collections::HashMap<String, FluentValue>
) -> String {
LOCALES.lookup_with_args(&LANGID, key, args).unwrap_or(key.to_string())
LOCALES.lookup_with_args(&global::LANGID, key, args).unwrap_or(key.to_string())
}
#[allow(dead_code)]
@ -135,7 +135,7 @@ macro_rules! pub_locale {
args: &std::collections::HashMap<String, FluentValue>
) -> $crate::html::PreEscaped<String> {
$crate::html::PreEscaped(
LOCALES.lookup_with_args(&LANGID, key, args).unwrap_or(key.to_string())
LOCALES.lookup_with_args(&global::LANGID, key, args).unwrap_or(key.to_string())
)
}
};