mod catalog;
mod lang;
mod plural;
mod registry;
#[cfg(feature = "ui")]
pub mod ui;
use std::path::Path;
pub use inventory;
pub use lang::{LanguageInfo, LanguageManager, language_name};
pub use patois_macros::embed_domain;
#[cfg(feature = "ui")]
pub use patois_macros::embed_wx_translations;
pub struct EmbeddedDomain {
pub name: &'static str,
pub files: &'static [(&'static str, &'static [u8])],
}
inventory::collect!(EmbeddedDomain);
#[macro_export]
macro_rules! t {
($msgid:literal) => {
$crate::__translate(env!("CARGO_PKG_NAME"), $msgid)
};
}
#[macro_export]
macro_rules! nt {
($singular:literal, $plural:literal, $n:expr) => {
$crate::__translate_plural(env!("CARGO_PKG_NAME"), $singular, $plural, ($n) as u64)
};
}
pub fn init(domain: &str, locale_dir: impl AsRef<Path>, locale_id: &str) {
let mut reg = registry::global().lock().unwrap();
reg.register_path(domain, locale_dir.as_ref().to_path_buf());
reg.set_default_domain(domain);
reg.set_locale(locale_id);
}
pub fn t(s: &str) -> String {
let mut reg = registry::global().lock().unwrap();
let domain = reg.default_domain().map(str::to_string);
match domain {
Some(d) => reg.translate(&d, s),
None => s.to_string(),
}
}
pub fn nt(singular: &str, plural: &str, n: u64) -> String {
let mut reg = registry::global().lock().unwrap();
let domain = reg.default_domain().map(str::to_string);
match domain {
Some(d) => reg.translate_plural(&d, singular, plural, n),
None => {
if n == 1 {
singular.to_string()
} else {
plural.to_string()
}
}
}
}
pub fn set_locale(locale_id: &str) {
registry::global().lock().unwrap().set_locale(locale_id);
}
pub fn get_locale() -> String {
registry::global().lock().unwrap().locale().to_string()
}
pub fn set_default_domain(domain: &str) {
registry::global().lock().unwrap().set_default_domain(domain);
}
pub fn available_locales(domain: &str) -> Vec<&'static str> {
inventory::iter::<EmbeddedDomain>
.into_iter()
.filter(|ed| ed.name == domain)
.flat_map(|ed| ed.files.iter().map(|(locale, _)| *locale))
.collect()
}
pub fn system_locale() -> String {
sys_locale::get_locale().map(|l| registry::normalize_locale(&l)).unwrap_or_else(|| "en".to_string())
}
pub fn init_auto(domain: &str) {
let locale = system_locale();
let mut reg = registry::global().lock().unwrap();
reg.set_default_domain(domain);
reg.set_locale(&locale);
}
#[doc(hidden)]
pub fn __translate(domain: &str, msgid: &str) -> String {
registry::global().lock().unwrap().translate(domain, msgid)
}
#[doc(hidden)]
pub fn __translate_plural(domain: &str, singular: &str, plural: &str, n: u64) -> String {
registry::global().lock().unwrap().translate_plural(domain, singular, plural, n)
}