#![doc = include_str!("../README.md")]
#[doc(hidden)]
pub mod meta;
#[doc(hidden)]
pub mod registry;
#[cfg(feature = "derive")]
pub use es_fluent_derive::{EsFluent, EsFluentChoice, EsFluentThis, EsFluentVariants};
#[doc(hidden)]
pub use fluent_bundle::FluentValue;
#[doc(hidden)]
pub use inventory as __inventory;
#[doc(hidden)]
pub use rust_embed as __rust_embed;
#[doc(hidden)]
pub use es_fluent_manager_core as __manager_core;
#[doc(hidden)]
pub use unic_langid;
use arc_swap::ArcSwap;
use es_fluent_manager_core::FluentManager;
use std::sync::{Arc, OnceLock};
#[cfg(feature = "build")]
pub mod build {
pub use es_fluent_toml::build::*;
}
mod traits;
pub use traits::{EsFluentChoice, FluentDisplay, ThisFtl, ToFluentString};
#[doc(hidden)]
static CONTEXT: OnceLock<ArcSwap<FluentManager>> = OnceLock::new();
#[doc(hidden)]
static CUSTOM_LOCALIZER: OnceLock<
Box<
dyn Fn(&str, Option<&std::collections::HashMap<&str, FluentValue>>) -> Option<String>
+ Send
+ Sync,
>,
> = OnceLock::new();
#[doc(hidden)]
pub fn set_context(manager: FluentManager) {
CONTEXT
.set(ArcSwap::from_pointee(manager))
.map_err(|_| "Context already set")
.expect("Failed to set context");
}
#[doc(hidden)]
pub fn set_shared_context(manager: Arc<FluentManager>) {
CONTEXT
.set(ArcSwap::new(manager))
.map_err(|_| "Context already set")
.expect("Failed to set shared context");
}
#[doc(hidden)]
pub fn set_custom_localizer<F>(localizer: F)
where
F: Fn(&str, Option<&std::collections::HashMap<&str, FluentValue>>) -> Option<String>
+ Send
+ Sync
+ 'static,
{
CUSTOM_LOCALIZER
.set(Box::new(localizer))
.map_err(|_| "Custom localizer already set")
.expect("Failed to set custom localizer");
}
#[doc(hidden)]
pub fn select_language(lang: &unic_langid::LanguageIdentifier) {
if let Some(context) = CONTEXT.get() {
context.load().select_language(lang);
}
}
#[doc(hidden)]
pub fn localize<'a>(
id: &str,
args: Option<&std::collections::HashMap<&str, FluentValue<'a>>>,
) -> String {
if let Some(custom_localizer) = CUSTOM_LOCALIZER.get()
&& let Some(message) = custom_localizer(id, args)
{
return message;
}
if let Some(context) = CONTEXT.get()
&& let Some(message) = context.load().localize(id, args)
{
return message;
}
tracing::warn!("Translation for '{}' not found or context not set.", id);
id.to_string()
}