use std::collections::BTreeMap;
use std::fmt;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
)]
#[serde(transparent)]
pub struct Locale(pub String);
impl Locale {
#[must_use]
pub fn new(tag: impl Into<String>) -> Self {
Self(tag.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn language(&self) -> &str {
self.0.split(['-', '_']).next().unwrap_or(self.0.as_str())
}
#[must_use]
pub fn region(&self) -> Option<&str> {
let mut parts = self.0.split(['-', '_']);
parts.next()?;
parts.next().filter(|s| !s.is_empty())
}
#[must_use]
pub fn same_language(&self, other: &Locale) -> bool {
self.language().eq_ignore_ascii_case(other.language())
}
}
impl Default for Locale {
fn default() -> Self {
Self::new("en")
}
}
impl From<&str> for Locale {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<String> for Locale {
fn from(value: String) -> Self {
Self(value)
}
}
impl fmt::Display for Locale {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub struct LocalizedText {
pub default: String,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub translations: BTreeMap<Locale, String>,
}
impl LocalizedText {
#[must_use]
pub fn new(default: impl Into<String>) -> Self {
Self {
default: default.into(),
translations: BTreeMap::new(),
}
}
#[must_use]
pub fn with(mut self, locale: impl Into<Locale>, text: impl Into<String>) -> Self {
self.translations.insert(locale.into(), text.into());
self
}
#[must_use]
pub fn resolve(&self, locale: &Locale) -> &str {
if let Some(exact) = self.translations.get(locale) {
return exact;
}
self.translations
.iter()
.find(|(candidate, _)| candidate.same_language(locale))
.map_or(self.default.as_str(), |(_, text)| text.as_str())
}
}
impl From<&str> for LocalizedText {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for LocalizedText {
fn from(value: String) -> Self {
Self::new(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn locale_parts() {
let l = Locale::from("it-IT");
assert_eq!(l.language(), "it");
assert_eq!(l.region(), Some("IT"));
assert_eq!(Locale::from("en").region(), None);
assert!(Locale::from("it-CH").same_language(&l));
}
#[test]
fn localized_text_resolution_order() {
let text = LocalizedText::new("Confirm")
.with("it-IT", "Conferma")
.with("de", "Bestätigen");
assert_eq!(text.resolve(&Locale::from("it-IT")), "Conferma");
assert_eq!(text.resolve(&Locale::from("it-CH")), "Conferma");
assert_eq!(text.resolve(&Locale::from("de-AT")), "Bestätigen");
assert_eq!(text.resolve(&Locale::from("fr")), "Confirm");
}
#[test]
fn localized_text_round_trip() {
let text = LocalizedText::new("x").with("it", "y");
let json = serde_json::to_string(&text).unwrap();
let back: LocalizedText = serde_json::from_str(&json).unwrap();
assert_eq!(back, text);
assert_eq!(
serde_json::to_string(&LocalizedText::new("x")).unwrap(),
r#"{"default":"x"}"#
);
}
}