use crate::error::Error;
use crate::i18n::{I18n, View};
use crate::text::Text;
use std::borrow::Cow;
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
#[cfg(feature = "global")]
static GLOBAL: std::sync::OnceLock<I18n> = std::sync::OnceLock::new();
#[cfg(feature = "global")]
pub fn install(i18n: I18n) -> Result<(), Error> {
GLOBAL.set(i18n).map_err(|_| Error::AlreadyInstalled)
}
#[cfg(feature = "global")]
#[must_use]
pub fn global() -> Option<&'static I18n> {
GLOBAL.get()
}
pub(crate) fn global_instance() -> Option<&'static I18n> {
#[cfg(feature = "global")]
return GLOBAL.get();
#[cfg(not(feature = "global"))]
None
}
#[cold]
pub(crate) fn uninstalled(key: &str) {
#[cfg(debug_assertions)]
{
use std::sync::atomic::{AtomicBool, Ordering};
static WARNED: AtomicBool = AtomicBool::new(false);
if !WARNED.swap(true, Ordering::Relaxed) {
eprintln!(
"i18n-next: \"{key}\" shown before install() and outside a scope with an instance: the key stands in"
);
}
}
#[cfg(not(debug_assertions))]
let _ = key;
}
#[must_use]
pub fn t(key: impl Into<Cow<'static, str>>) -> Text {
Text::new(key.into(), None)
}
fn implicit() -> Option<I18n> {
current(true, false, false)
.0
.or_else(|| global_instance().cloned())
}
pub fn set_locale(code: &str) -> Result<(), Error> {
implicit().ok_or(Error::NotInstalled)?.set_locale(code)
}
#[must_use]
pub fn locale() -> Option<Arc<str>> {
let (i18n, locale, ..) = current(true, true, false);
locale.map(Code::arc).or_else(|| {
i18n.or_else(|| global_instance().cloned())
.map(|i| i.locale())
})
}
#[must_use]
pub fn page(name: &str) -> Option<View> {
let (i18n, locale, ..) = current(true, true, false);
let i18n = i18n.or_else(|| global_instance().cloned())?;
Some(View {
i18n,
locale: locale.map(Code::arc),
page: Some(Arc::from(name)),
})
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Code {
Static(&'static str),
Shared(Arc<str>),
}
impl Code {
pub(crate) fn as_str(&self) -> &str {
match self {
Code::Static(s) => s,
Code::Shared(s) => s,
}
}
fn arc(self) -> Arc<str> {
match self {
Code::Static(s) => Arc::from(s),
Code::Shared(s) => s,
}
}
}
impl From<Cow<'static, str>> for Code {
fn from(s: Cow<'static, str>) -> Self {
match s {
Cow::Borrowed(s) => Code::Static(s),
Cow::Owned(s) => Code::Shared(Arc::from(s)),
}
}
}
#[derive(Debug, Default)]
struct Frame {
instance: Option<I18n>,
locale: Option<Code>,
page: Option<Code>,
quiet: bool,
}
thread_local! {
#[allow(
clippy::missing_const_for_thread_local,
reason = "already `const`: clippy misreads the expansion on targets without native TLS (Android)"
)]
static STACK: RefCell<Vec<Frame>> = const { RefCell::new(Vec::new()) };
}
pub(crate) fn current(
instance: bool,
locale: bool,
page: bool,
) -> (Option<I18n>, Option<Code>, Option<Code>, bool) {
STACK
.try_with(|cell| {
let Ok(stack) = cell.try_borrow() else {
return (None, None, None, false);
};
if stack.is_empty() {
return (None, None, None, false);
}
let frames = || stack.iter().rev();
(
instance
.then(|| frames().find_map(|f| f.instance.clone()))
.flatten(),
locale
.then(|| frames().find_map(|f| f.locale.clone()))
.flatten(),
page.then(|| frames().find_map(|f| f.page.clone()))
.flatten(),
frames().any(|f| f.quiet),
)
})
.unwrap_or((None, None, None, false))
}
pub(crate) fn quiet() -> bool {
current(false, false, false).3
}
pub(crate) fn nest<R>(
instance: Option<&I18n>,
locale: Option<&Code>,
page: Option<&Code>,
quiet: bool,
f: impl FnOnce() -> R,
) -> R {
let frame = Frame {
instance: instance.cloned(),
locale: locale.cloned(),
page: page.cloned(),
quiet,
};
let _pushed = Pushed::new(frame, None);
f()
}
struct Pushed<'a> {
depth: usize,
back: Option<&'a mut Option<Frame>>,
}
impl<'a> Pushed<'a> {
fn new(frame: Frame, back: Option<&'a mut Option<Frame>>) -> Self {
let depth = STACK
.try_with(|cell| {
let mut stack = cell.borrow_mut();
stack.push(frame);
stack.len() - 1
})
.unwrap_or(usize::MAX);
Pushed { depth, back }
}
}
impl Drop for Pushed<'_> {
fn drop(&mut self) {
let _ = STACK.try_with(|cell| {
let mut stack = cell.borrow_mut();
if self.depth < stack.len() {
let mut frames = stack.drain(self.depth..);
if let Some(back) = self.back.take() {
*back = frames.next();
}
}
});
}
}
#[derive(Debug, Default)]
#[must_use]
pub struct Scope {
frame: Frame,
}
pub fn scope() -> Scope {
Scope::default()
}
impl Scope {
pub fn locale(mut self, code: impl AsRef<str>) -> Self {
self.frame.locale = Some(Code::Shared(Arc::from(code.as_ref())));
self
}
pub fn page(mut self, name: impl AsRef<str>) -> Self {
self.frame.page = Some(Code::Shared(Arc::from(name.as_ref())));
self
}
pub fn instance(mut self, i18n: I18n) -> Self {
self.frame.instance = Some(i18n);
self
}
pub fn run<R>(self, f: impl FnOnce() -> R) -> R {
let _pushed = Pushed::new(self.frame, None);
f()
}
pub fn wrap<F: Future>(self, fut: F) -> Scoped<F> {
Scoped {
frame: Some(self.frame),
fut: Box::pin(fut),
}
}
}
pub struct Scoped<F> {
frame: Option<Frame>,
fut: Pin<Box<F>>,
}
impl<F> std::fmt::Debug for Scoped<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Scoped")
.field("frame", &self.frame)
.finish_non_exhaustive()
}
}
impl<F: Future> Future for Scoped<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> {
let this = self.get_mut();
let frame = this.frame.take().unwrap_or_default();
let _pushed = Pushed::new(frame, Some(&mut this.frame));
this.fut.as_mut().poll(cx)
}
}
pub fn negotiate<I>(accept: &str, available: I) -> Option<String>
where
I: IntoIterator,
I::Item: AsRef<str>,
{
const MAX_ENTRIES: usize = 32;
const MAX_BYTES: usize = 4096;
let accept = match accept.as_bytes().get(..=MAX_BYTES) {
Some(head) => accept
.get(..head.iter().rposition(|&b| b == b',').unwrap_or(0))
.unwrap_or(""),
None => accept,
};
let mut refused: Vec<&str> = Vec::new();
let mut entries: Vec<(&str, u16)> = accept
.split(',')
.take(MAX_ENTRIES)
.filter_map(|entry| {
let mut fields = entry.split(';');
let tag = fields.next()?.trim();
let mut q = 1000;
for f in fields {
let f = f.trim_matches([' ', '\t']);
let v = f.strip_prefix("q=").or_else(|| f.strip_prefix("Q="))?;
q = quality(v)?;
}
if tag.is_empty() {
return None;
}
if q == 0 {
refused.push(tag);
return None;
}
Some((tag, q))
})
.collect();
entries.sort_by_key(|e| std::cmp::Reverse(e.1));
let ok = |a: &str| !refused.iter().any(|r| same_tag(a, r));
let available: Vec<I::Item> = available.into_iter().filter(|a| ok(a.as_ref())).collect();
let find = |hit: &dyn Fn(&str) -> bool| {
available
.iter()
.map(AsRef::as_ref)
.find(|a| hit(a))
.map(str::to_owned)
};
for (tag, _) in entries {
if tag == "*" {
if let Some(a) = find(&|_| true) {
return Some(a);
}
continue;
}
let mut t = tag;
loop {
if ok(t)
&& let Some(a) = find(&|a| same_tag(a, t))
{
return Some(a);
}
match t.rfind(['-', '_']) {
Some(cut) => t = t.get(..cut).unwrap_or(""),
None => break,
}
}
let want = script(tag);
let same_lang = |a: &str| same_tag(primary(a), t);
if let Some(a) = want
.and_then(|s| find(&|a| same_lang(a) && script(a).is_some_and(|x| same_tag(x, s))))
.or_else(|| find(&same_lang))
{
return Some(a);
}
}
None
}
fn script(tag: &str) -> Option<&str> {
tag.split(['-', '_'])
.nth(1)
.filter(|s| s.len() == 4 && s.bytes().all(|b| b.is_ascii_alphabetic()))
}
fn quality(v: &str) -> Option<u16> {
let (int, frac) = v.split_once('.').unwrap_or((v, ""));
if frac.len() > 3 || !frac.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
let mut n: u16 = match int {
"0" => 0,
"1" => 1000,
_ => return None,
};
for (i, b) in frac.bytes().enumerate() {
n += u16::from(b - b'0') * [100, 10, 1][i];
}
(n <= 1000).then_some(n)
}
fn primary(tag: &str) -> &str {
tag.split(['-', '_']).next().unwrap_or(tag)
}
fn same_tag(a: &str, b: &str) -> bool {
a.len() == b.len()
&& a.bytes().zip(b.bytes()).all(|(x, y)| {
let norm = |c: u8| {
if c == b'_' {
b'-'
} else {
c.to_ascii_lowercase()
}
};
norm(x) == norm(y)
})
}