#[cfg(test)]
mod tests;
use crate::error::{AppError, AppResult, ErrorDetail};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Locale {
En,
PtBr,
}
impl Locale {
pub fn as_bcp47(self) -> &'static str {
match self {
Self::En => "en",
Self::PtBr => "pt-BR",
}
}
pub fn resolve(explicit: Option<&str>) -> AppResult<Self> {
if let Some(tag) = explicit {
return Self::parse_supported(tag);
}
Ok(Self::from_system())
}
pub fn parse_supported(tag: &str) -> AppResult<Self> {
let t = normalize_lang_tag(tag);
if t.is_empty() {
return Err(AppError::of(ErrorDetail::UnsupportedLang {
tag: String::new(),
}));
}
if t == "en" || t.starts_with("en-") {
return Ok(Self::En);
}
if t == "pt-br" {
return Ok(Self::PtBr);
}
Err(AppError::of(ErrorDetail::UnsupportedLang {
tag: tag.to_string(),
}))
}
#[cfg(test)]
pub fn resolve_ignores_product_env_for_tests() -> AppResult<Self> {
Self::resolve(None)
}
pub fn from_system() -> Self {
match sys_locale::get_locale() {
Some(sys) => {
let loc = Self::soft_from_tag(&sys);
tracing::debug!(
target: "docsrs_cli::i18n",
system_locale = %sys,
resolved = %loc.as_bcp47(),
"locale resolved from system"
);
loc
}
None => {
tracing::debug!(
target: "docsrs_cli::i18n",
"sys-locale returned None; defaulting to en"
);
Self::En
}
}
}
pub fn soft_from_tag(tag: &str) -> Self {
let t = normalize_lang_tag(tag);
if t == "pt" || t.starts_with("pt-") {
Self::PtBr
} else {
Self::En
}
}
pub fn progress_fetching(self, target: &str) -> String {
match self {
Self::En => format!("fetching {target}..."),
Self::PtBr => format!("buscando {target}..."),
}
}
pub fn error_prefix(self) -> &'static str {
match self {
Self::En => "error",
Self::PtBr => "erro",
}
}
pub fn format_error(self, err: &AppError) -> String {
format!("{}: {}", self.error_prefix(), err.localized(self))
}
pub fn doctor_ok(self, ok: bool) -> String {
match (self, ok) {
(Self::En, true) => "doctor: ok".into(),
(Self::En, false) => "doctor: failed".into(),
(Self::PtBr, true) => "doctor: ok".into(),
(Self::PtBr, false) => "doctor: falhou".into(),
}
}
pub fn from_argv_for_bootstrap<I, T>(args: I) -> Self
where
I: IntoIterator<Item = T>,
T: AsRef<std::ffi::OsStr>,
{
const FLAG: &str = "--lang";
let mut want_value = false;
for arg in args {
let Some(a) = arg.as_ref().to_str() else {
want_value = false;
continue;
};
if want_value {
return Self::soft_from_tag(a);
}
if let Some(tag) = a.strip_prefix("--lang=") {
return Self::soft_from_tag(tag);
}
if a == FLAG {
want_value = true;
}
}
Self::En
}
pub fn bootstrap_provider_conflict(self) -> &'static str {
match self {
Self::En => {
"docsrs-cli: rustls CryptoProvider was already installed; refusing dual init"
}
Self::PtBr => {
"docsrs-cli: o CryptoProvider do rustls já estava instalado; recusando init duplo"
}
}
}
pub fn bootstrap_runtime_failure(self, detail: &str) -> String {
match self {
Self::En => format!("docsrs-cli: failed to build async runtime: {detail}"),
Self::PtBr => format!("docsrs-cli: falha ao construir o runtime assíncrono: {detail}"),
}
}
}
fn normalize_lang_tag(tag: &str) -> String {
let trimmed = tag.trim();
let base = trimmed.split(['.', '@']).next().unwrap_or(trimmed).trim();
base.replace('_', "-").to_ascii_lowercase()
}