use std::{cell::OnceCell, rc::Rc};
use cranpose_core::{CompositionLocal, compositionLocalOf};
use cranpose_ui::Locale;
pub fn system_languages() -> Vec<Locale> {
let supplied = cranpose_services::host_controller()
.map_or_else(Vec::new, |host| host.preferred_languages());
let tags = if supplied.is_empty() {
sys_locale::get_locales().collect()
} else {
supplied
};
tags.into_iter()
.filter_map(|tag| Locale::parse(&tag).ok())
.collect()
}
pub fn local_system_languages() -> CompositionLocal<Rc<[Locale]>> {
thread_local! {
static LOCAL: OnceCell<CompositionLocal<Rc<[Locale]>>> = const { OnceCell::new() };
}
LOCAL.with(|local| {
local
.get_or_init(|| compositionLocalOf(|| system_languages().into()))
.clone()
})
}
#[expect(non_snake_case)]
#[track_caller]
pub fn ProvideApplicationLocalization(
application: cranpose_ui::localization::Localization,
content: impl FnOnce(),
) {
let system = local_system_languages().current();
cranpose_ui::ProvideLanguagePreferences(application, &system, content);
}